9.5 水平垂直居中

56 阅读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>
        .parent{
            width: 200px;
            height: 200px;
            background-color: red;
            margin: 0 auto;
            /* 设置水平垂直居中 */
            /* 方案一 */
            /* display: flex; */
            /* 更改主轴对齐方式 */
            /* justify-content: center; */
            /* 更改交叉轴对齐方式 */
            /* align-items: center; */

            /* 方案二 2.1 给父元素设置伸缩盒*/
            display: flex;
        }
        .child{
            width: 100px;
            height: 100px;
            background-color: blue;
            /* 2.2给子元素设置margin :auto */
            margin: auto;
        }
    </style>
</head>
<body>
    <div class="parent">
        <div class="child"></div>
    </div>
</body>
</html>