div设置垂直水平居中的几种方法

html样式

        --------children相对于box水平垂直居中

        <div class="box">

                <div class="chidren></div>

        </div>

css样式

方法一:

    .box {

                width:500px;

                height: 500px;

                border:3px solid pink;

                position: relative;

       }

.children {

            width:100px;

            height: 100px;

            background-color: skyblue;

            position: absolute;

            left:50%;

            top:50%;

            transform:translate(-50%,-50%);

      }

方法二:

    .box {

            width:500px;

            height: 500px;

            border:3px solid pink;

            position: relative;

    }

    .children {

            width:100px;

            height: 100px;

            background-color: skyblue;

            position: absolute;

            left:0;

            top:0;

            right:0;

            bottom:0;

            margin: auto;

}

方法三:

    .box {

            width:500px;

            height: 500px;

            border:3px solid pink;

            display: flex;

            align-items: center;

            justify-content: center;

    }

    .children {

            width:100px;

            height: 100px;

            background-color: skyblue;

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容