CSS—定位

一、定位的叠放顺序

控制盒子的叠放顺序:

选择器 {
    z-index: 1;
    }

<!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>
        .box {
            position: absolute;
            top: 0;
            left: 0;
            width: 200px;
            height: 200px;
        }
        .one {
            background-color: aqua;
            z-index: 1;
        }
        .two {
            background-color: rgb(62, 32, 235);
            left: 50px;
            top: 50px;
        }
        .three {
            background-color: rgb(224, 22, 49);
            left: 100px;
            top: 100px;
        }
    </style>
</head>
<body>
    <div class="box one">one</div>
    <div class="box two">two</div>
    <div class="box three">three</div>
</body>
</html>

注意: 

  1. 数值可以是正整数、负数或0,默认值是auto,数值越大盒子越靠上
  2. 如果属性值相同,则按书写顺序后来居上
  3. 数字后面不能加单位
  4. 只有定位的盒子才有这个属性

二、绝对定位居中

<!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>
        .box {
            position: absolute;
            /* 1.left 50% */
            left: 50%;
            /* margin负值往左盒子一半 */
            margin-left: -100px;
            top: 50%;
            margin-top: -50px;
            width: 200px;
            height: 200px;
            background-color: aqua;
            /* margin: auto; */
            /* 绝对定位的盒子不能通过margin实现居中 */
        }
    </style>
</head>
<body>
    <div class="box"></div>
</body>
</html>

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值