標籤:style blog http color 檔案 os
一、類內部Hack
IE都能識別*;標準瀏覽器(如FF)不能識別*;
IE6能識別*,但不能識別 !important,
IE7能識別*,也能識別!important;
FF不能識別*,但能識別!important;
IE6支援底線,IE7和firefox均不支援底線。
| |
IE6 |
IE7 |
FF |
| * |
√ |
√ |
× |
| !important |
× |
√ |
√ |
| _ |
√ |
× |
× |
下面給出一個執行個體,在google,Firefox下顯示紅色,IE7顯示綠色,IE6顯示藍色的執行個體。
<head> <title>CSS_Hack測試</title> <style type="text/css"> .div1{ background-color:Red; *background-color:Green; _background-color:blue; width:200px;height:200px; } </style></head><body> <div class="div1"> google:紅色,IE7:綠色,IE6:藍色 </div></body>
改代碼最終顯示效果為
一般來說,書寫的順序都是firefox的寫在前面,IE7的寫在中間,IE6的寫在最後面。
這樣對於IE7來說,寫在後面的優先順序高,能夠覆蓋原本給所有瀏覽器看的。而對於IE6來說,由於寫在後面的優先順序較高,能夠覆蓋原本寫給IE7和IE6共同看的。而又不會影響到IE7。
!important的作用,且看下面這行代碼
.div1{ background-color:blue; } 標記為重要,因此IE7與Firefox都會認為其優先順序較高,從而忽略後面的"background-color:blue;" 而對於IE6來說,因此識別不了!important,因此直接跳過改行代碼,顯示為藍色。
IE8 IE9 Hack
\9 僅僅IE6789支援,常常用於區分IE與Firefox
\0 僅僅IE8 IE9都支援
\9\0 僅僅IE9支援
如以下代碼,在Firefox,google顯示紅色,IE8顯示藍色,IE9顯示color:Fuchsia色,IE7顯示color:Aqua色,IE6顯示黑色。
其實方法都一樣,共同支援的寫在前面,單獨支援的寫在後面,以覆蓋共同支援的。
.div1{ background-color:blue\0; /* ie 8/9*/ background-color:Fuchsia\9\0; *background-color:Aqua; _background-color:Black }
二、選取器Hack
*+html 與 *html 是IE特有的標籤, firefox 暫不支援.而*+html 又為 IE7特有標籤.
<head> <title>CSS_Hack測試</title> <style type="text/css"> .div1 { background-color:red; } /* FireFox */ *html .div1 { background-color:blue; } /* ie6 fixed */ *+html .div1 { background-color:green; } /* ie7 fixed, 注意順序 */ </style></head><body> <div class="div1"> google:紅色,IE7:綠色,IE6:藍色 </div></body>
以上代碼可以理解為:先設定全部瀏覽器為紅色,在設定IE瀏覽器為藍色,在設定IE7為綠色。
注意:
*+html 對IE7的HACK 必須保證HTML頂部有如下聲明:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
CSS Hack還是盡量少用,我看別人的網站一般都看不到Hack代碼。頁面配置如果合理,基本上是不用寫Hack的。
一些方法總結:
浮動在IE6裡產生的雙倍距離問題,可以加個display:inline;來解決
.div1{float:left; width:100px; margin-left:100px;}
例如這段代碼,在IE6裡 而在google裡
三、HTML頭部引入Hack
<!--[if !IE]><!--> 除IE外都可識別 <!--<![endif]-->
<!--[if IE]> 所有的IE可識別 <![endif]-->
<!--[if IE 6]> 僅IE6可識別 <![endif]-->
<!--[if lt IE 6]> IE6以下版本可識別 <![endif]-->
<!--[if gte IE 6]> IE6以及IE6以上版本可識別 <![endif]-->
<!--[if IE 7]> 僅IE7可識別 <![endif]-->
<!--[if lt IE 7]> IE7以下版本可識別 <![endif]-->
<!--[if gte IE 7]> IE7以及IE7以上版本可識別 <![endif]-->
<!--[if IE 8]> 僅IE8可識別 <![endif]-->
<!--[if IE 9]> 僅IE9可識別 <![endif]-->
執行個體根據不同的IE執行個體引入不同的CSS檔案。
<head> <title></title> <!--[if IE 6]> <link href="test.css" rel="stylesheet" type="text/css" /> <![endif]--> <!--[if IE 7]> <link href="test1.css" rel="stylesheet" type="text/css" /> <![endif]--></head><body> <div class="div1"> <!--[if IE 6]> 我是IE6瀏覽器 <![endif]--> <!--[if IE 7]> 我是IE7瀏覽器 <![endif]--> </div></body>
IE6顯示效果為: IE7顯示效果為:
此頭部Hack能夠僅僅在希望輸出的瀏覽器輸出代碼。例如上例中,根據不容的瀏覽器引入不同的CSS檔案,根據不同的瀏覽器顯示不同的文字等等。