今天被在修改CSS的时候,突然碰到要单独Hack IE8的。当然,用注释非常方便,只要添加相应的注释就可以解决。但问题是,为了一句CSS写多一个文件,或者在header上添加注释,那显然不是懒人的习惯做法。结论如下:
selector{ property:value; /* 所有浏览器 */ property:value\9; /* 所有IE浏览器 */ +property:value; /* IE7 */ _property:value; /* IE6 */ }
当然,注意顺序。根据CSS的优先性,上面的写法,分别针对Firefox、IE8、IE7和IE6显示值。让我们看看这个演示的CSS代码:
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5 <title>CSS Hack: 区分 IE6 / IE7 /IE8 /Firefox</title> 6 </head> 7 <style type="text/css" media="screen"> 8 p.ie{ 9 height:60px;text-align:center;line-height:60px;border:1px dashed #bbb;background:#f7f7f7;font:15; 10 color:blue; // 所有浏览器 11 color:brown\9; // 所有IE浏览器 12 +color:red; // IE7 13 _color:green; // IE6 14 } 15 </style> 16 <body> 17 <p class="ie"> 18 <span style="display:block;display:none\9;">嘿嘿,小子竟然也用Firefox,蓝色文字。</span> 19 <!--[if IE 8]>不错不错,挺先进的嘛,使用IE8呢!文字是褐色的。<![endif]--> 20 <!--[if IE 7]>你,IE7,红色文字!<![endif]--> 21 <!--[if IE 6]>孩子,虽然显示的是绿色文字,不过,IE6可不是好东西呢!<![endif]--> 22 </p> 23 </body> 24 </html>
对,就是IE条件注释+CSS的结果。顺路学一下IE条件注释吧。不用再举例了吧,一看就知道那个对那个了。