今天被在修改CSS的时候,突然碰到要单独Hack IE8的。当然,用注释非常方便,只要添加相应的注释就可以解决。但问题是,为了一句CSS写多一个文件,或者在header上添加注释,那显然不是懒人的习惯做法。结论如下:
selector{
property:value; /* 所有浏览器 */
property:value\9; /* 所有IE浏览器 */
*+property:value; /* IE7 */
_property:value; /* IE6 */
*property:value; /*IE6、IE7 共用*/
}
注意顺序。根据CSS的优先性,上面的写法,分别针对Firefox、IE8、IE7和IE6显示值。
p.ie
{
height : 60px ; text-align : center ; line-height : 60px ; border : 1px dashed #bbb ; background : #f7f7f7 ; font : 15 ;
color : blue ; // 所有浏览器
color : brown\9 ; // 所有IE浏览器
+color : red ; // IE7
_color : green ; // IE6
}
height : 60px ; text-align : center ; line-height : 60px ; border : 1px dashed #bbb ; background : #f7f7f7 ; font : 15 ;
color : blue ; // 所有浏览器
color : brown\9 ; // 所有IE浏览器
+color : red ; // IE7
_color : green ; // IE6
}
哈,事实就这么简单。你看到的是那一句话呢?如果你多个浏览器都测试了,就会看到,显示的文字和颜色是不同的。为什么?看看我的HTML中这个段落是这样写的:
<
p
class
="ie"
>
< span style ="display:block;display:none\9;" > 嘿嘿,小子竟然也用Firefox,蓝色文字。 </ span >
<!-- [if IE 8]>不错不错,挺先进的嘛,使用IE8呢!文字是褐色的。<![endif] -->
<!-- [if IE 7]>你,IE7,红色文字!<![endif] -->
<!-- [if IE 6]>孩子,虽然显示的是绿色文字,不过,IE6可不是好东西呢!<![endif] -->
</ p >
< span style ="display:block;display:none\9;" > 嘿嘿,小子竟然也用Firefox,蓝色文字。 </ span >
<!-- [if IE 8]>不错不错,挺先进的嘛,使用IE8呢!文字是褐色的。<![endif] -->
<!-- [if IE 7]>你,IE7,红色文字!<![endif] -->
<!-- [if IE 6]>孩子,虽然显示的是绿色文字,不过,IE6可不是好东西呢!<![endif] -->
</ p >
对,就是IE条件注释+CSS的结果。顺路学一下IE条件注释吧。不用再举例了吧,一看就知道那个对那个了。