多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國(guó)內(nèi)最全I(xiàn)T社區(qū)平臺(tái) 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當(dāng)前位置:首頁(yè) > web前端 > htmlcss > CSS實(shí)現(xiàn)居中的7種方法

CSS實(shí)現(xiàn)居中的7種方法

來(lái)源:程序員人生   發(fā)布時(shí)間:2014-12-17 08:29:24 閱讀次數(shù):4488次

實(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 

1.水平居中―使用 text-align 

Photograph of a classic Chuck Converse shoe

有些場(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 

2. margin: auto 居中

Photograph of a white classic Nike sneaker

一樣也是水平居中,局限性跟第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.

3. table-cell 居中

Photograph of black Oxford calfskin shoe designed by Vivienne Westwood

使用 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+.

4. Absolute 居中

Photograph of an Under Armour Micro G Toxic Six shoe

最近  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)證了這段代碼的許多版本

5. 使用 translate 居中

Photograph of a Jimmy Choo shoe

 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):

  • CSS transform 需要針對(duì)不同的閱讀器使用 特定的前綴  (-moz  或  -o  或  -webkit
  • 在低版本的IE (IE8 及以下 )中無(wú)效
  • 外部容器需要設(shè)置高度 height (or gain it in some other way)  由于它不能從它的absolutely-positioned 內(nèi)容上取得高度.
  • 如果內(nèi)容包括 text, 當(dāng)前閱讀器合成技術(shù)對(duì)文本解釋得很模糊.

6. 使用 Flexbox 居中

Photograph of a Manolo Blahnik shoe

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.

7. 使用 calc 居中

Photograph of a Christian Louboutin shoe

在某些場(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)
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關(guān)閉
程序員人生
主站蜘蛛池模板: 手机看片高清国产日韩片 | 羞污影院 | 日韩综合第一页 | 久久人人澡人人爽人人爱 | 欧美另类69xxxxx性欧 | 亚洲aⅴ | 亚洲在线免费免费观看视频 | 亚洲黄色在线视频 | 亚洲不卡在线视频 | 欧美性最xxx | 日本成人在线免费 | 毛片色 | 欧美18videosex动漫3d | 中文字幕天堂 | 欧美一级毛片欧美一级成人毛片 | 在线看中文字幕 | 泰国一级毛片aaa下面毛多 | 国产性生交xxxxx免费 | 国产一级一片 | 国产一区日韩二区欧美三区 | 欧美色频| 牛和人交vvideos欧美 | a级亚洲片精品久久久久久久 | 在线高清视频18jin观看 | 深夜做爰性大片中文 | 亚洲精品不卡视频 | 69热在线 | 久久免费观看国产精品 | 亚洲乱码一区二区三区在线观看 | 性盈影院| 欧美精品综合一区二区三区 | 欧美午夜三级 | 91四虎国自产在线播放线 | 亚洲精品第一页中文字幕 | 老司机成人免费精品视频 | 亚洲天堂免费观看 | 日本啊啊视频 | 亚洲欧洲一区二区 | 国产成人久久久精品毛片 | 婷婷在线五月 | 亚洲视频免|