天天看點

html圖檔縱向變,html/css如何讓圖檔上下居中(居中垂直)?

html/css如何讓圖檔上下居中(居中垂直)?下面本篇文章來給大家介紹一下讓圖檔上下居中(居中垂直)的方法。有一定的參考價值,有需要的朋友可以參考一下,希望對大家有所幫助。

html圖檔縱向變,html/css如何讓圖檔上下居中(居中垂直)?

html/css圖檔垂直居中

1、利用高==行高實作圖檔垂直居中

這種方法是要注明高度才可以使用,代碼如下:

html圖檔縱向變,html/css如何讓圖檔上下居中(居中垂直)?

style="display: inline-block; vertical-align: middle;" />

2、利用table實作圖檔垂直居中

利用table的方法是利用了table的垂直居中屬性,代碼如下:

html圖檔縱向變,html/css如何讓圖檔上下居中(居中垂直)?

style="display: inline-block;" />

這裡使用display: table;和display: table-cell;來模拟table,這種方法并不相容IE6/IE7,IE67不支援display: table,如果你不需要支援IE67那就可以用

缺點:當你設定了display: table;可能會改變你的原有布局

3、利用絕對定位實作圖檔垂直居中

如果已知圖檔的寬度和高度可以這樣,代碼如下:

html圖檔縱向變,html/css如何讓圖檔上下居中(居中垂直)?

top:50%;margin-left:-60px;margin-top:-20px;" />

4、移動端可以利用flex布局實作css圖檔垂直居中

移動端一般浏覽器版本都比較高,是以可以大膽的使用flex布局,(flex布局參考css3的flex布局用法)示範代碼如下:

css代碼:

.ui-flex {

display: -webkit-box !important;

display: -webkit-flex !important;

display: -ms-flexbox !important;

display: flex !important;

-webkit-flex-wrap: wrap;

-ms-flex-wrap: wrap;

flex-wrap: wrap

}

.ui-flex, .ui-flex *, .ui-flex :after, .ui-flex :before {

box-sizing: border-box

}

.ui-flex.justify-center {

-webkit-box-pack: center;

-webkit-justify-content: center;

-ms-flex-pack: center;

justify-content: center

}

.ui-flex.center {

-webkit-box-pack: center;

-webkit-justify-content: center;

-ms-flex-pack: center;

justify-content: center;

-webkit-box-align: center;

-webkit-align-items: center;

-ms-flex-align: center;

align-items: center

}

html代碼:

html圖檔縱向變,html/css如何讓圖檔上下居中(居中垂直)?

更多web開發知識,請查閱 HTML中文網 !!