定位的使用

  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>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        div {
            width: 300px;
            height: 300px;
        }

        .red {
            background-color: red;
        }

        .blue {
            /* 定位方式 */
            /* 静态定位 */
            position: static;
            /* 偏移值 */
            left: 10px;
            top: 10px;
            background-color: blue;
        }

        /* 
        特点
        静态定位就是之前的标准流,不能通过方位属性来移动位置
        */
    </style>
</head>

<body>
    <div class="red"></div>
    <div class="blue"></div>
</body>

</html>
  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>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        div {
            width: 300px;
            height: 300px;
        }

        .red {
            background-color: red;
        }

        .green {
            /* 相对定位 */
            position: relative;
            left: 20px;
            top: 40px;
            background-color: green;
        }

        .blue {
            background-color: blue;
        }

        /*  
            特点
            1 需要配合方位属性来移动位置
            2 相对于自己原来的位置进行移动
            3 没有脱标
        */
    </style>
</head>

<body>
    <div class="red"></div>
    <div class="green"></div>
    <div class="blue"></div>
</body>

</html>
  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>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        div {
            width: 300px;
            height: 300px;
        }

        .red {
            background-color: red;
        }

        .green {
            /* 绝对定位 */
            position: absolute;
            left: 150px;
            top: 150px;
            background-color: green;
        }

        .blue {
            background-color: blue;
        }

        /*  
            特点
            1 需要配合方位属性实现移动
            2 默认相对于浏览器的可视区域进行移动位置
            3 不占位置 脱标
        */
    </style>
</head>

<body>
    <div class="red"></div>
    <div class="green"></div>
    <div class="blue"></div>
</body>

</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值