比如要分辨IE6和firefox两种浏览器,可以这样写:
1 2 3 4 | div{ background : green ; /* for firefox */ * background : red ; /* for IE6 */ ( both IE 6 && IE 7 ) } |
我在IE6中看到是红色的,在firefox中看到是绿色的。
解释一下:
上面的css在firefox中,它是认识不了后面的那个带星号的东西是什么的,于是将它过滤掉,不予理睬,解析得到的结果是:div{background:green},于是理所当然这个div的背景是绿色的。
在IE6中呢,它两个background都能识别出来,它解析得到的结果是:div{background:green;background:red;},于是根据优先级别,处在后面的red的优先级高,于是当然这个div的背景颜色就是红色的了。
CSS hack:区分IE6,IE7,firefox
区别不同浏览器,CSS hack写法:
区别IE6与FF:
1 | background :orange;* background : blue ; |
区别IE6与IE7:
1 | background : green !important ; background : blue ; |
区别IE7与FF:
1 | background :orange; * background : green ; |
区别FF,IE7,IE6:
1 2 | background :orange;* background : green ; _background : blue ; background :orange;* background : green !important ;* background : blue ; |
注:IE都能识别*;标准浏览器(如FF)不能识别*;
IE6能识别*;不能识别 !important;
IE7能识别*,能识别!important;
FF不能识别*,但能识别!important;
IE6 | IE7 | FireFox | |
* | √ | √ | × |
!important | × | √ | √ |
浏览器优先级别:FF<IE7<IE6,CSS hack书写顺序一般为FF IE7 IE6
以: " #demo {width:100px;} "为例;
#demo {width:100px;} /*被FIREFOX,IE6,IE7执行.*/
* html #demo {width:120px;} /*会被IE6执行,之前的定义会被后来的覆盖,所以#demo的宽度在IE6就为120px; */
*+html #demo {width:130px;} /*会被IE7执行*/
所以最后,#demo的宽度在三个浏览器的解释为: FIREFOX:100px; ie6:120px; ie7:130px;
IE8 最新css hack:
"\9" 例:"border:1px \9;".这里的"\9"可以区别所有IE和FireFox.
"\0" IE8识别,IE6、IE7不能.
"*" IE6、IE7可以识别.IE8、FireFox不能.
"_" IE6可以识别"_",IE7、IE8、FireFox不能.
IE6 hack
1 | _background-color : #CDCDCD ; /* ie 6*/ |