以下整理實作中常見的 CSS 相容問題與對應寫法。這些技巧都是特定世代瀏覽器的產物,建議集中放在相容樣式表中,以條件式註解載入。
區別 IE6 與 FF: background:orange; *background:blue; 區別 IE6 與 IE7: background:green !important; background:blue; 區別 IE7 與 FF: background:orange; *background:green; 區別 FF、IE7、IE6: background:orange; *background:green !important; *background:blue;
IE7、IE8 相容:<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
關鍵字支援規則:!important 可被 Firefox 與 IE7 識別;* 可被 IE6、IE7 識別;_ 可被 IE6 識別;*+ 可被 IE7 識別。
<!-- 其他瀏覽器 --> <link rel="stylesheet" type="text/css" href="css.css" /> <!--[if IE 7]> <!-- 適合 IE7 --> <link rel="stylesheet" type="text/css" href="ie7.css" /> <![endif]--> <!--[if lte IE 6]> <!-- 適合 IE6 及以下 --> <link rel="stylesheet" type="text/css" href="ie.css" /> <![endif]-->
IE / Opera:物件實際寬度 = margin-left + width + margin-right
Firefox / Mozilla:物件實際寬度 = margin-left + border-left-width + padding-left + width + padding-right + border-right-width + margin-right
1. 游標手型。Firefox 不支援 cursor:hand,但支援 pointer;IE 兩者都支援,因此統一寫 pointer。
2. 單位。Firefox 設定 HTML 標籤的 style 屬性時,位置、寬高與尺寸值必須帶 px;IE 也支援這種寫法,因此統一加上單位,例如 Obj.Style.Height = imgObj.Style.Height + 'px';
3. padding 簡寫。Firefox 無法解析簡寫的 padding: 5px 4px 3px 1px,必須展開為 padding-top / right / bottom / left。
4. 列表縮進。消除 ul、ol 的縮進應寫成 list-style:none;margin:0px;padding:0px;——margin 對 IE 有效,padding 對 Firefox 有效。
5. 透明度。IE:filter:progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=60); Firefox:opacity:0.6;
6. 圓角。IE 不支援;Firefox 用 -moz-border-radius:4px;,也可分別設定四個角。
7. 雙線凹凸邊框。IE:border:2px outset; Firefox:用 -moz-border-top-colors 等屬性分別指定兩色。
8. 自訂游標與捲軸顏色。IE 支援 cursor:url() 與捲軸顏色設定,Firefox 兩者皆不支援。
9. Select 控件。IE 中 Select 永遠處於最上層,且所有 CSS 對它都不起作用。
10. Label 標籤。IE 支援包含圖片與文字的 Label;Firefox 不支援含圖片的 Label,點擊圖片不會觸發對應的 Radio 或 CheckBox。
11. TextArea 事件。Firefox 的 TextArea 不支援 onScroll 事件。
12. display 值。Firefox 不支援 display 的 inline 與 block 混用寫法。
13. 水平居中。Firefox 中對 Div 設 margin-left/right: auto 即可居中,IE 不行;若對 body 設了 text-align,Firefox 下的 Div 還需設 margin: auto 才能居中。
14. 連結樣式順序。最好遵守 L-V-H-A:link、visited、hover、active,否則造訪過的連結可能失去 hover 與 active 樣式。
15. 長段落自動換行。IE 用 word-wrap:break-word;;Firefox 需靠 JS 插入空白來實作。
16. 子容器浮動撐不開父層。解法:在下一個標籤中加上清除浮動的 clear:both;
17. IE6 外边距加倍。浮動元素的 margin 在 IE6 中被解釋為實際值的兩倍,解法是加上 display:inline。
18. IE6 圖片下方有空隙。為 img 加上 display:block,或設定 vertical-align: top | bottom | middle | text-bottom。
19. IE6 兩個層之間有空隙。讓右側 div 同樣 float:left,或針對 IE6 定義 margin-right:-3px;
20. LI 內容過長以省略號顯示。IE 專用:li { width:200px; white-space:nowrap; text-overflow:ellipsis; -o-text-overflow:ellipsis; overflow:hidden; }
21. 文字垂直居中。把元素高度與行高設為相同值:div { height:30px; line-height:30px; }
22. 文字與輸入框對齊。加上 vertical-align:middle;
23. 固定高度又要能被撐開。去掉 height 改用 min-height,並為不支援的 IE6 補寫:{ height:auto!important; height:200px; min-height:200px; }
24. 捲軸顏色。標準模式下的 IE 無法對 body 設定捲軸樣式,需改為對 html 設定 scrollbar-face-color 等屬性。
25. IE6 無法定義 1px 高的容器。受預設行高影響,解法是設定 overflow:hidden; zoom:0.08; line-height:1px;
26. 讓層顯示在 Flash 之上。加入 <param name="wmode" value="transparent" /> 解決 IE,並在 <embed> 上加 wmode="transparent" 解決 Firefox。
27. padding 影響寬高。Firefox 設定 padding 後會相應增加 width 與 height,IE 不會,解法是用 !important 多定義一套寬高。
28. div 之間的空白。Firefox 忽略 div 之間的空白,IE 會處理,因此盡量不要讓相鄰 div 之間有空格或換行,否則可能造成如著名 3px 偏差這類難以追查的問題。
29. 父層撐不開的問題。形如 <div id="parent"><div id="content"></div></div> 的結構,當 content 內容變多時,即使 parent 設了 height:100% 或 auto,在不同瀏覽器下仍無法完善自動伸展。解法是在最下方放一個高度為 0 的清除元素:
<div id="parent"> <div id="content"></div> <div style="font: 0px/0px sans-serif;clear: both;display: block"></div> </div>
30. small 字級的解釋差異。Firefox 為 13px,IE 為 16px。
31. 空白字元寬度差異。Firefox 為 4px,IE 為 8px。
最後提醒:以上大量寫法屬於舊世代瀏覽器的相容補丁。新專案中應優先採用標準語法(flexbox、grid、CSS 變數等),並以特徵偵測取代版本判斷,把 hack 的數量壓到最低。