使用 z-index 将元素定位在 div 下
Position an element under a div by using z-index
我试图放置一个子元素 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/
你可以 hack z-index
:
您可以将 z-index: -1
添加到 box2
。 (将 child 堆叠在 parent 下方)
将z-index: -2
和position: relative
添加到box3
(现在把这个放在box2
后面)
从 box1
中删除 z-index
- 请参阅下面的演示:
.box1 {
background-color: blue;
width: 500px;
height: 100px;
position: relative;
}
.box2 {
position: absolute;
background-color: red;
width: 200px;
height: 100px;
left: 30%;
top: 20px;
z-index: -1;
}
.box3 {
background-color: yellow;
width: 500px;
height: 100px;
z-index: -2;
position: relative;
}
<div class="box1">
<div class="box2"></div>
</div>
<div class="box3"></div>
我试图放置一个子元素 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/
你可以 hack z-index
:
您可以将
z-index: -1
添加到box2
。 (将 child 堆叠在 parent 下方)将
z-index: -2
和position: relative
添加到box3
(现在把这个放在box2
后面)
从 box1
中删除 z-index
- 请参阅下面的演示:
.box1 {
background-color: blue;
width: 500px;
height: 100px;
position: relative;
}
.box2 {
position: absolute;
background-color: red;
width: 200px;
height: 100px;
left: 30%;
top: 20px;
z-index: -1;
}
.box3 {
background-color: yellow;
width: 500px;
height: 100px;
z-index: -2;
position: relative;
}
<div class="box1">
<div class="box2"></div>
</div>
<div class="box3"></div>