Kir*_*oss 6 html css position z-index
我试图理解为什么这个例子中的蓝色 div 并不总是在上面?即为什么红色 div #2 位于蓝色孩子 1 的顶部。
body {
padding: 30px;
}
.red1 {
position: absolute;
z-index: 1;
width: 400px;
height: 200px;
background: red;
}
.red2 {
position: absolute;
z-index: 1;
width: 400px;
height: 200px;
top: 250px;
background: red;
}
.blue {
z-index: 9;
padding: 10px;
text-align: center;
color: white;
text-transform: uppercase;
position: absolute;
top: 100px;
left: 100px;
width: 150px;
height: 130px;
background: blue;
}
Run Code Online (Sandbox Code Playgroud)
<div class="red1">
<div class="blue">
blue child 1
</div>
</div>
<div class="red2">
<div class="blue">
blue child 2
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
因为.red1
和.red2
形成不同的堆叠上下文。
一个堆叠上下文中的元素不与另一堆叠上下文中的元素一起参与。
如果您给出.red2
a z-index: -1
,您将得到您期望的行为(演示)。
这是因为.red1
和.red2
都是绝对定位的,没有定位的祖先。这意味着根元素是它们的直接祖先,并且根元素形成堆叠上下文。
更多详细信息请参见此处:
归档时间: |
|
查看次数: |
7002 次 |
最近记录: |