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;
}
相关推荐
利刃大大7 分钟前
【Vue】props完整语法 && 非父子组件通信 && provide && inject && eventBus
前端·javascript·vue.js
萧曵 丶10 分钟前
前端工程化项目中全类型配置文件的详细解析
前端·javascript·配置文件·工程化
讯方洋哥17 分钟前
HarmonyOS App开发——鸿蒙公共事件App开发
服务器·前端·javascript
Можно20 分钟前
吃透 Vue 的 v - 指令家族:从入门到精通
前端·javascript·vue.js
光影少年31 分钟前
react的hooks优缺点、底层实现及hooks参数
前端·react.js·前端框架
qq_124987075335 分钟前
基于SpringBoot+Vue的旅游信息咨询网站的设计与实现(源码+论文+部署+安装)
java·vue.js·spring boot·毕业设计·旅游·计算机毕设·计算机毕业设计
weixin_4569074143 分钟前
2026+:html+css 生态的成型之年与平台化跃迁
前端·css·html
上海合宙LuatOS1 小时前
LuatOS框架的使用(2)
java·服务器·开发语言·前端·数据库·嵌入式硬件·php
江湖有缘1 小时前
Docker部署NextTrace Web路由工具
前端·docker·容器
wljt1 小时前
游标分页原理
java·前端·数据库