定位

138 阅读1分钟
<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;
        }
        .div1{
            width: 200px;
            height: 200px;
            background-color: red;
            position: absolute;
            top: 200px;
            left: 200px;
        }
        .child{
            position:absolute;
            width: 90px;
            height: 90px;
            background-color: blue;
            left: 10px;
            top: 10px;
        }
        /* absolute绝对定位 */
        /* 设置绝对定位之后,前面的会被后面的盖住 */
        /* 因为设置了绝对定位后就脱离了标准的文档流 */
        /* .div2{
            width: 200px;
            height: 200px;
            background-color:palevioletred;
            position: absolute;
        } */

    </style>
</head>
<body>
    <div class="div1">
        <!-- 内部是绝对定位,父盒子也是绝对定位,就会以父盒子为基准进行定位 -->
        <!-- div1的父盒子不是绝对定位,就会以body为基准进行定位 -->
        <!-- 如果div1不是绝对定位,那么child会以body为基准定位,div1本身也已body为基准 -->
        <div class="child">

        </div>
    </div>
    <!-- <div class="div2"></div> -->
</body>
</html>