【发布时间】:2017-09-13 23:37:00
【问题描述】:
我试图将一个子 div 放在其父元素之下和其他元素之上。
.box1{
background-color: blue;
width: 500px;
height: 100px;
position: relative;
z-index: 3;
}
.box2{
position: absolute;
background-color: red;
width: 200px;
height: 100px;
left: 30%;
top: 20px;
z-index: 2;
}
.box3{
background-color: yellow;
width: 500px;
height: 100px;
z-index: 1;
}
<div class="box1">
<div class="box2"></div>
</div>
<div class="box3"></div>
我想将红色矩形定位在蓝色下方和黄色上方。我将 z-index 放在其中三个上。但是,它不起作用。
您对此有何看法?谢谢!
更新:虽然这些框的顺序正确,但是这些框内的元素现在无法点击。
您可以在这里查看错误:https://jsfiddle.net/p1xd6zah/
【问题讨论】:
-
蓝色和黄色框有不同的stacking contexts。由于蓝色盒子的 z-index 为 3,黄色盒子的 z-index 为 1,因此无论内在的孩子(你可以将它设置为 1000,它不会改变任何东西)。将红色夹在两者之间的唯一方法是确保红色框是其他两个框的兄弟。
标签: html css css-position z-index