css z-index的层级关系

定义和用法

z-index 属性设置元素的堆叠顺序。拥有更高堆叠顺序的元素总是会处于堆叠顺序较低的元素的前面。

注释:元素可拥有负的 z-index 属性值。

注释:Z-index 仅能在定位元素上奏效(例如 position:absolute;)

说明

该属性设置一个定位元素沿 z 轴的位置,z 轴定义为垂直延伸到显示区的轴。如果为正数,则离用户更近,为负数则表示离用户更远。

优先关系

如果同时出现两个相同的z-index值,那么在后面的元素层级会高于前面的元素。例如:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>z-index</title>
</head>
<style type="text/css">
.first{200px; height: 500px; position: absolute; background:#00f; z-index: 100;}
.second{250px; height: 250px; position: absolute; background:#f33; z-index: 100;}
</style>
<body>
<div class="first">first</div>
<div class="second">second</div>
</body>
</html>

类名为second的div会在类名为first的div上面。

z-index属性是区分所有兄弟元素的高度,并不是所有页面元素的高度;例如:

<style type="text/css">
.first{200px; height: 500px; position: absolute; top: 0; background:#00f; z-index: 80;}
.second{250px; height: 250px; position: absolute; top: 50px; background:#f33; z-index: 100;}
.first-child{250px; height: 250px; position: absolute; top: 0; background:#333; z-index: 101;}
</style>
<body>
<div class="first">first
<div class="first-child">first-child
</div>
</div>
<div class="second">second</div>
</body>

类名为first-child的div元素的z-index大于类名为second的div元素的z-index;但页面显示效果first-child位于second下面  

原文地址:https://www.cnblogs.com/ypppt/p/12841398.html