层叠上下文(z-index)

一、概述

在日常code中,是否遇到过关于层级的问题,下面梳理下各种情况

二、首先来一张著名的7阶层叠水平图


换言之,除了z-index,display:inline-block,float,block也是可以控制元素层级显示的,ps:当元素有内容的时候,内容的层级会更高。

三、代码实现

首先是dom结构和样式代码,这里body当做是background来看

<body>
	<div class="disblock">display:block</div>
    <div class="zindex-1">z-index:-1</div>
    <div class="float">float</div>
    <div class="disin-block">display:inline-block</div>
    <div class="zindex-0">z-index:0</div>
    <div class="zindex1">正z-index</div>
</body>
<style>
    body{
        background: lightblue;
        color: #fff;
        font-size: 16px;
    }
    .disblock{
        position: relative;
        left: 100px;
        top: 100px;
        width: 500px;
        height: 500px;
        background: hotpink;
    }
    .zindex-1{
        position: relative;
        top: -500px;
        left: 0;
        width: 600px;
        height: 600px;
        background: purple;
        z-index: -1;
    }
    .float{
        position: relative;
        top: -900px;
        left: 200px;
        float: left;
        width: 400px;
        height: 400px;
        background: green;
    }
	.mydiv{
		float: left;
		width: 200px;
		height: 200px;
		background: #fff;
	}
	.mywrap{
		width: 500px;
		height: 500px;
		background: #000;
	}
    .disin-block{
        position: relative;
        top: -800px;
        left: -100px;
        display: inline-block;
        width: 300px;
        height: 300px;
        background: lightgreen;
    }
    .zindex-0{
        position: relative;
        width: 200px;
        height: 200px;
        top: -1000px;
        left: 400px;
        z-index: 0;
        color: #000;
        background: yellow;
    }
    .zindex1{
        position: relative;
        top: -1100px;
        left: 500px;
        width: 100px;
        height: 100px;
        z-index: 1;
        background: red;
    }
</style>
然后是效果图


四、注意事项

1、z-index的值的比较只能在同个层级上下文环境中,不同父元素的子节点的z-index的值是不能比较的;

2、开发中尽量避免层叠上下文的多层嵌套。避免使用太多的z-index值。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
z-index是CSS属性之一,用于控制元素在层叠上下文中的显示顺序。未设置z-index属性的元素会采用默认值0,而设置了z-index属性的元素可以通过属性值的大小来确定其层级关系。 当设置了z-index属性的元素的属性值大于0时,该元素的层级会高于未设置z-index属性的元素。这意味着具有较高属性值的元素会覆盖在较低属性值的元素之上。 然而,z-index受限于层叠上下文的影响。即使一个元素具有较高的z-index属性值,但如果它所属的层叠上下文z-index值小于其他元素所属层叠上下文z-index值,那么它仍然会处于下方。因此,层叠上下文z-index值比元素自身的z-index值更重要。 另外,当设置了z-index属性的元素的属性值为0时,它与未设置z-index属性的元素层级相同,遵循后面的元素覆盖前面元素的规则。 总结来说,z-index属性用于控制元素在层叠上下文中的层级关系,较高的属性值会覆盖较低的属性值,但受限于层叠上下文的影响。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [CSS 之 z-index 属性详解](https://blog.csdn.net/weixin_45092437/article/details/126493240)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [CSS深入理解z-indexz-index相关知识总结)](https://blog.csdn.net/qq_41000891/article/details/110005863)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值