z-index层级关系分析

定义和用法

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

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

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

说明

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

优先关系

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

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>z-index</title>
 6 </head>
 7 <style type="text/css">
 8     .first{width:200px; height: 500px; position: absolute; background:#00f; z-index: 100;}
 9     .second{width:250px; height: 250px; position: absolute; background:#f33; z-index: 100;}
10 </style>
11 <body>
12     <div class="first">first</div>
13     <div class="second">second</div>
14 </body>
15 </html>

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

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

 1 <style type="text/css">
 2     .first{width:200px; height: 500px; position: absolute; top: 0; background:#00f; z-index: 80;}
 3     .second{width:250px; height: 250px; position: absolute; top: 50px; background:#f33; z-index: 100;}
 4     .first-child{width:250px; height: 250px; position: absolute; top: 0; background:#333; z-index: 101;}
 5 </style>
 6 <body>
 7     <div class="first">first
 8         <div class="first-child">first-child
 9         </div>
10     </div>
11     <div class="second">second</div>
12 </body>

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

转载于:https://www.cnblogs.com/janewin-na/p/4442013.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值