【发布时间】:2016-07-26 17:24:01
【问题描述】:
我试图理解为什么这个例子中的蓝色 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;
}
<div class="red1">
<div class="blue">
blue child 1
</div>
</div>
<div class="red2">
<div class="blue">
blue child 2
</div>
</div>
【问题讨论】:
-
我希望第一个红色 div 中的蓝色孩子位于红色 div 2 的顶部。两个蓝色 div 的 z-index 均为 9,两个红色的 z-index 均为 1,所以怎么能红色会出现在蓝色之上吗?