【转】IE6、IE7、IE8、Firefox、Opera、谷歌浏览器、Safari CSS hack区分,浏览器兼容性

2011-12-11 14:24
转载自 myidealtwo
最终编辑 myidealtwo

针对样式名

如果只让ie6看见用*html .head{color:#000;}
如果只让ie7看见用*+html .head{color:#000;}
如果只让ff看见用:root body .head{color:#000;}
如果只让ffIE8看见用html>/**/body .head{color:#000;}
如果只是不让ie6看见用html>body .head{color:#000;} 即对IE 6无效
如果只是不让ffIE8看见用*body .head{color:#000;} 即对ffIE8无效

谷歌浏览器、Safari
@media screen and (-webkit-min-device-pixel-ratio:0) {
其中写入要兼容的样式.denglu table{color:#424242;margin-top:0px;}
}

针对具体属性

如果只让ie6看见用_      .head{_color:#000;}
如果只让ie7看见用+_结合的方法:      .head{+color:#f00;!;_color:#000;}

IE8正式版hack

\9″:”margin:0px auto\9;”.这里的”\9″可以区别所有IE8FireFox.
“*”IE6IE7可以识别.IE8FireFox不能.
“_”IE6可以识别”_”,IE7IE8FireFox不能.
如:
.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;/* FirefoxIE7支持 */
_color:red; /* IE6支持 */
*color:red; /* IE6IE7支持 */
*+color:red; /* IE7支持 */
color:red\9; /* IE6IE7IE8支持 */
color:red\0; /* IE8支持 */
}

body:nth-of-type(1) p{color:red;} /* ChromeSafari支持 */

整体测试代码示例:

.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″ />

2body:nth-of-type(1) 如果这样写,表示全局查找body,将会对应第一个<body>

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

4*+html IE7hack 必须保证HTML顶部有如下声明:

http://www.w3.org/TR/html4/loose.dtd

5、顺序:FirefoxIE8IE7IE6依次排列。

小知识:什么是CSS hack

由于不同的浏览器,比如IE6IE7IE8Firefox等,对CSS的解析认识不一样,因此会导致生成的页面效果不一样,得不到我们所需要的页面效果。

这个时候我们就需要针对不同的浏览器去写不同的CSS,让它能够同时兼容不同的浏览器,能在不同的浏览器中也能得到我们想要的页面效果。

这个针对不同的浏览器写不同的CSS code的过程,就叫CSS hack,也叫写CSS hack

比如 IE6能识别下划线_和星号*IE7能识别星号*,当不能识别下划线_,而firefox两个都不能认识。等

书写顺序,一般是将识别能力强的浏览器的CSS写在后面。下面如何写里面说得更详细些。

如何写CSS Hack

比如要分辨IE6firefox两种浏览器,可以这样写:

<style>

div{

background:green; /* for firefox */

*background:red;    /* for IE6 */

}    _background:red;    /* for IE6 */

</style>

<div>我在IE6中看到是红色的,在firefox中看到是绿色的。</div>

解释一下:

上面的cssfirefox中,它是认识不了后面的那个带星号*的东东是什么的,于是将它过滤掉,不予理睬,解析得到的结果是:div{background:green},于是理所当然这个div的背景是绿色的。

IE6中呢,它两个background都能识别出来,它解析得到的结果是:div{background:green;background:red;},于是根据优先级别,处在后面的red的优先级高,于是当然这个div的背景颜色就是红色的了。

CSS hack:区分IE6IE7firefox

区别不同浏览器,CSS hack写法:

区别IE6FF

background:orange;*background:blue;

区别IE6IE7

background:green !important;background:blue;

区别IE7FF

background:orange; *background:green;

区别FFIE7IE6

background:orange;*background:green !important;*background:blue;

注:IE都能识别*;标准浏览器(FF)不能识别*

IE6能识别*,但不能识别 !important,

IE7能识别*,也能识别!important;

FF不能识别*,但能识别!important;

IE6 IE7 FF

* √ √ ×

!important × √ √

另外再补充一个,下划线“_”,

IE6支持下划线,IE7firefox均不支持下划线。

于是大家还可以这样来区分IE6IE7firefox

: background:orange;*background:green;_background:blue;

注:不管是什么方法,书写的顺序都是firefox的写在前面,IE7的写在中间,IE6的写在最后面

IE专用的条件注释

<!-- 其他浏览器 -->
<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]-->
 
posted @ 2011-12-12 11:32  julia2011  阅读(200)  评论(0)    收藏  举报