對前端工作者而言,最痛苦的事莫過於瀏覽器相容性調試,而其中最難纏的莫過於微軟的 IE6.0/IE7.0/IE8.0。為了讓程式碼在各主流瀏覽器中正常運行,我們不得不為不同瀏覽器撰寫對應樣式。本文將為你整理針對各瀏覽器的 CSS 相容 Hack(以 IE6/IE7/IE8/FF 為主),以及 IE 特有的條件註釋用法。
IE6、IE7 能識別 *,標準瀏覽器(如 FF)不能識別 *;
IE6 能識別 *,但不能識別 !important;
IE7 能識別 *,也能識別 !important;
IE8 能識別 ` `,不能識別 *、+、_、* 加 !important;
FF 不能識別 *,但能識別 !important。
例如 style=”*width:10px!important; width:20px;”,在 IE7 下寬度為 10px,在 IE6 下寬度為 20px。
此外還有如下三種寫法:
第一種
width:100px; /* FireFox及其他瀏覽器 */
width:200px` ; /* IE8能識別*/
*width:300px!important; /* IE7 既能識別*號,也能識別important */
*width:400px; /* IE6也能識別*號 */
/*Mr.Think提示:請注意書寫順序@MrThink.net*/
第二種
width:100px; /* FireFox及其他瀏覽器 */
width:200px` ; /* IE8能識別*/
*width:300px; /* IE7也能識別*號 */
_width:400px; /* IE6能識別下劃線*/
/*Mr.Think提示:請注意書寫順序@MrThink.net*/
第三種
width:100px; /* FireFox及其他瀏覽器 */
width:200px` ; /* IE8能識別*/
+width:300px; /* +只識別IE7 */
_width:400px; /* IE6能識別下劃線*/
/*Mr.Think提示:請注意書寫順序@MrThink.net*/
1..color1{ color:#F00; color/***/:#00F /***/}/*IE6,IE7,IE8,FF,OP,SA識別*/
2..color2{ color:#F00; color /***/:#00F /*9**/}/*IE7,IE8,FF,OP,SA識別*/
3..color3{ color:#F00; color/***/:#00F 9}/*IE6,IE7,IE8識別*/
4..color4{ color:#F00; color /***/:#00F9}/*IE7,IE8識別*/ /*"color"和"/***/"之間有個空格*/
width:100px`;/* 支援IE8 */
_width:100px; /* 支援IE6 */
[width:100px; /* 支援IE6,7 */
+width:100px; /* 支援IE6,7 */
*width:100px; /* 支援IE6,7 */
*width:100px!important; /* 支援IE6,7 */
*+width:100px; /* 支援IE6,7 */
*+width:100px!important;/* 支援IE6,7 */
width:100px9; /* 支援IE6,7,8 */
width:100px!important; /* 支援IE6,7,8,FF */
width:100px; /*IE5.x不支持 IE6、IE7、IE8、FF支持 */
1. 僅 IE 可見的寫法
<!--[if IE]>此處內容只有IE可見<![endif]–>
2. 僅 IE6 可見的寫法
<!-–[if IE 6]>此處內容只有IE6.0可見<![endif]–->
3. 僅 IE7 可見的寫法
<!–-[if IE 7]>此處內容只有IE7.0可見<![endif]–->
4. 版本區間可顯示寫法
<!--[if lt IE 6]> IE6以及IE6以下版本可識別 <![endif]--> <!--[if gte IE 6]> IE6以及IE6以上版本可識別 <![endif]--> <!--[if lt IE 7]> IE7以及IE7以下版本可識別 <![endif]--> <!--[if gte IE 7]> IE7以及IE7以上版本可識別 <![endif]--> 5. 非 IE 可見的寫法(注意:此條不符合WEB標準,但確實實用)<!--[if !IE]>此處內容只非IE可見<![endif]-->