让盒子居中的方法

96 阅读1分钟

用flex弹性布局让盒子居中

<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 {
            margin: 100px auto;
            width: 500px;
            height: 500px;
            background-color: aqua;
            /* 开启弹性布局写在 父元素上 */
            display: flex;
            /* 让盒子按主轴x轴居中 */
            justify-content: center;
            /* 让盒子按侧轴y轴居中 */
            align-items: center;
        }

        .box1 {
            width: 200px;
            height: 200px;
            background-color: blue;
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="box1"></div>
    </div>
</body>
</html>

用translate让盒子居中

<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>
        .box {
            /* 给父盒子添加相对定位 */
            position: relative;
            width: 500px;
            height: 500px;
            background-color: aqua;
        }
        .box1 {
            /* 给子盒子添加绝对定位 */
            position: absolute;
            width: 200px;
            height: 200px;
            background-color: blue;
            /* 整体向右移和下移,父盒子的50%。 */
            left: 50%;
            top: 50%;
            /* 向左和上移动自己宽度的50% */
            transform: translate(-50%,-50%);
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="box1"></div>
    </div>
</body>
</html>

用margin让盒子居中

<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>
        .box {
            /* 添加相对定位 */
            position: relative;
            margin: 0 auto;
            width: 600px;
            height: 600px;
            background-color: cadetblue;
        }
        .box1 {
            /* 添加绝对定位 */
            position: absolute;
            /* 向右移动整体的50%,并且回退自身宽度的50% */
            right: 50%;
            margin-right: -100px;
            top: 50%;
            /* 向上移动整体的50%,并且回退自身宽度的50% */
            margin-top: -100px;
            width: 200px;
            height: 200px;
            background-color: chartreuse;

        }
    </style>
</head>
<body>
    <div class="box">
        <div class="box1"></div>
    </div>
</body>
</html>

用margin让盒子居中的第二种方法

<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>
        .box {
            /* 添加相对定位 */
            position: relative;
            margin: 0 auto;
            width: 600px;
            height: 600px;
            background-color: cadetblue;
        }
        .box1 {
            /* 添加绝对定位 */
            position: absolute;
            /* 让盒子在上下左右,都为0 */
            top: 0;
            bottom: 0;
            left: 0;
            right: 0;
            width: 200px;
            height: 200px;
            /* 让盒子上下左右居中 */
            margin: auto;
            background-color: chartreuse;

        }
    </style>
</head>
<body>
    <div class="box">
        <div class="box1"></div>
    </div>
</body>
</html>