CSS的z-index属性用于控制元素在页面上的堆叠顺序。具有更高z-index值的元素会覆盖具有较低z-index值的元素。这个属性通常用于定位重叠的HTML元素。
z-index值决定,数值越大,元素越靠上。z-index,可以精确控制哪些元素覆盖其他元素。z-index可以帮助实现特定的视觉效果。z-index值的元素之上。z-index值的元素之下。z-index,元素仍然不显示在最上面?z-index值较低。position属性不是relative、absolute或fixed。z-index,元素仍然被其他元素覆盖?z-index值更高。<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Z-Index Example</title>
<style>
.container {
position: relative;
}
.box {
position: absolute;
width: 100px;
height: 100px;
}
.box1 {
background-color: red;
z-index: 1;
}
.box2 {
background-color: blue;
z-index: 2;
}
</style>
</head>
<body>
<div class="container">
<div class="box box1"></div>
<div class="box box2"></div>
</div>
</body>
</html>在这个示例中,蓝色的.box2会显示在红色的.box1之上,因为它的z-index值更高。
通过以上解释和示例,你应该能够更好地理解CSS的z-index属性及其应用。
没有搜到相关的沙龙