html - 当父级的 z-index 与另一个 div 相等时,子级的 z-index 较高会隐藏绝对子级
问题描述
我有一个限制:这两个父 div 不能是“一个高于另一个”,它们的重要性相同。
我有 2 个主要 div,它们都是z-index: 2
. 在第一个 div 里面,我有一个child
who z-index is 99999
,现在,因为relative
和static
都被浏览器以先来后到的方式处理,也就是说,div2 的顺序比 div1 高,我的绝对child
里面div1
会在后面div2
. 手表:
#item1 {
width: 100%;
height: 200px;
background-color: gray;
position: relative;
z-index: 2;
}
#child {
position: absolute;
bottom: -15px;
width: 50px;
height: 50px;
border-radius: 50%;
background: white;
z-index: 15;
}
#item2 {
width: 100%;
height: 200px;
background-color: green;
position: relative;
z-index: 2;
}
<div id="item1">
<div id="child">
</div>
</div>
<div id="item2">
</div>
我在这里想念什么?据说网络上充满了relative
一个又一个的 div,它们里面有绝对的 div。
解决方案
增加z-index
父项 ( #item1
) 或从两个父项中删除 z-index。它会起作用的。
实际上你不需要z-index
在父元素中使用,如果你需要使用z-index
那么给第一个父元素更高,浏览器在第二个元素上给予比第一个元素更高的优先级(z-index),因为浏览器需要在第一个元素上显示第二个元素。
#item1 {
width: 100%;
height: 200px;
background-color: gray;
position: relative;
}
#child {
position: absolute;
bottom: -15px;
width: 50px;
height: 50px;
border-radius: 50%;
background: white;
z-index: 15;
}
#item2 {
width: 100%;
height: 200px;
background-color: green;
position: relative;
}
<div id="item1">
<div id="child">
</div>
</div>
<div id="item2">
</div>
推荐阅读
- node.js - Typescript / Eslint 在我开始的每个项目中都会抛出“无法读取文件 tsconfig.json”
- c# - GZipStream 和压缩块到文件
- aws-lambda - 我需要将 lambda 日志解析为字段
- html-email - 我完成了一个项目,但是当我在附加文件时通过电子邮件发送文件时,图像没有出现我如何修复它以便它们出现?
- reactjs - 无法获取数组的长度
- arrays - 使用汇编交换数组中的第一个和最后一个元素
- reactjs - 你能解释一下这个反应原生代码(渲染方法中的奇怪箭头函数)吗?
- java - 如何使用 Scanner.useDelimiter() 匹配两个相邻的字符后跟一个单词?
- flutter - 在颤动中设置只读值
- javascript - React - renderToString 如何将样式应用于字符串