CSS定位的叠放次序z-index,简单介绍

在使用定位布局的时候,可能出现盒子重叠现象,此时可以使用 z-index 来控制盒子的前后次序。

语法:

选择器 {z-index:1}

  • 数值可以为负数,数值越大,越靠上,越靠近用户。 
  • 如果值相同,则按照书写顺序,后来者居上。
  • 只有定位的盒子才有 z-index 属性。
<head>
  <style>
    .parent{
      position: relative;
      width: 300px;
      height: 300px;
      border: 1px solid #4c4c4c;
    }
    .box1,.box2,.box3 {
      position: absolute;
      width: 100px;
      height: 100px;
    }

    .box1 {
      background-color: hotpink;
    }
    .box2 {
      background-color: khaki;
    }

    .box3 {
      background-color: lightcoral;
    }
  </style>
</head>
<body>
  <div class="parent">
    <div class="box1">1</div>
    <div class="box2">2</div>
    <div class="box3">3</div>
  </div>
</body>

结果:box3在最上面。(默认情况下,z-index值为auto,多个同等级的按加载顺序显示)

现将box2样式里添加,z-index:1,结果

层叠水平:

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值