57元素的层级

注释:Z-index 仅能在定位元素上奏效(例如 position:absolute;)!
视频里面没说这点,…
所以如果想要使用z-index,必须先开启元素的定位才行

<!DOCTYPE html>
<html lang="ch">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>元素的层级</title>
    <style>
        /* 
            通过z-index去设置元素的层级
                元素的层级的意思就是相当于三位直角坐标系中的z轴
                也可以用蛋糕的层数去理解,z-index越大,相当于蛋糕的越高层,这样,我们俯瞰蛋糕的时候,就优先看到的是高层

                如果元素的层级一样,就优先显示“结构”上靠下的元素

                祖先元素的层级再高也不会盖住后代元素
         */
        div {
            font-size:70px;
        }
        .box1, .box2, .box3 {
            width: 200px;
            height: 200px;
        }
        .box1 {
            background-color: red;
            position: absolute;
        }
        .box2 {
            background-color: orange;
            position: absolute;
            top: 50px;
            left: 50px;
            z-index: 1;
        }
        .box3 {
            background-color: #bfa;
            position: absolute;
            top: 100px;
            left: 100px;
        }
        .box4 {
            width: 80px;
            height: 80px;
            background-color: yellowgreen;
        }
    </style>
</head>
<body>
    <div class="box1">1</div>
    <div class="box2">2</div>
    <div class="box3">
        3
        <div class="box4">4</div>
    </div>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值