z-index的值可以控制定位元素在垂直于显示屏幕方向(z轴)上的堆叠顺序(stack order),值大的元素发生重叠时会在值小的元素上面。
son1:z-index:1
son2:z-index:2
son1在son2下面
<!DOCTYPE html>
<html>
<head>
<title>position</title>
<style>
body{
background-color: green;
}
.parent{
position:relative;
right:-5px;
width: 300px;
height: 300px;
background-color: red;
}
.son1{
z-index:1;
position: absolute;
left:50px;
top:50px;
width: 100px;
height: 100px;
background-color: blue;
}
.son2{
z-index:2;
position: absolute;
left:80px;
top:80px;
width: 100px;
height: 100px;
background-color: yellow;
}
</style>
</head>
<body>
<div class="parent">
<div class="son1"></div>
<div class="son2"></div>
</div>
</body>
</html>