堆叠上下文

堆叠上下文

堆叠上下文(stack context),它是一块区域,这块区域由某个元素创建,它规定了该区域中的内容在Z轴上排列的先后顺序。

创建堆叠上下文的元素

  1. html元素(根元素)
  2. 设置了z-index(非auto)数值的定位元素

同一个堆叠上下文中元素在Z轴上的排列

从后到前的排列顺序:

  1. 创建堆叠上下文的元素的背景和边框
  2. 堆叠级别(z-index, stack level)为负值的堆叠上下文
  3. 常规流非定位的块盒
  4. 非定位的浮动盒子
  5. 常规流非定位行盒
  6. 任何 z-index 是 auto 的定位子元素,以及 z-index 是 0 的堆叠上下文
  7. 堆叠级别为正值的堆叠上下文

每个堆叠上下文,独立于其他堆叠上下文,它们之间不能相互穿插。
测试:

    <style>
        .container{
            background: #008c8c;
            width: 200px;
            height: 300px;
            position: relative;
            z-index: 0;
        }
        .item{
            width: 100px;
            height: 100px;
            background: red;
            z-index: -1;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="item">

        </div>
    </div>
</body>

效果, 创建堆叠上下文的元素的背景和边框永远在最后
在这里插入图片描述
测试2:

    <style>
        html{
            background: lightblue;
        }
        .container{
            background: #008c8c;
            width: 200px;
            height: 200px;
            position: relative;
            margin: 50px;
        }

        .item{
            width: 100px;
            height: 100px;
            background: red;
            z-index: -1;
            position: absolute;
            left: -30px;
            top: -30px;
        }
        .normal{
            width: 200px;
            height: 200px;
            background: chocolate;
            margin-top: -200px;
        }
    </style>
</head>
<body>
    <p style="background: lightyellow">
        Lorem ipsum dolor sit amet, consectetur adipisicing elit. Atque suscipit ullam nam corporis at placeat, tempore tenetur? Impedit, aliquid provident officiis aspernatur ea pariatur optio! Laudantium dolor voluptatibus explicabo quae?
    </p>
    <div class="container">
        <div class="item"></div>
    </div>
    <!-- <div class="float"></div> -->
    <div class="normal">
    </div>
   
</body>

效果;
在这里插入图片描述
测试三:

<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: chocolate;
            left: 100px;
            top: 100px;
        }
        .item1{
            position: absolute;
            width: 100px;
            height: 100px;
        }
        .item1{
            right: -50px;
            bottom: -50px;
        }
    </style>
</head>

<body>
    <div class="c1">
        c1
        <div class="item1" style="background: red; z-index: -9999;">item1</div>
     
    </div>
    <div class="c2">
        c2
        <div class="item1" style="background:green ; z-index: 9999;">item2</div>
  
    </div>
</body>

效果;(每个堆叠上下文,独立于其他堆叠上下文,它们之间不能相互穿插。)在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值