瀏覽器安全色 IE6、IE7、IE8、Firefox、Opera CSS hack區分

來源:互聯網
上載者:User

小知識:什麼是CSS hack?

  由於不同的瀏覽器,比如IE6、IE7、IE8、Firefox等,對CSS的解析認識不一樣,因此會導致產生的頁面效果不一樣,得不到我們所需要的頁面效果。

  這個時候我們就需要針對不同的瀏覽器去寫不同的CSS,讓它能夠同時相容不同的瀏覽器,能在不同的瀏覽器中也能得到我們想要的頁面效果。

  這個針對不同的瀏覽器寫不同的CSS code的過程,就叫CSS hack,也叫寫CSS hack。

針對樣式名

如果只讓ie6看見用*html .head{color:#000;} 如果只讓ie7看見用*+html .head{color:#000;} 如果只讓ff看見用:root body .head{color:#000;} 如果只讓ff、IE8看見用html>/**/body .head{color:#000;} 如果只是不讓ie6看見用html>body .head{color:#000;} 即對IE 6無效 如果只是不讓ff、IE8看見用*body .head{color:#000;}    即對ff、IE8無效

針對具體屬性

如果只讓ie6看見用_     .head{_color:#000;} 如果只讓ie7看見用+與_結合的方法:     .head{+color:#f00;!;_color:#000;}

IE8正式版hack

\9″ 例:”margin:0px auto\9;”.這裡的”\9″可以區別所有IE8和FireFox. “*” IE6、IE7可以識別.IE8、FireFox不能. “_” IE6可以識別”_”,IE7、IE8、FireFox不能. 如: .a {color:#f00;     color:#f60\9;    +color:#00FF00;    _color:#0000FF; } 從左至右分別對應 FF,IE8 IE7 IE6

-------------------------------------------------

各瀏覽器CSS hack相容表:

  IE6 IE7 IE8 Firefox Chrome Safari
!important   Y   Y    
_ Y          
* Y Y        
*+   Y        
\9 Y Y Y      
\0     Y      
nth-of-type(1)         Y Y

程式碼範例:

#test{ color:red; /* 所有瀏覽器都支援 */ color:red !important;/* Firefox、IE7支援 */ _color:red; /* IE6支援 */ *color:red; /* IE6、IE7支援 */ *+color:red; /* IE7支援 */ color:red\9; /* IE6、IE7、IE8支援 */ color:red\0; /* IE8支援 */ }

body:nth-of-type(1) p{color:red;} /* Chrome、Safari支援 */

整體測試程式碼範例:

.test{ color:#000000; color:#0000FF\0; [color:#00FF00; *color:#FFFF00; _color:#FF0000; }

其他說明:

1、如果你的頁面對IE7相容沒有問題,又不想大量修改現有代碼,同時又能在IE8中正常使用,微軟聲稱,開發商僅需要在目前相容IE7的網站上添加一行代碼即可解決問題,此代碼如下: <meta http-equiv="x-ua-compatible" content="ie=7" />

2、body:nth-of-type(1) 如果這樣寫,表示全域尋找body,將會對應第一個<body>。

3、還有其他寫法,比如: *html #test{}或者 *+html #test{}

4、*+html 對IE7的hack 必須保證HTML頂部有如下聲明: http://www.w3.org/TR/html4/loose.dtd

5、順序:Firefox、IE8、IE7、IE6依次排列。

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.