CSS水平居中和垂直居中多種解決方案
這里和大家重點描述一下CSS水平居中和垂直居中的多種解決方案,主要包括單行垂直居中,層水平居中,div層垂直居中等內(nèi)容,相信通過本文的學(xué)習(xí)你對CSS居中問題會有更加深刻的認(rèn)識。
CSS實例:水平居中和垂直居中的多種解決方案
在定義網(wǎng)頁的CSS樣式的時候,我們或許并不怎么用到垂直居中。在51cto.com中,大量的文章介紹了這些知識。在和別人合作的時候,你的代碼的自適應(yīng)性就需要做到最大程度的好。你自己在做自己的網(wǎng)頁的時候或許可以保證在視覺上是居中的,但是如果讓這個元素或者他的父元素變大了,你還能保證居中嗎?
vertical-align是個不錯的屬性,但是這個屬性起初是針對表格的cell的。當(dāng)然這個屬性雖然對塊級元素不起作用,但是對行內(nèi)元素比如span還是有作用的(這時候,這個屬性設(shè)置為負(fù)值元素內(nèi)容就下移,正值則上移)。
下面是比較專業(yè)的垂直居中的集中解決方案。記錄于此,以便查閱。
1、單行垂直居中
文字在層中垂直居中vertical-align屬性是做不到的.我們這里有個比較巧妙的方法就是:設(shè)置height的高度與line-height的高度相同!
ExampleSourceCode
- <divstyledivstyle="line-height:500px;height:500;">
2、層水平居中
設(shè)置div的寬度小于父div的寬度,設(shè)置margin:0auto;,即可讓div居中。
ExampleSourceCode
- #parentdiv
- {
- width:500px;
- }
- #childdiv{
- width:200px;
- margin:0auto;
- }
3、層中的文字水平居中
在childdiv的css加上text-align:center
ExampleSourceCode
- #parentdiv
- {
- width:500px;
- }
- #childdiv{
- width:200px;
- margin:0auto;
- text-align:center;
- }
#p#4、div層垂直居中
ExampleSourceCode
- <divstyledivstyle="width:275px;height:375px;border:solidred;">
- <divstyledivstyle="background:green;height:375px;width:275px;
- position:relative;display:table-cell;vertical-align:middle;">
- <div
- style="background:red;position:static;position:absolute\9;top:50%;">
- <div
- style="background:blue;position:relative;top:-50%;">
- < span>div>
- < span>div>
- < span>div>
- < span>div>
5、div層垂直水平居中,英文超長換行
ExampleSourceCode
- <divstyledivstyle="float:left;width:275px;height:375px;border:solidred;">
- <div
- style="height:375px;width:275px;position:relative;
- display:table-cell;vertical-align:middle;">
- <div
- style="position:static;position:absolute\9;top:50%;">
- <divstyledivstyle="position:relative;top:-50%;text-align:center;">
- <divstyledivstyle="width:85px;WORD-WRAP:break-word;TABLE-LAYOUT:fixed;
- word-break:break-all;margin:0auto;">
- < span>div>
- < span>div>
- < span>div>
- < span>div>
- < span>div>
6、div垂直滾動
ExampleSourceCode
- <div
- style="width:160px;height:260px;
- overflow-y:scroll;border:1pxsolid;">
- < span>div>
#p#
7、垂直居中和使用text-align水平居中
ExampleSourceCode
- <divstyledivstyle="float:left;width:275px;height:375px;border:solidred;">
- <div
- style="height:375px;width:275px;position:relative;
- display:table-cell;vertical-align:middle;">
- <div
- style="position:static;position:absolute\9;top:50%;">
- <div
- style="position:relative;top:-50%;text-align:center;">
- <divstyledivstyle="width:275px;">
- <divstyledivstyle="width:160px;WORD-WRAP:break-word;TABLE-LAYOUT:fixed;
- word-break:break-all;text-align:left;">
- < span>div>
- < span>div>
- < span>div>
- < span>div>
- < span>div>
- < span>div>
8、垂直居中和使用margin水平居中
ExampleSourceCode
- <divstyledivstyle="float:left;width:275px;
- height:375px;border:solidred;">
- <div
- style="height:375px;width:275px;position:relative;
- display:table-cell;vertical-align:middle;">
- <div
- style="position:static;position:absolute\9;top:50%;">
- <div
- style="position:relative;top:-50%;">
- <divstyledivstyle="margin:0auto;width:160px;WORD-WRAP:break-word;
- TABLE-LAYOUT:fixed;word-break:break-all;">
- < span>div>
- < span>div>
- < span>div>
- < span>div>
- < span>div>
【編輯推薦】
- 解析DIV+CSS網(wǎng)頁布局的意義與副作用
- 深入剖析CSS層疊與繼承的使用
- 專家推薦三大“頂級”CSS技巧!
- CSS中hack和注釋書寫規(guī)范
- DIV CSS初學(xué)者必須掌握的10個問題與技巧