想知道 Firefox 與 IE 在 CSS 樣式表上究竟有哪些差異嗎?以下就從最基礎的幾個環節談起。
現在大多數人用 !important 做 hack,在 IE6 與 Firefox 下都正常,但 IE7 能正確解析 !important,反而導致頁面顯示不如預期。針對 IE7,一個不錯的 hack 是使用「*+html」:
#1{color:#333;}/*Moz*/
*html#1{color:#666;}/*IE6*/
*+html#1{color:#999;}/*IE7*/
如此一來,Firefox 顯示 #333、IE6 顯示 #666、IE7 顯示 #999。
主要樣式如下:
body{text-align:center;}
#center{margin-right:auto;margin-left:auto;}
先在父元素定義 text-align:center,讓內部內容置中,IE 這樣就足夠;但在 Mozilla 中不會生效,解法是在子元素上再加 margin-right:auto 與 margin-left:auto。若想让整个頁面置中,建議別全塞進單一 div,而是拆成多個 div,各自定義這組 margin。
#box{
width:600px;/*for IE6.0*/
width:500px;/*for ff+IE6.0*/
}
#box{
width:600px!important;/*for ff*/
width:600px;/*for ff+IE6.0*/
width/**/:500px;/*for IE6.0-*/
}
像 #box{float:left;width:100px;margin:0 0 0 100px;} 這種情況,IE 會產生 200px 的距離,此時加上 display:inline 就能讓浮動忽略這個問題。
順帶說明 block 與 inline 兩類元素的特性:block 元素總是在新行開始,寬高、行高、邊距都能控制;inline 元素則與其他元素排在同一行,寬高無法控制。透過 display:block 可把內嵌元素模擬成塊元素,display:inline 則能實現同行排列的效果。