【css-绝对定位的布局与元素的层级】


一、绝对定位的布局

-当我们开启了绝对定位后:

        水平方向的布局等式就需要添加left 和 right两个值

水平布局的公式:

        left+margin-left+border-left+padding-left+width+padding-right+border-right+margin-right+right=包含块的内容区的宽度

        此时规则和之前一样,只是多添加了两个值

                当发生过度约束:

                如果9个值都没有auto 则自动调整right值以使等式满足

                如果有auto,则自动调整auto的值以使等式满足

        -可以设置auto的值

        margin-left/right width left right

        -当left和right的值不设置时,那么left和right的值默认为auto

        所以如果不设置left和right 则等式不满足时,会自动调整这两个值

垂直布局的公式:

        top+margin-top+border-top+padding-top+height+padding-bottom+border-bottom+margin-bottom=包含块的高度

<!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>绝对定位元素的布局</title>
    <link rel="stylesheet" href="./reset.css">
    <style>
        .box1{
            width: 400px;
            height:400px;
            background-color: orange;
       /* 开启了相对定位,因为box1是box2的最近的开启定位的祖先元素,所有box2是相对于box1定位的 */
            position: relative;
        }

        .box2{
            width: 100px;
            height: 100px;
            background-color: #bfa;

            /* 开启了绝对定位 */    
            position: absolute;
            left: 0;
            right:0;
            top: 0;
            bottom: 0;
            margin: auto;
            /* 如果 left:0;right:0;top:0;bottom:0;
               开启了绝对定位的元素 设置 margin:auto,那么则会使元素在包含块居中
            */
    </style>
</head>
<body>
    <div class="box1">
        <div class="box2"></div>
    </div>
</body>
</html>

 

 

二、定位元素

 对于开启了定位元素,可以通过z-index属性来指定元素的层级

        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>元素的层级</title>
    <style>
        body{
            font-size: 40px;
        }

        .box1{
            width: 300px;
            height: 300px;
            background-color: #bfa;
            position: absolute;
            z-index: 4;
        }
        .box2{
            width: 300px;
            height: 300px;
            background-color: orange;
            position: absolute;
            top: 100px;
            left: 100px;
            z-index: 4;
            /* 假设此时box1和box2的层级是最高的,会优先显示靠下的元素 */
        }

        .box3{
            width: 300px;
            height: 300px;
            background-color: chocolate;
            position: absolute;
            top: 200px;
            left: 200px;
            z-index: 16;
            /* 此时box3的元素层级最高,所有应该是box4显示在最上边 */
        }
        .box4{
            width: 100px;
            height: 100px;
            background-color: firebrick;
            position: absolute;
     
        }
    </style>
</head>
<body>
    <div class="box1"></div>
    <div class="box2"></div>
    <div class="box3">
        <div class="box4"></div>
    </div>
</body>
</html>

 此文件出自跟随尚硅谷老师学习自打的,仅用于自我学习

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值