html代码如下:
<div class="first common">
i am the first div.
<div class="second common">
i am the second div.
<div class="third common">
i am the third div.
</div>
</div>
</div>
css:
.common {
position: absolute;
left: 50px;
top: 50px;
width: 200px;
height: 200px;
color: #fff;
}
.first {
background-color: #8EE5EE;
}
.second {
background-color: #7A67EE;
}
.third {
background-color: #0000AA;
}
然后显示效果是这样的:
那么问题来了,现在我想让第一个div
的层级最高,第三个在最下面,这样依次类推,可是我试过z-index
加定位
都无法实现这个效果,应该怎么写呢?找过很多答案,都是父子div
之间的,像这样的3级或者更多层级的div
相关的都没有提到。
我所了解的,父子之间只能通过负的z-index来让父元素高于子元素,负z-index的前提是父元素没有设置z-index,但是你这个嵌套有三层,第二层必定要设置负z-index,所以第三层就没办法了。