32、元素的层级 z-index

2022-06-21  本文已影响0人  小黄不头秃

对于开启了定位的元素,可以通过z-index属性来指定元素之间的层级关系
z-index需要一个整数作为参数,值越大层级越高
如果没有设置z-index,系统自动选择显示靠下的元素
祖先的元素层级再高也不能盖住后代元素

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>元素的层级</title>
    <style>
        /*
        对于开启了定位的元素,可以通过z-index属性来指定元素之间的层级关系
         z-index需要一个整数作为参数,值越大层级越高

         如果没有设置z-index,统计时间系统自动选择显示靠下的元素

         祖先的元素层级再高也不能盖住后代元素
         */
        body{
            font-size: 60px;
        }
        .box1{
            width: 100px;
            height: 100px;
            background-color: rgba(255, 0, 0, .8);
            position: relative;
            z-index: 999;
        }
        .box2{
            width: 200px;
            height: 200px;
            background-color: green;
            position: absolute;
            top: 50px;
            left: 50px;
        }
        .box3{
            width: 300px;
            height: 300px;
            background-color: blue;
            position: relative;
            top: 50px;
            left: 100px;
        }
        .box4{
            width: 100px;
            height: 100px;
            background-color: orange ;
        }
    </style>
</head>
<body>
    <div class="box1">1</div>
    <div class="box2"> 2   <div class="box4">4</div></div>
    <div class="box3">3</div>

</body>
</html>
上一篇 下一篇

猜你喜欢

热点阅读