注释:Z-index 仅能在定位元素上奏效(例如 position:absolute;)!
视频里面没说这点,…
所以如果想要使用z-index,必须先开启元素的定位才行
<!DOCTYPE html>
<html lang="ch">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>元素的层级</title>
<style>
/*
通过z-index去设置元素的层级
元素的层级的意思就是相当于三位直角坐标系中的z轴
也可以用蛋糕的层数去理解,z-index越大,相当于蛋糕的越高层,这样,我们俯瞰蛋糕的时候,就优先看到的是高层
如果元素的层级一样,就优先显示“结构”上靠下的元素
祖先元素的层级再高也不会盖住后代元素
*/
div {
font-size:70px;
}
.box1, .box2, .box3 {
width: 200px;
height: 200px;
}
.box1 {
background-color: red;
position: absolute;
}
.box2 {
background-color: orange;
position: absolute;
top: 50px;
left: 50px;
z-index: 1;
}
.box3 {
background-color: #bfa;
position: absolute;
top: 100px;
left: 100px;
}
.box4 {
width: 80px;
height: 80px;
background-color: yellowgreen;
}
</style>
</head>
<body>
<div class="box1">1</div>
<div class="box2">2</div>
<div class="box3">
3
<div class="box4">4</div>
</div>
</body>
</html>