解决IE6、IE7、Firefox兼容最简单的CSS Hack

作者:我就是个世界 发表于:2011-12-30


------------------------------------------ [color=#0000FF]2012年5月30日 更新[/color] --------------------------------------------------------------

区别IE6与FF:

background:orange;*background:blue;

区别IE6与IE7:

background:green !important;background:blue;

区别IE7与FF:

background:orange; *background:green;

区别FF,IE7,IE6:

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

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

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

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

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

另外再补充一个,下划线"_",
IE6支持下划线,IE7和firefox均不支持下划线。

于是大家还可以这样来区分IE6,IE7,firefox:

[code]background:orange;*background:green;_background:blue;[/code]

"/9" 例:"margin:0px auto/9;".这里的"/9"可以区别所有IE和FireFox.
"*" IE6、IE7可以识别.IE8、FireFox不能.
"_" IE6可以识别"_",IE7、IE8、FireFox不能.

如此,就可以完全区分开IE6、IE7、IE8、FireFox了.

---------------------------------------------------------------------------------------------------
[code]
background: red;       /* 对FF Opera和Safari有效 */
#background: blue;      /* 对 IE6 和 IE7有效 */
_background: green;      /* 只对IE6有效 */
/*/background: orange;*/      /** 只对IE8有效 **/

!important         /*FF、IE7有效*/

*         /*IE都有效*/
[/code]
============================================================

IE8是可以和IE7兼容的,简单一行代码,让IE8自动调用IE7的渲染模式
只需要在页面中加入如下HTTP meta-tag:
[code]<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />[/code]
只要IE8读到这个标签,它就会自动启动IE7兼容模式,保证页面完整展示。


[b][color=#FF0000]注:不管是什么方法,书写的顺序都是firefox的写在前面,IE7的写在中间,IE6的写在最后面。[/color][/b]

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

具体写法很容易:
[code]
#someNode
{
    position: fixed;
   #position: fixed;
   _position: fixed;
}
[/code]
    第一排给Firefox以及其他浏览器看
    第二排给IE7(可能以后的IE8、IE9也是如此,谁知道呢)看
    第三排给IE6以及更老的版本看


最好的应用就是可以让IE6也“支持”position:fixed,而且,配合这个原理,可以做到不引入JavaScript代码(仅用IE6的expression),我这里有一个现成的页面,CSS如下写:
[code]
#ff-r
{
position:  fixed;
_position:  absolute;
right:     15px;
top:       15px;
top:       expression(eval(document.compatMode &&
           document.compatMode=='CSS1Compat') ?
           documentElement.scrollTop+15 :
           document.body.scrollTop +
           (document.body.clientHeight
           -this.clientHeight));
}
[/code]
是不是很方便:)
来源:aw's blog http://www.awflasher.com/blog/archives/1080
版权声明

未经许可,不得转载。