div居中的十二種方法

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

水平居中
父元素添加text-align: center;子元素添加 display: inline-block;(文字也會(huì)居中)
 <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;(文字不會(huì)居中,若是文字也居中可以在父元素或者子元素添加一個(gè)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; */
        }
父元素添加相對(duì)定位position: relative;子元素添加 絕對(duì)定位position: absolute;(文字不會(huì)居中,若是文字也居中可以在父元素或者子元素添加一個(gè)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;子元素不添加;(文字不會(huì)居中,若是文字也居中可以在父元素或者子元素添加一個(gè)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;子元素不添加(文字不會(huì)居中,若是文字也居中可以在父元素或者子元素添加一個(gè)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; */
        }
父元素添加相對(duì)定位position: relative;子元素添加 絕對(duì)定位position: absolute;(文字不會(huì)居中,若是文字也居中可以在父元素或者子元素添加一個(gè)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;子元素不添加;(文字不會(huì)居中,若是文字也居中可以在父元素或者子元素添加一個(gè)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;(文字也會(huì)居中)
 <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;
        }
父元素添加相對(duì)定位position: relative;子元素添加 絕對(duì)定位position: absolute;(文字不會(huì)居中,若是文字也居中可以在父元素或者子元素添加一個(gè)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; */
        }
父元素添加相對(duì)定位position: relative;子元素添加 絕對(duì)定位position: absolute;(文字不會(huì)居中,若是文字也居中可以在父元素或者子元素添加一個(gè)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;子元素不添加;(文字不會(huì)居中,若是文字也居中可以在父元素或者子元素添加一個(gè)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;(文字不會(huì)居中,若是文字也居中可以在父元素或者子元素添加一個(gè)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; */
        }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • div水平居中的N種方法 一、單行垂直居中 如果一個(gè)容器中只有一行文字,對(duì)它實(shí)現(xiàn)居中相對(duì)比較簡(jiǎn)單,我們只需要設(shè)置它...
    fredah閱讀 6,048評(píng)論 0 1
  • 1.div絕對(duì)定位水平垂直居中【margin:auto實(shí)現(xiàn)絕對(duì)定位元素的居中】 兼容性:,IE7及之前版本不支持 ...
    凱凱_閱讀 481評(píng)論 0 0
  • 文章目錄 一、行高(line-height)法 二、內(nèi)邊距(padding)法 三、模擬表格法 四、CSS3的tr...
    msqt閱讀 1,305評(píng)論 0 0
  • 開篇序言:之前在華南理工大學(xué)計(jì)算機(jī)研究所面試,被一個(gè)老師抓著這個(gè)問(wèn)題問(wèn)了一下午 當(dāng)時(shí)回答的很窘迫,大概只說(shuō)出七八種...
    等風(fēng)來(lái)臨閱讀 37,402評(píng)論 1 27
  • margin: 0 auto; text-align: center 居中 當(dāng)父元素設(shè)置了寬高 可用margin:...
    U17閱讀 659評(píng)論 0 0

友情鏈接更多精彩內(nèi)容