css實(shí)現(xiàn)垂直水平居中的方法

網(wǎng)上這種類型的文章數(shù)之不盡,作為個(gè)人學(xué)習(xí)的一個(gè)整理,僅供參考,歡迎斧正。
兼容性方面請(qǐng)參考http://caniuse.com/

方法一:table-cell實(shí)現(xiàn)垂直居中

缺點(diǎn):margin無效(我給所有元素設(shè)置了margin:auto的)
優(yōu)點(diǎn):兼容IE8以上瀏覽器,動(dòng)態(tài)設(shè)置居中,不需要知道寬高

//html
<div class="ct1 ct">
        <div class="box1 box"></div>
</div>
//css
.ct {
        width: 300px;
        height: 300px;
        border: 1px solid red;
        margin: 0 auto;
    }
 .box {
        width: 80px;
        height: 80px;
        background: lightblue;
    }
.ct1 {
        display: table-cell;
        vertical-align: middle;
        text-align: center;
    }
.box1 {
        display: inline-block;//為了使text-align生效
    }

效果:

image.png

方法二:父元素設(shè)置相對(duì)定位,子元素絕對(duì)定位。

缺點(diǎn):
1、要么必須知道寬高,手動(dòng)設(shè)置偏移量(margin量)。要么用css3的tranform(IE11以上,兼容性不好)
2、子元素高于父元素會(huì)超出父元素直接顯示,不會(huì)出現(xiàn)滾動(dòng)條
優(yōu)點(diǎn):
影響小,寫法固定且簡便

//html
 <div class="ct2 ct">
        <div class="box2 box"></div>
 </div>

//css
.ct {
    width: 300px;
    height: 300px;
    border: 1px solid red;
    margin: 0 auto;
}
.box {
    width: 80px;
    height: 80px;
    background: lightblue;
}
.ct2 {
     position: relative;
}

.box2 {
     position: absolute;
     left: 50%;
     top: 50%;
     transform: translate(-50%, -50%);
 }

效果:


image.png

方法三:display:flex

缺點(diǎn):對(duì)IE兼容性不好,IE11才部分支持
優(yōu)點(diǎn):用起來又簡單又方便,很爽啊有木有~~(?′▽`)??

//html
<div class="ct3 ct">
        <div class="box3 box"></div>
</div>

//css
.ct {
    width: 300px;
    height: 300px;
    border: 1px solid red;
    margin: 0 auto;
}
.box {
    width: 80px;
    height: 80px;
    background: lightblue;
}
.ct3{
    display: flex;
    align-items: center;
    justify-content: center;
 }
image.png

方法四:定位+偏移0+margin:auto

//html
    <div class="ct4 ct">
        <div class="box4 box"></div>
    </div>
//css
    .ct4{
        position: relative;
    }
    .box4{
        position: absolute;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        margin: auto;
    }
image.png

你問我最喜歡用哪個(gè)?不考慮兼容性的情況下當(dāng)然是flexbox啊(????)

最后編輯于
?著作權(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),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • 問答題47 /72 常見瀏覽器兼容性問題與解決方案? 參考答案 (1)瀏覽器兼容問題一:不同瀏覽器的標(biāo)簽?zāi)J(rèn)的外補(bǔ)...
    _Yfling閱讀 14,121評(píng)論 1 92
  • 收聽音頻,戳鏈接,舊號(hào)itclan已暫停使用,歡迎關(guān)注微信itclanCoder公眾號(hào)可收聽更多音頻 前言 關(guān)于網(wǎng)...
    itclanCoder閱讀 8,347評(píng)論 3 30
  • 1.絕對(duì)定位居中技術(shù) 我們一直用margin:auto實(shí)現(xiàn)水平居中,而一直認(rèn)為margin:auto不能實(shí)現(xiàn)垂直居...
    DecadeHeart閱讀 1,662評(píng)論 0 3
  • 茶,大抵是工作之余用來慢嘗細(xì)品的隱匿之物。 每天在快節(jié)奏的生活中奔波勞碌之后,必定會(huì)淡定下來,泡一杯熱茶,聞著裊裊...
    5ebc5e57260e閱讀 443評(píng)論 2 4
  • 我以為 告別是一場(chǎng)莊重的儀式 停下匆匆的步伐 等待 山寒水冷 冬雪模糊了迢迢歸路 回首帶不走經(jīng)歷的朝朝暮暮 等待 ...
    蓼仃花溆閱讀 215評(píng)論 0 0

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