堆叠上下文

堆叠上下文

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

创建堆叠上下文的标签

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

同一个堆叠上下文中标签在z轴上的排列

从后(远离用户)到前(靠近用户)的排列顺序:

1.创建堆叠上下文的标签的背景和边框

<style>
        html{
            background: lightblue;
            /* 背景永远是最后的 */
        }
        .container{
            background: lightgreen;
            width: 200px;
            height: 200px;
            position: relative;
            z-index: 0;
            margin: 50px;
        }
        .item{
            width: 100px;
            height: 100px;
            background: red;
            z-index: -1;
            position: absolute;
            left: -30px;
            top: -30px;
        }
        /* Z-index 仅能在定位元素上奏效(例如 position:absolute;) */
    </style>
</head>
<body>
    <div class="container">
      <div class="item"></div>
    </div>
</body>
</html>

在这里插入图片描述

2.堆叠级别(z-index)为负值的堆叠上下文

    <style>
        html{
            background: lightblue;
            /* 背景永远是最后的 */
        }
        .container{
            background: lightgreen;
            width: 200px;
            height: 200px;
            position: relative;
            z-index: 0;
            margin: 50px;
        }
        .item{
            width: 100px;
            height: 100px;
            background: red;
            z-index: -1;
            position: absolute;
            left: -30px;
            top: -30px;
        }
        /* Z-index 仅能在定位元素上奏效(例如 position:absolute;) */
    </style>
</head>
<body>
    <div class="container">
      <div class="item"></div>
      <div class="item" style="background: magenta;z-index: -2;   left: -40px;top: -40px;"></div>
      <!-- 处于同一个堆叠上下文中,后面的覆盖前面的,同时负数的情况,值越大,越靠前 -->
    </div>
</body>
</html>

在这里插入图片描述

3.常规流非定位的块盒

<style>
        html{
            background: lightblue;
            /* 背景永远是最后的 */
        }
        .container{
            background: lightgreen;
            width: 200px;
            height: 200px;
            position: relative;
            z-index: 0;
            margin: 50px;
        }
        .item{
            width: 100px;
            height: 100px;
            background: red;
            z-index: -1;
            position: absolute;
            left: -30px;
            top: -30px;
        }
        /* Z-index 仅能在定位元素上奏效(例如 position:absolute;) */
        .normal{
            width: 200px;
            height: 200px;
            background: chocolate;
            margin-top: -75px;
        }
    </style>
</head>
<body>
    <div class="container">
      <div class="item"></div>
      <div class="item" style="background: magenta;z-index: -2;   left: -40px;top: -40px;"></div>
      <!-- 处于同一个堆叠上下文中,后面的覆盖前面的,同时负数的情况,值越大,越靠前 -->
    </div>
    <div class="normal"></div>常规流非定位盒子
</body>
</html>

在这里插入图片描述

4.非定位的浮动盒子

   <style>
        html{
            background: lightblue;
            /* 背景永远是最后的 */
        }
        .container{
            background: lightgreen;
            width: 200px;
            height: 200px;
            position: relative;
            z-index: 0;
            margin: 50px;
        }
        .item{
            width: 100px;
            height: 100px;
            background: red;
            z-index: -1;
            position: absolute;
            left: -30px;
            top: -30px;
        }
        /* Z-index 仅能在定位元素上奏效(例如 position:absolute;) */
        .normal{
            width: 200px;
            height: 200px;
            background: chocolate;
            margin-top: -75px;
        }
        .float{
            width: 200px;
            height: 200px;
            float: left;
            margin-left: 100px;
            margin-top: -250px;
            background: darkblue;
        }
    </style>
</head>
<body>
    <div class="container">
      <div class="item"></div>
      <div class="item" style="background: magenta;z-index: -2;   left: -40px;top: -40px;"></div>
      <!-- 处于同一个堆叠上下文中,后面的覆盖前面的,同时负数的情况,值越大,越靠前 -->
    </div>
    <div class="normal"></div>
    <div class="float"></div>
</body>
</html>

在这里插入图片描述

5.常规流非定位的行盒

覆盖不了span标签,如果p标签里面有文字,可以覆盖p标签,但覆盖不了文字,因为会形成匿名行盒。

6.任何z-index是auto的定位子标签,以及z-index是0的堆叠上下文

7.堆叠级别为正值的堆叠上下文

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

    <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: #d63f3f;
            left: 100px;
            top: 100PX;
        }
        .item1,.item2{
            position: absolute;
            width: 100px;
            height: 100px;
        }
        .item1{
            right: -50px;
            bottom: -50px;
           
        }
    </style>
</head>
<body>
    <div class="c1">
        <div class="item1" style="background: red;"></div>
        <div class="item2"></div>
    </div>
    <!-- 整体 -->
    <div class="c2">
        <div class="item1" style="background: magenta;"></div>
        <div class="item2"></div>
    </div>
    <!-- 整体,无论如何调两个item1的z-index都是红色覆盖粉色 -->
</body>
</html>

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值