CSS垂直居中方法三
這種方法,在 content 元素外插入一個(gè) div。設(shè)置此 div height:50%; margin-bottom:-contentheight;。 content 清除浮動(dòng),并顯示在中間。
<div id="floater"></div>
<div id="content">
Content here
</div>
#floater {float:left; height:50%; margin-bottom:-120px;}
#content {clear:both; height:240px; position:relative;}
優(yōu)點(diǎn): 適用于所有瀏覽器 沒有足夠空間時(shí)(例如:窗口縮小) content 不會(huì)被截?cái)啵瑵L動(dòng)條出現(xiàn)
缺點(diǎn): 唯一我能想到的就是需要額外的空元素了(也沒那么糟,又是另外一個(gè)話題)
CSS垂直居中方法四
這個(gè)方法使用了一個(gè) position:absolute,有固定寬度和高度的 div。這個(gè) div 被設(shè)置為 top:0; bottom:0;。但是因?yàn)樗泄潭ǜ叨,其?shí)并不能和上下都間距為 0,因此 margin:auto; 會(huì)使它居中。使用 margin:auto;使塊級(jí)元素垂直居中是很簡(jiǎn)單的。
<div id="content">
Content here</div>
#content {
position:absolute;
top:0;
bottom:0;
left:0;
right:0;
margin:auto;
height:240px;
width:70%;
}
優(yōu)點(diǎn): 簡(jiǎn)單
缺點(diǎn): IE(IE8 beta)中無效 無足夠空間時(shí),content 被截?cái),但是不?huì)有滾動(dòng)條出現(xiàn)
CSS垂直居中方法五
這個(gè)方法只能將單行文本置中。只需要簡(jiǎn)單地把 line-height 設(shè)置為那個(gè)對(duì)象的 height 值就可以使文本居中了。
<div id="content">
Content here</div>
#content {height:100px; line-height:100px;}
優(yōu)點(diǎn): 適用于所有瀏覽器 無足夠空間時(shí)不會(huì)被截?cái)?/P>
缺點(diǎn): 只對(duì)文本有效(塊級(jí)元素?zé)o效) 多行時(shí),斷詞比較糟糕
這個(gè)方法在小元素上非常有用,例如使按鈕文本或者單行文本居中。
上一頁 [1] [2] [3] [4] [5] [6] 下一頁 |