CSS实现水平垂直居中

<style>
        /* 1、绝对定位+负margin值  */
        .box {
            /* 子绝父相 */
            position: relative;
            width: 200px;
            height: 200px;
            border: 1px solid red;
        }

        .childrenbox {
            position: absolute;
            width: 100px;
            height: 100px;
            background-color: yellow;
            /* 先向下、向右移动父盒子高度和宽度的一半 */
            top: 50%;
            left: 50%;
            /* 向上移动子盒子高度的一半;向左移动子盒子宽度的一半*/
            margin-left: -50px;
            margin-top: -50px;
        }

        /* 2、绝对定位+transform */
        .box {
            /* 子绝父相 */
            position: relative;
            width: 200px;
            height: 200px;
            border: 1px solid red;
        }

        .childrenbox {
            position: absolute;
            width: 100px;
            height: 100px;
            background-color: yellow;
            /* 先向下、向右移动父盒子高度和宽度的一半 */
            top: 50%;
            left: 50%;
            /* 向上移动子盒子高度的一半;向左移动子盒子宽度的一半*/
            transform: translate(-50%, -50%);
        }

        /* 3、绝对定位+left/right/bottom/top+margin */
        .box {
            /* 子绝父相 */
            position: relative;
            width: 200px;
            height: 200px;
            border: 1px solid red;
        }

        .childrenbox {
            position: absolute;
            display: inline;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0px;
            background: yellow;
            margin: auto;
            height: 100px;
            width: 100px;
        }

        /* 4、flex布局 */
        .box {
            display: flex;
            width: 200px;
            height: 200px;
            border: 1px solid red;
            justify-content: center;
            align-items: center;
        }

        .childrenbox {
            width: 100px;
            height: 100px;
            background: yellow;
        }

        /* 5、grid布局 */
        .box {
            display: grid;
            width: 200px;
            height: 200px;
            border: 1px solid red;
        }

        .childrenbox {
            width: 100px;
            height: 100px;
            background: yellow;
            margin: auto;
        }

        /* 6、table-cell+vertical-align+inline-block/margin:auto */
        .box {
            display: table-cell;
            width: 200px;
            height: 200px;
            border: 1px solid red;
            text-align: center;
            vertical-align: middle;
        }

        .childrenbox {
            /* 可以换成margin:auto; */
            display: inline-block;
            width: 100px;
            height: 100px;
            background: yellow;
        }
    </style>
    <body>
    <div class="box">
        <div class="childrenbox"></div>
    </div>
</body>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值