網頁設計DIV實現垂直居中對齊的方法

 


Web Design儘管有CSS的vertical-align特性,但是並不能有效解決未知高度的垂直居中問題(在一個DIV標籤裡有未知高度的文本或圖片的情況下)。網站設計標準瀏覽器如Mozilla, Opera, Safari等.,可將父級元素顯示方式設定為TABLE(display: table;) ,內部子元素定為table-cell (display: table-cell),通過vertical-align特性使其垂直居中,但非標準瀏覽器是不支持的。 非標準瀏覽器只能在子元素裡設距頂部50%,裡面再套個元素距頂部-50% 來抵消。

 

body {padding: 0; margin: 0;}body,html{height: 100%;} #outer {height: 100%; overflow: hidden; position: relative;width: 100%; background:ivory;} #outer[id] {display: table; position: static;} #middle {position: absolute; top: 50%;} /* for explorer only*/ #middle[id] {display: table-cell; vertical-align: middle; position: static;} #inner {position: relative; top: -50%;width: 400px;margin: 0 auto;} /* for explorer only */ div.greenBorder {border: 1px solid green; background-color: ivory;} <div id="outer"> <div id="middle"> <div id="inner"> </div> </div> </div>
網頁寄存中CSS代碼的優點是沒有hacks,採用了IE不支持的CSS2選擇器#value[id]。 CSS2選擇器#value[id]相當於選擇器#value,但是Internet Explorer不支持這種類型的選擇器。同樣地.value[class],相當於.value,這些只有標準瀏覽器能讀懂。 測試:Firefox1.5、Opera9.0、IE6.0、IE5.0通過。


 

 

arrow
arrow
    全站熱搜

    hkseo 發表在 痞客邦 留言(0) 人氣()