Vue中集中常见的布局方式

  • 布局叠加
  • 完整代码
  • 最外层的Container设置为relative,内部的几个box设置为absolute
js 复制代码
<template>
  <div class="container">
    <div class="box box1">Box 1</div>
    <div class="box box2">Box 2</div>
    <div class="box box3">Box 3</div>
  </div>
</template>

<script>
export default {
  // 组件逻辑
};
</script>

<style scoped>
.container {
  position: relative;
  width: 300px;
  height: 300px;
  background: #888888;
}

.box {
  position: absolute;
  width: 100px;
  height: 100px;
  text-align: center;
  line-height: 100px;
}

.box1 {
  background-color: red;
  top: 50px;
  left: 50px;
}

.box2 {
  background-color: blue;
  top: 100px;
  left: 100px;
}

.box3 {
  background-color: green;
  top: 150px;
  left: 150px;
}
</style>
  • 如果Container不设置relative则,内部的box则会相对整个页面进行布局
  • 使用绝对布局,一个居中,一个居右。
  • 其中box1,左上距离父布局50%,然后自身偏移50%,使其居中
js 复制代码
.box1 {
  background-color: red;
  top: 50%;
  left: 50%;
  transform: translate(-50%,-50%);
}
  • 完整代码
js 复制代码
<template>

  <div class="container">
    <div class="box box1">Box 1</div>
    <div class="box box3">Box 3</div>
  </div>

</template>

<script>
export default {
  // 组件逻辑
};
</script>

<style scoped>
.container {
  position: relative;
  width: 300px;
  height: 300px;
  background: #888888;
}

.box {
  position: absolute;
  width: 100px;
  height: 100px;
  text-align: center;
  line-height: 100px;
}

.box1 {
  background-color: red;
  top: 50%;
  left: 50%;
  transform: translate(-50%,-50%);
}

.box3 {
  background-color: green;
  top: 50%;
  right: 0;
  transform: translateY(-50%);
}
</style>
  • 水平方向线性布局
  • 设置父布局 display: flex; flex-direction: row;子布局取消position: absolute;即可
js 复制代码
.container {
  display: flex;
  flex-direction: row;
  width: 300px;
  height: 300px;
  background: #888888;
}

.box {
  //position: absolute;
  width: 100px;
  height: 100px;
  text-align: center;
  line-height: 100px;
}
相关推荐
天天进步20152 分钟前
Webpack到Vite:构建工具迁移实战经验总结
前端·webpack·node.js
0***144 分钟前
免费的WebAssembly模块打包,Webpack配置
前端·webpack·wasm
小胖学前端4 分钟前
解决 uniapp H5 与原生应用通信的坑:一个经过实战验证的解决方案
前端·uni-app
LaoZhangAI5 分钟前
Gemini 2.5 Flash Image API尺寸设置完整指南:10种宽高比详解
前端·后端
kangyouwei7 分钟前
鸿蒙开发:19-本地开发配置bash环境执行hvigorw命令
前端·harmonyos
Achieve前端实验室10 分钟前
JavaScript 原型/原型链
前端·javascript
一碗下酒菜12 分钟前
React 闭包陷阱详解
前端
littleplayer13 分钟前
ArkTs单元测试 UnitTest 指南
前端
LXA080914 分钟前
vue3开发使用框架推荐
前端·javascript·vue.js
拿不拿铁1934 分钟前
Vite & Webpack & Rollup 入口与产出配置与示例
前端