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;
}
相关推荐
excel2 小时前
为什么在 Three.js 中平面能产生“起伏效果”?
前端
excel3 小时前
Node.js 断言与测试框架示例对比
前端
天蓝色的鱼鱼4 小时前
前端开发者的组件设计之痛:为什么我的组件总是难以维护?
前端·react.js
codingandsleeping4 小时前
使用orval自动拉取swagger文档并生成ts接口
前端·javascript
石金龙5 小时前
[译] Composition in CSS
前端·css
白水清风5 小时前
微前端学习记录(qiankun、wujie、micro-app)
前端·javascript·前端工程化
Ticnix6 小时前
函数封装实现Echarts多表渲染/叠加渲染
前端·echarts
用户22152044278006 小时前
new、原型和原型链浅析
前端·javascript
阿星做前端6 小时前
coze源码解读: space develop 页面
前端·javascript
叫我小窝吧6 小时前
Promise 的使用
前端·javascript