vue3中父div设置display flex,2个子div重叠

在Vue 3中,若要设置父div使用flex布局并且使得2个子div重叠,可以在父div上使用样式display: flex以及position: relative,然后在子div上使用position: absolute来定位。

复制代码
<template>
  <div class="parent">
    <div class="child child1">Child 1</div>
    <div class="child child2">Child 2</div>
  </div>
</template>
 
<script>
export default {
  // Vue 3 组件选项
};
</script>
 
<style>
.parent {
  display: flex;
  position: relative;
  width: 100%;
  height: 200px; /* 父div的高度 */
}
 
.child {
  position: absolute;
  width: 100%;
  height: 100%;
}
 
.child1 {
  background-color: red;
}
 
.child2 {
  background-color: blue;
  /* 将第二个子div向右移动,以显示出重叠效果 */
  right: 0;
}
</style>

div设置了display: flex来使用flexbox布局。两个子div通过position: absolute被绝对定位于父div的同一位置,并且通过right: 0属性使得第二个子div被推到了父div的右侧,从而实现重叠

相关推荐
前端小趴菜0536 分钟前
React - 组件通信
前端·react.js·前端框架
Amy_cx1 小时前
在表单输入框按回车页面刷新的问题
前端·elementui
dancing9991 小时前
cocos3.X的oops框架oops-plugin-excel-to-json改进兼容多表单导出功能
前端·javascript·typescript·游戏程序
后海 0_o2 小时前
2025前端微服务 - 无界 的实战应用
前端·微服务·架构
Scabbards_2 小时前
CPT304-2425-S2-Software Engineering II
前端
小满zs2 小时前
Zustand 第二章(状态处理)
前端·react.js
程序猿小D2 小时前
第16节 Node.js 文件系统
linux·服务器·前端·node.js·编辑器·vim
萌萌哒草头将军2 小时前
🚀🚀🚀Prisma 发布无 Rust 引擎预览版,安装和使用更轻量;支持任何 ORM 连接引擎;支持自动备份...
前端·javascript·vue.js
狼性书生2 小时前
uniapp实现的简约美观的星级评分组件
前端·uni-app·vue·组件
书语时2 小时前
ES6 Promise 状态机
前端·javascript·es6