z-index
属性指定 元素的堆栈顺序。
z-index 属性
定位元素时,它们可以与其他元素重叠。
z-index
属性指定元素的堆栈顺序(哪个元素应放在其他元素的前面或后面)。
元素可以具有正或负堆栈顺序:
img {
position: absolute;
left: 0px;
top: 0px;
z-index: -1;
}

在这里,我们看到具有较大堆栈顺序的元素总是位于具有较低堆栈顺序的元素之上:
<html>
<head>
<style>
.container {
position: relative;
}
.black-box {
position: relative;
z-index: 1;
border: 2px solid black;
height: 100px;
margin: 30px;
}
.gray-box {
position: absolute;
z-index: 3;
background: lightgray;
height: 60px;
width: 70%;
left: 50px;
top: 50px;
}
.green-box {
position: absolute;
z-index: 2;
background: lightgreen;
width: 35%;
left: 270px;
top: -15px;
height: 100px;
}
</style>
</head>
<body>
<div class="container">
<div class="black-box">Black box</div>
<div class="gray-box">Gray box</div>
<div class="green-box">Green box</div>
</div>
</body>
</html>

如果两个定位的元素在没有指定的情况下相互重叠,则 HTML 代码中最后定义的z-index
元素将显示在顶部。
<html>
<head>
<style>
.container {
position: relative;
}
.black-box {
position: relative;
border: 2px solid black;
height: 100px;
margin: 30px;
}
.gray-box {
position: absolute;
background: lightgray;
height: 60px;
width: 70%;
left: 50px;
top: 50px;
}
.green-box {
position: absolute;
background: lightgreen;
width: 35%;
left: 270px;
top: -15px;
height: 100px;
}
</style>
</head>
<body>
<div class="container">
<div class="black-box">Black box</div>
<div class="gray-box">Gray box</div>
<div class="green-box">Green box</div>
</div>
</body>
</html>

网友评论