gpt4 book ai didi

css - 为什么固定位置元素显示在嵌套绝对值之上?

转载 作者:行者123 更新时间:2023-11-28 09:28:33 25 4
gpt4 key购买 nike

例如,采用以下 HTML 和 CSS:

<div class="fixed"></div>
<div class="wrapper">
<div class="child"></div>
</div>

.fixed {
background: blue;
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
z-index: 2;
}

.wrapper, .child {
position: absolute;
height: 20px;
width: 20px;
padding: 20px;
}

.wrapper {
z-index: 1;
background: red;
}

.child {
position: absolute;
z-index: 3;
background: yellow;
}

预期的行为是 .child 显示在 .fixed 上方,而 .wrapperhttp://jsfiddle.net/STLMR/ 上是不可见的.fixed 显示在最上面(在 Chrome + Firefox 中测试)。这有什么诀窍,还是我缺少一些 CSS 怪癖?

最佳答案

在CSS中,z-index不是绝对的,而是相对于父容器的。对于“绝对”,我指的不是 position: absolute 属性,我这样说是因为它可能会造成混淆。

相关:https://stackoverflow.com/a/7490187/671092

关于css - 为什么固定位置元素显示在嵌套绝对值之上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14198766/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com