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;
}
相关推荐
油丶酸萝卜别吃36 分钟前
jquery-ajax.js 说明文档
前端·javascript·jquery
windliang43 分钟前
Claude Code 源码分析(九):子 Agent 如何分叉、继续与回到父会话
前端·javascript·面试
柒和远方1 小时前
V063: TS 面试必考:interface 与 type 的四大差异,与 LLM Harness 的自动化择优
前端·javascript
半个落月1 小时前
React useRef 详解:DOM 引用、持久化值与 Worker 实例
前端·react.js
阿黎梨梨1 小时前
TypeScript 类型编程:从新手到 Harness 工程实践
前端
黄金决明子1 小时前
Vue3 + Vite 打包后打开空白?
前端
张龙6871 小时前
终端效率翻倍实战:fzf + zoxide + ripgrep + bat 组合拳,告别重复敲命令
前端
宿6742 小时前
vue3-env环境
前端·vue.js
蔬菜_2 小时前
前端转全栈-day5(数组、list、set)
java·前端·数据结构·list
汉堡大王95272 小时前
Vue 3 + TS + Element Plus 实战:如何从零搭建企业级违章记录管理 SaaS 前端
前端·javascript·vue.js