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的右侧,从而实现重叠

相关推荐
Java展博8 小时前
[前端够用就好] 跨域报错时,浏览器到底在拦谁
前端·状态模式
用户112936112199 小时前
npm 都提示成功了,node_modules 为什么还占着 370M?
linux·前端·后端
掘金安东尼10 小时前
Agent Harness 也有 benchmark:评分榜与评测工作台现状
前端·agent
digger_41910 小时前
从零理解 Electron:主进程、渲染进程、preload 与 IPC 实战
前端·electron
去伪存真11 小时前
别让你的 RAG 平台“花五份钱、答一份结巴的题”:幂等性架构的自我修养
前端·agent
snow@li11 小时前
前端:导入EXCEL并解析文件内容,demo
前端·javascript·vue.js
补码缩写12 小时前
压缩PNG不损失透明:只量化RGB、alpha原样留的减色流程
前端
RuoyiOffice12 小时前
Flowable只是起点:一套OA背后的单据、资源与协作设计
java·vue.js·spring boot·flowable·企业管理·oa·ruoyi office
TDengine (老段)12 小时前
TDengine IDMP 知识库与知识图谱
大数据·前端·物联网·知识图谱·时序数据库·tdengine·涛思数据
goehou14 小时前
PNG 转 WebP 不一定变小,我量了三类图
前端·性能优化·webp·sharp