1. z-index
z-index 属性用于设置元素的叠放层次,属性值可以为一个整数,整数值越大越优先显示
注意:z-index只对开启了定位的元素有效
示例如下:
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
div {
width: 200px;
height: 200px;
}
.box1 {
background-color: #c7edcc;
position: relative;
left: 50px;
top: 100px;
z-index: -2;
}
.box2 {
background-color: #fde6e0;
position: absolute;
left: 150px;
top: 150px;
z-index: -1;
}
.box3 {
background-color: #dce2f1;
float: left;
}
.box4 {
background-color: red;
position: fixed;
left: 125px;
top: 225px;
}
</head>
<body>
<div class="box1">相对定位</div>
<div class="box2">绝对定位</div>
<div class="box3">浮动</div>
<div class="box4">固定定位</div>
</body>
</html>