實(shí)現(xiàn)HTML元素的居中 看似簡(jiǎn)單,實(shí)則不然
水平居中比如容易,垂直居中比較難弄定,水平垂直都居中更不容易。在這個(gè)響應(yīng)式布局的年代,很難固定元素的寬高,俺統(tǒng)計(jì)了1下,目前的幾種方法。本文由淺入深逐一介紹,使用了同1段HTML代碼:
<div class="center">
<img src="jimmy-choo-shoe.jpg" alt="">
</div>
下面鞋子圖片會(huì)變化但原始大小始終是500px × 500px,主題背景色彩使用了HSL colors
有些場(chǎng)景下 簡(jiǎn)單的方法就是最好的方法
div.center { text-align: center; background: hsl(0, 100%, 97%); }
div.center img { width: 33%; height: auto; }
但該方法不能讓圖片垂直居中:需要給 div 添加 padding 或 給 div 中的元素添加 margin-top
和 margin-bottom
一樣也是水平居中,局限性跟第1種方法1樣:
div.center { background: hsl(60, 100%, 97%); }
div.center img { display: block; width: 33%; height: auto; margin: 0 auto; }
注意 display: block
,
這類情況下必須有 margin:
0 auto
.
使用 display:
table-cell
, 可以實(shí)現(xiàn)水平垂直都居中。通常需要添加1個(gè)額外的空元素。
<div class="center-aligned">
<div class="center-core">
<img src="jimmy-choo-shoe.jpg">
</div>
</div>
CSS 代碼:
.center-aligned { display: table; background: hsl(120, 100%, 97%);width: 100%; }
.center-core { display: table-cell; text-align: center; vertical-align:middle; }
.center-core img { width: 33%; height: auto; }
注意 width: 100%
是為了避免 div
折疊,外面的容器需要1個(gè)高度才能垂直居中。
如果垂直居中的元素是放在 . body 中的話,需要給 html
和 body
設(shè)置
height. 在所有閱讀器中均有效,包括 IE8+.
最近 Stephen Shaw 推行的1項(xiàng)新技術(shù)可以很好地兼容各種閱讀器。唯1的缺點(diǎn)是外部容器必須聲明height
.absolute-aligned {
position: relative; min-height: 500px;
background: hsl(200, 100%, 97%);
}
.absolute-aligned img {
width: 50%;
min-width: 200px;
height: auto;
overflow: auto; margin: auto;
position: absolute;
top: 0; left: 0; bottom: 0; right: 0;
}
Stephen 在 他的文章 中驗(yàn)證了這段代碼的許多版本
Chris Coiyer 提出了1個(gè)新的方法:使用 CSS transforms. 同時(shí)支持水平居中和垂直居中:
.center { background: hsl(180, 100%, 97%); position: relative; min-height: 500px; }
.center img { position: absolute; top: 50%; left: 50%;
transform: translate(⑸0%, ⑸0%); width: 30%; height: auto; }
有以下缺點(diǎn):
1旦屬性變量和特定前綴消失的話,這類方法極可能成為首選的居中方案.
.center { background: hsl(240, 100%, 97%); display: flex; justify-content: center; align-items: center; }
.center img { width: 30%; height: auto; }
在許多方面 flexbox 是最簡(jiǎn)單的解決方案,但制約因素是
各種陳腐語(yǔ)法和低版本的IE, (雖然 display:
table-cell
是1個(gè)可以接受的方案). 完全的 CSS代碼:
.center { background: hsl(240, 100%, 97%);
display: -webkit-box; /* OLD: Safari, iOS 6 and earlier; Android browser, older WebKit */
display: -moz-box; /* OLD: Firefox (can be buggy) */
display: -ms-flexbox; /* OLD: IE 10 */
display: -webkit-flex; /* FINAL, PREFIXED, Chrome 21+ */
display: flex; /* FINAL: Opera 12.1+, Firefox 22+ */
-webkit-box-align: center;
-moz-box-align: center;
-ms-flex-align: center;
-webkit-align-items: center;
align-items: center;
-webkit-box-pack: center;
-moz-box-pack: center;
-ms-flex-pack: center;
-webkit-justify-content: center;
justify-content: center;
}
現(xiàn)在規(guī)范已構(gòu)成,閱讀器也支持, I have written extensively on flexbox layout and its uses.
在某些場(chǎng)景下比 flexbox 更通用:
.center { background: hsl(300, 100%, 97%); min-height: 600px; position:relative; }
.center img { width: 40%; height: auto; position: absolute; top:calc(50% - 20%); left: calc(50% - 20%); }
不言而喻, calc
允許在當(dāng)前的頁(yè)面布局上進(jìn)行計(jì)算。在上面的例子中,50%
是容器中元素的中間點(diǎn),但是單獨(dú)使用會(huì)使得image的左上角位于<div>
中間。我們需要把width
和height 再往回拉1下,大小分別是圖片width
和height 的1半。表達(dá)式以下:
top: calc(50% - (40% / 2)); left: calc(50% - (40% / 2));
在目前的閱讀器中,你可以發(fā)現(xiàn):當(dāng)內(nèi)容 fixed 且大小已知的時(shí)候,該技術(shù)效果最好:
.center img { width: 500px; height: 500px; position: absolute; top:calc(50% - (300px / 2)); left: calc(50% - (300px
生活不易,碼農(nóng)辛苦
如果您覺(jué)得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)