margin负——块元素垂直、水平居中

    <img src="https://img2020.cnblogs.com/blog/2216408/202012/2216408-20201213111053901-2074406338.png" width="50%" height="50%">
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>垂直居中</title>
    <style>
        .boxBig {
            /* 子绝父相 */
            position: relative;
            width: 600px;
            height: 700px;
            background-color: gray;
        }
        
        .box {
            position: absolute;
            /* 水平居中 2行——自己width的一半*/
            left: 50%;
            margin-left: -150px;
            /* 垂直居中 2行——自己height的一半*/
            top: 50%;
            margin-top: -150px;
            width: 300px;
            height: 300px;
            background-color: pink;
        }
    </style>
</head>

<body>
    <div class="boxBig">大盒子
        <div class="box">盒子</div>
    </div>
</body>

</html>
posted @ 2020-12-13 11:14  ice猫猫3  阅读(130)  评论(0编辑  收藏  举报