前端必會(huì)~CSS?水平垂直居中布局技巧

水平居中元素:

方式一:CSS3 transform

.parent {

? ? position: relative;

}

.child {

? ? position: absolute;

? ? left: 50%:

? ? transform: translateX(-50%);

}

方式二:flex 布局

.parent {

? ? display: flex;

? ? justify-content: center;

}

適用于子元素為浮動(dòng),絕對(duì)定位,內(nèi)聯(lián)元素,均可水平居中。

居中的元素為常規(guī)文檔流中的內(nèi)聯(lián)元素(display: inline)

常見的內(nèi)聯(lián)元素有:span, a, img, input, label 等等

.parent {

? ? text-align: center;

}

居中的元素為常規(guī)文檔流中的塊元素(display: block)

常見的塊元素:div, h1~h6, table, p, ul, li 等等

方式一:設(shè)置 margin

.parent {

? ? width: 100%;

}

.child {

? ? width: 600px;

? ? height: 50px;

? ? margin: 0 auto;

? ? background: #999;

}

方式二:修改為 inline-block 屬性

.parent {

? ? text-align: center;

}

.child {

? ? display: inline-block;

}

.child {

? ? width: 100px;

? ? float: left;

? ? position: relative;

? ? left: 50%;

? ? margin-left: -50px;

}

方式一:

.parent {

? ? position: relative;

}

.child {

? ? position: absolute;

? ? width: 100px;

? ? left: 50%;

? ? margin-left: -50px;

}

方式二:

.parent {

? ? position: relative;

}

.child {

? ? position: absolute;

? ? width: 100px;

? ? left: 0;

? ? right: 0;

? ? margin: 0 auto;

}

垂直居中元素:

方式一:CSS3 transform

.parent {

? ? position: relative;

}

.child {

? ? position: absolute;

? ? top: 50%;

? ? transform: translateY(-50%);

}

方式二:flex 布局

.parent {

? ? display: flex;

? ? align-items: center;

}

適用于子元素為浮動(dòng),絕對(duì)定位,內(nèi)聯(lián)元素,均可垂直居中。

.text {

? ? line-height: 200px;

? ? height: 200px;

}

方式一:

.parent {

? ? position: relative;

}

.child{

? ? position: absolute;

? ? top: 50%;

? ? height: 100px;

? ? margin-top: -50px;

}

方式二:

.parent {

? ? position: relative;

}

.child{

? ? position: absolute;

? ? top: 0;

? ? bottom: 0;

? ? height: 100px;

? ? margin: auto 0;

}

垂直居中元素:

div {

? ? width: 100px;

? ? height: 100px;

? ? margin: auto;

? ? position: fixed;

? ? //absolute is ok

? ? top: 0;

? ? right: 0;

? ? bottom: 0;

? ? left: 0;

}

優(yōu)點(diǎn):

不僅可以實(shí)現(xiàn)在正中間,還可以在正左方,正右方

元素的寬高支持百分比 % 屬性值和 min-/max- 屬性

可以封裝為一個(gè)公共類,可做彈出層

瀏覽器支持性好

.child {

? ? width: 100px;

? ? height: 100px;

? ? position: absolute;

? ? top: 50%;

? ? left: 50%;

? ? margin-left: -50px;

? ? margin-top: -50px;

}

特點(diǎn):

良好的跨瀏覽器特性,兼容 IE6 - IE7

靈活性差,不能自適應(yīng),寬高不支持百分比尺寸和 min-/max- 屬性

.child {

? ? width: 100px;

? ? height: 100px;

? ? position: absolute;

? ? top: 50%;

? ? left: 50%;

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

}

特點(diǎn):

內(nèi)容可自適應(yīng),可以封裝為一個(gè)公共類,可做彈出層

可能干擾其他 transform 效果

.parent {

? ? display: flex;

? ? justify-content: center;

? ? align-items: center;

}

這是 CSS 布局未來的趨勢(shì)。Flexbox 是 CSS3 新增屬性,設(shè)計(jì)初衷是為了解決像垂直居中這樣的常見布局問題。

text {

? ? height: 100px;

? ? line-height: 100px;

? ? text-align: center;

}

原文:https://zhuanlan.zhihu.com/p/25068655

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 各種純css圖標(biāo) CSS3可以實(shí)現(xiàn)很多漂亮的圖形,我收集了32種圖形,在下面列出。直接用CSS3畫出這些圖形,要比...
    劍殘閱讀 9,700評(píng)論 0 8
  • 單列布局水平居中水平居中 水平居中的頁面布局中最為常見的一種布局形式,多出現(xiàn)于標(biāo)題,以及內(nèi)容區(qū)域的組織形式,下面介...
    Osmond_wang閱讀 337評(píng)論 0 1
  • 一、水平居中 1.行內(nèi)元素水平居中 text-align: center可以實(shí)現(xiàn)在塊級(jí)元素內(nèi)部的行內(nèi)元素水平居...
    Cheng丶閱讀 458評(píng)論 0 0
  • 單列布局 水平居中 水平居中的頁面布局中最為常見的一種布局形式,多出現(xiàn)于標(biāo)題,以及內(nèi)容區(qū)域的組織形式,下面介紹四種...
    阿七筆記閱讀 179評(píng)論 0 1
  • 目前,我們很多商品都是通過傳統(tǒng)的經(jīng)銷商渠道進(jìn)行銷售,因此,一個(gè)完美的市場(chǎng)開發(fā)方案就顯得尤為重要,下面介紹一位營銷總...
    靜靜地發(fā)呆閱讀 568評(píng)論 0 2