前端学习笔记57-元素的层级

前端学习笔记57-元素的层级

兄弟元素

看代码

<!DOCTYPE html>
<html lang="en">
<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>Document</title>
    <style>
        .box1{
            width: 100px;
            height: 100px;
            background-color: #bfa;
            position: absolute;
        }
        .box2{
            width: 100px;
            height: 100px;
            background-color: orange;
            position: absolute;
            top: 50px;
            left: 50px;
        }
        .box3{
            width: 100px;
            height: 100px;
            background-color: yellow;
            position: absolute;
            top: 100px;
            left: 100px;
        }
    </style>
</head>
<body>
    <div class="box1">1</div>
    <div class="box2">2</div>
    <div class="box3">3</div>
 
</body>
</html>

在这里插入图片描述
可以看到,box3的层级最高。
如果想设置层级,则需要用z-index来设置层级,值越大,层级越高。
给box1设置1。

        .box1{
            width: 100px;
            height: 100px;
            background-color: #bfa;
            position: absolute;
            z-index:1;
        }
        .box2{
            width: 100px;
            height: 100px;
            background-color: orange;
            position: absolute;
            top: 40px;
            left: 40px;
        }
        .box3{
            width: 100px;
            height: 100px;
            background-color: yellow;
            position: absolute;
            top: 60px;
            left: 60px;
        }

在这里插入图片描述
如果想把box2盖在box1前面,则可以给box2设置z-index:2。

如果设置的值相同,则在body中靠下的元素层级越高。

祖先元素

给box3添加一个子元素,并把box3的z-index设置最大。

<!DOCTYPE html>
<html lang="en">
<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>Document</title>
    <style>

        .box1{
            width: 100px;
            height: 100px;
            background-color: #bfa;
            position: absolute;
        }
        .box2{
            width: 100px;
            height: 100px;
            background-color: orange;
            position: absolute;
            top: 40px;
            left: 40px;
        }
        .box3{
            width: 100px;
            height: 100px;
            background-color: yellow;
            position: absolute;
            top: 60px;
            left: 60px;
            z-index: 2;
        }
        .box4{
            width: 50px;
            height: 50px;
            background-color: violet;
            position: absolute;
            z-index: 1;
        }
    </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>

在这里插入图片描述
box3并没有盖住box4。
所以祖先元素不会盖住后代元素。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小欣CZX

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值