日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

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

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

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

實現(xiàn)HTML元素的居中 看似簡單,實則不然

水平居中比如容易,垂直居中比較難弄定,水平垂直都居中更不容易。在這個響應(yīng)式布局的年代,很難固定元素的寬高,俺統(tǒng)計了1下,目前的幾種方法。本文由淺入深逐一介紹,使用了同1段HTML代碼:

<div class="center">
<img src="jimmy-choo-shoe.jpg" alt="">
</div>

下面鞋子圖片會變化但原始大小始終是500px × 500px,主題背景色彩使用了HSL colors 

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

Photograph of a classic Chuck Converse shoe

有些場景下 簡單的方法就是最好的方法

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,  可以實現(xiàn)水平垂直都居中。通常需要添加1個額外的空元素。

<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個高度才能垂直居中。 如果垂直居中的元素是放在 .  body 中的話,需要給 html 和 body 設(shè)置 height. 在所有閱讀器中均有效,包括 IE8+.

4. Absolute 居中

Photograph of an Under Armour Micro G Toxic Six shoe

最近  Stephen Shaw 推行的1項新技術(shù)可以很好地兼容各種閱讀器。唯1的缺點是外部容器必須聲明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 在 他的文章 中驗證了這段代碼的許多版本

5. 使用 translate 居中

Photograph of a Jimmy Choo shoe

 Chris Coiyer 提出了1個新的方法:使用 CSS transforms. 同時支持水平居中和垂直居中:

.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; }

有以下缺點:

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

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 是最簡單的解決方案,但制約因素是 各種陳腐語法和低版本的IE, (雖然 display: table-cell是1個可以接受的方案). 完全的 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

在某些場景下比 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)前的頁面布局上進(jìn)行計算。在上面的例子中,50% 是容器中元素的中間點,但是單獨(dú)使用會使得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ù)效果最好:

.center img { width: 500px; height: 500px; position: absolute; top:calc(50% - (300px / 2)); left: calc(50% - (300px
生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關(guān)閉
程序員人生
主站蜘蛛池模板: 国产精品178页 | 日韩一区中文字幕 | 精品国产91亚洲一区二区三区www | 黄色在线观看视频网站 | 国产污视频在线 | 成人欧美一区二区三区视频xxx | 91蝌蚪色| 日本在线精品 | 狠狠淫xx | 国产日韩精品在线 | av网站免费线看 | 欧美日韩夜夜 | 99久久视频| av影视大全 | 亚洲精品免费观看 | 久久国产精品精品国产色婷婷 | 日本黄色网址大全 | 亚洲视频免费观看 | 久久国产精品99久久久久久牛牛 | 日韩精品av一区二区三区 | 欧日韩在线 | 久久久青草婷婷精品综合日韩 | 日韩精品视频在线免费观看 | 欧美久久久 | 精产国产伦理一二三区 | 国产一区二区免费看 | 免费一区二区三区 | 日本一区二区视频 | 色呦呦视频在线观看 | 免费三级在线 | 国产精品福利小视频 | 中文字幕黄色 | 一区二区三区 在线 | 九九视频网 | 插插插插综合 | 免费黄色小网站 | 日韩精品影院 | 黄色电影免费在线观看 | 国产精品一区二区三区四区五区 | 黄色在线免费视频 | 久久久不卡 |