div居中的十二种方法

水平居中,垂直居中,水平垂直居中

水平居中
父元素添加text-align: center;子元素添加 display: inline-block;(文字也会居中)
 <div class="parent">
        <div class="child">
            1
        </div>
 </div>
   .parent {
            width: 400px;
            height: 400px;
            background-color: aqua;
            text-align: center;
        }
    .child {
            width: 200px;
            height: 200px;
            background-color: yellow;
            display: inline-block;
        }
父元素什么都不加;子元素添加 margin: 0 auto;(文字不会居中,若是文字也居中可以在父元素或者子元素添加一个text-align: center;)
 <div class="parent">
        <div class="child">
            2
        </div>
 </div>
   .parent {
            width: 400px;
            height: 400px;
            background-color: aqua;
            /* text-align: center; */
        }
    .child {
            width: 200px;
            height: 200px;
             background-color: green;
              margin: 0 auto;
            /* text-align: center; */
        }
父元素添加相对定位position: relative;子元素添加 绝对定位position: absolute;(文字不会居中,若是文字也居中可以在父元素或者子元素添加一个text-align: center;)
 <div class="parent">
        <div class="child">
            3
        </div>
 </div>
   .parent {
            width: 400px;
            height: 400px;
            background-color: aqua;
            position: relative;
            /* text-align: center; */
        }
    .child {
            width: 200px;
            height: 200px;
            background-color: greenyellow;
            position: absolute;
            left: 50%;
            transform: translateX(-50%);
            /* text-align: center; */
        }
父元素添加display: flex;子元素不添加;(文字不会居中,若是文字也居中可以在父元素或者子元素添加一个text-align: center;)
 <div class="parent">
        <div class="child">
            4
        </div>
 </div>
   .parent {
            width: 400px;
            height: 400px;
            background-color: aqua;
            display: flex;
            justify-content: center;
            /* text-align: center; */
        }
    .child {
            width: 200px;
            height: 200px;
            background-color: wheat;
            /* text-align: center; */
        }
垂直居中
父元素添加display: table-cell;子元素不添加(文字不会居中,若是文字也居中可以在父元素或者子元素添加一个text-align: center;)
 <div class="parent">
        <div class="child">
            1
        </div>
 </div>
   .parent {
            width: 400px;
            height: 400px;
            background-color: darkcyan;
            display: table-cell;
            vertical-align: middle;
            /* text-align: center; */
        }
    .child {
            width: 200px;
            height: 200px;
            background-color: aquamarine;
            /* text-align: center; */
        }
父元素添加相对定位position: relative;子元素添加 绝对定位position: absolute;(文字不会居中,若是文字也居中可以在父元素或者子元素添加一个text-align: center;)
 <div class="parent">
        <div class="child">
            2
        </div>
 </div>
   .parent {
            width: 400px;
            height: 400px;
            background-color: darkcyan;
            position: relative;
            /* text-align: center; */
        }
    .child {
            width: 200px;
            height: 200px;
            background-color: darkgreen;
            position: absolute;
            top: 50%;
            transform: translateY(-50%);
            /* text-align: center; */
        }
父元素添加display: flex;子元素不添加;(文字不会居中,若是文字也居中可以在父元素或者子元素添加一个text-align: center;)
 <div class="parent">
        <div class="child">
            3
        </div>
 </div>
   .parent {
            width: 400px;
            height: 400px;
            background-color: darkcyan;
            display: flex;
            align-items: center;
            /* text-align: center; */
        }
    .child {
            width: 200px;
            height: 200px;
            background-color: blueviolet;
            /* text-align: center; */
        }
水平垂直居中
父元素添加display: table-cell;子元素添加 display: inline-block;(文字也会居中)
 <div class="parent">
        <div class="child">
            1
        </div>
 </div>
   .parent {
            width: 400px;
            height: 400px;
            background-color: pink;
            text-align: center;/*必须加,不加就成了垂直居中*/
            display: table-cell;
            vertical-align: middle;
        }
    .child {
            width: 200px;
            height: 200px;
            background-color: indianred;
            display: inline-block;
        }
父元素添加相对定位position: relative;子元素添加 绝对定位position: absolute;(文字不会居中,若是文字也居中可以在父元素或者子元素添加一个text-align: center;)
 <div class="parent">
        <div class="child">
            2
        </div>
 </div>
   .parent {
            width: 400px;
            height: 400px;
            background-color: pink;
            position: relative;
            /* text-align: center; */
        }
    .child {
            width: 200px;
            height: 200px;
            background-color: khaki;
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
            /* text-align: center; */
        }
父元素添加相对定位position: relative;子元素添加 绝对定位position: absolute;(文字不会居中,若是文字也居中可以在父元素或者子元素添加一个text-align: center;)
 <div class="parent">
        <div class="child">
            4
        </div>
 </div>
   .parent {
            width: 400px;
            height: 400px;
            background-color: pink;
            position: relative;
            /* text-align: center; */
        }
    .child {
            width: 200px;
            height: 200px;
            background-color: coral;
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            margin: auto;
            /* text-align: center; */
        }
父元素添加display: flex;子元素不添加;(文字不会居中,若是文字也居中可以在父元素或者子元素添加一个text-align: center;)
 <div class="parent">
        <div class="child">
            3
        </div>
 </div>
   .parent {
            width: 400px;
            height: 400px;
            background-color: pink;
            display: flex;
            justify-content: center;
            align-items: center;
            /* text-align: center; */
        }
    .child {
            width: 200px;
            height: 200px;
            background-color: lightgreen;
            /* text-align: center; */
        }
父元素添加display: flex;子元素添加margin: auto;(文字不会居中,若是文字也居中可以在父元素或者子元素添加一个text-align: center;)
 <div class="parent">
        <div class="child">
            5
        </div>
 </div>
   .parent {
            width: 400px;
            height: 400px;
            background-color: pink;
            display: flex;
            /* text-align: center; */
        }
    .child {
            width: 200px;
            height: 200px;
            background-color: green;
            margin: auto;
            /* text-align: center; */
        }
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容