堆叠上下文
堆叠上下文(stack context)(层叠上下文),它是一块区域,这块区域由某个标签创造,它规定了该区域中的内容在z轴上排列的先后顺序。
创建堆叠上下文的标签
1.html标签(根标签)
2.设置了z-index数值的定位标签(非auto值)
同一个堆叠上下文中标签在z轴上的排列
从后(远离用户)到前(靠近用户)的排列顺序:
1.创建堆叠上下文的标签的背景和边框
<style>
html{
background: lightblue;
/* 背景永远是最后的 */
}
.container{
background: lightgreen;
width: 200px;
height: 200px;
position: relative;
z-index: 0;
margin: 50px;
}
.item{
width: 100px;
height: 100px;
background: red;
z-index: -1;
position: absolute;
left: -30px;
top: -30px;
}
/* Z-index 仅能在定位元素上奏效(例如 position:absolute;) */
</style>
</head>
<body>
<div class="container">
<div class="item"></div>
</div>
</body>
</html>
2.堆叠级别(z-index)为负值的堆叠上下文
<style>
html{
background: lightblue;
/* 背景永远是最后的 */
}
.container{
background: lightgreen;
width: 200px;
height: 200px;
position: relative;
z-index: 0;
margin: 50px;
}
.item{
width: 100px;
height: 100px;
background: red;
z-index: -1;
position: absolute;
left: -30px;
top: -30px;
}
/* Z-index 仅能在定位元素上奏效(例如 position:absolute;) */
</style>
</head>
<body>
<div class="container">
<div class="item"></div>
<div class="item" style="background: magenta;z-index: -2; left: -40px;top: -40px;"></div>
<!-- 处于同一个堆叠上下文中,后面的覆盖前面的,同时负数的情况,值越大,越靠前 -->
</div>
</body>
</html>
3.常规流非定位的块盒
<style>
html{
background: lightblue;
/* 背景永远是最后的 */
}
.container{
background: lightgreen;
width: 200px;
height: 200px;
position: relative;
z-index: 0;
margin: 50px;
}
.item{
width: 100px;
height: 100px;
background: red;
z-index: -1;
position: absolute;
left: -30px;
top: -30px;
}
/* Z-index 仅能在定位元素上奏效(例如 position:absolute;) */
.normal{
width: 200px;
height: 200px;
background: chocolate;
margin-top: -75px;
}
</style>
</head>
<body>
<div class="container">
<div class="item"></div>
<div class="item" style="background: magenta;z-index: -2; left: -40px;top: -40px;"></div>
<!-- 处于同一个堆叠上下文中,后面的覆盖前面的,同时负数的情况,值越大,越靠前 -->
</div>
<div class="normal"></div>常规流非定位盒子
</body>
</html>
4.非定位的浮动盒子
<style>
html{
background: lightblue;
/* 背景永远是最后的 */
}
.container{
background: lightgreen;
width: 200px;
height: 200px;
position: relative;
z-index: 0;
margin: 50px;
}
.item{
width: 100px;
height: 100px;
background: red;
z-index: -1;
position: absolute;
left: -30px;
top: -30px;
}
/* Z-index 仅能在定位元素上奏效(例如 position:absolute;) */
.normal{
width: 200px;
height: 200px;
background: chocolate;
margin-top: -75px;
}
.float{
width: 200px;
height: 200px;
float: left;
margin-left: 100px;
margin-top: -250px;
background: darkblue;
}
</style>
</head>
<body>
<div class="container">
<div class="item"></div>
<div class="item" style="background: magenta;z-index: -2; left: -40px;top: -40px;"></div>
<!-- 处于同一个堆叠上下文中,后面的覆盖前面的,同时负数的情况,值越大,越靠前 -->
</div>
<div class="normal"></div>
<div class="float"></div>
</body>
</html>
5.常规流非定位的行盒
覆盖不了span标签,如果p标签里面有文字,可以覆盖p标签,但覆盖不了文字,因为会形成匿名行盒。
6.任何z-index是auto的定位子标签,以及z-index是0的堆叠上下文
7.堆叠级别为正值的堆叠上下文
每个堆叠上下文独立于其他不堆叠上下文,他们之间不能相互穿插。
<style>
html{
background: lightblue;
}
.c1{
position: relative;
z-index: 0;
width: 200px;
height: 200px;
background: #008c8c;
}
.c2{
position: absolute;
z-index: -1;
width: 200px;
height: 200px;
background: #d63f3f;
left: 100px;
top: 100PX;
}
.item1,.item2{
position: absolute;
width: 100px;
height: 100px;
}
.item1{
right: -50px;
bottom: -50px;
}
</style>
</head>
<body>
<div class="c1">
<div class="item1" style="background: red;"></div>
<div class="item2"></div>
</div>
<!-- 整体 -->
<div class="c2">
<div class="item1" style="background: magenta;"></div>
<div class="item2"></div>
</div>
<!-- 整体,无论如何调两个item1的z-index都是红色覆盖粉色 -->
</body>
</html>