本文重點整理 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,各自定義這組 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 元素與其他元素同行、寬高無法控制;用 display:block 可把內嵌元素模擬為塊元素,display:inline 則實現同行排列。
IE 不認得 min- 定義,卻會把一般的 width、height 當成帶 min 的效果。解法是:
#box{width:80px;height:35px;}
html>body#box{width:auto;height:auto;min-width:80px;min-height:35px;}
min-width 能保證排版不會過窄,但 IE 不認,還把 width 當成最小寬度。解法是在 body 下放一個 div 並指定 class,然後:
#container{
min-width:600px;
width:expression(document.body.clientWidth<600?"600px":"auto");
}
第一行是標準做法,第二行則靠只有 IE 認得的 JavaScript 判斷來實現最小寬度,代價是 HTML 會不太正規。