目标IE9只能通过CSS

只是想知道这些IE浏览器的窍门

"\9" - for IE8 and below. "*" - for IE7 and below. "_" - for IE6. 

即如

 body { border:2px solid blue; border:2px solid yellow \9; *border:2px solid green; _border:2px solid orange; } 

是否有人有这样的黑客入侵IE9? 即我试图只通过CSS目标IE9?

我build议使用condcoms来喂养一个IE9的CSS文件或有一个条件的HTML类,类似于:

 <!--[if lt IE 7]> <html lang="en-us" class="no-js ie6"> <![endif]--> <!--[if IE 7]> <html lang="en-us" class="no-js ie7"> <![endif]--> <!--[if IE 8]> <html lang="en-us" class="no-js ie8"> <![endif]--> <!--[if gt IE 8]><!--> <html lang="en-us" class="no-js"> <!--<![endif]--> 

可怕的,但它应该工作:

 body { border:2px solid blue; border:2px solid yellow \9; *border:2px solid green; _border:2px solid orange; } body:nth-child(n) {border:1px solid purple \9; /*Should target IE9 only - not fully tested.*/} 

在这个地址: http : //www.impressivewebs.com/ie10-css-hacks/我发现一个特定于IE10的媒体查询(以下):

 @media screen and (-ms-high-contrast: active), (-ms-high-contrast: none) { /* IE10-specific styles go here */ } 

正如在一些评论中指出的那样,在某些情况下,有条件的HTML将无法工作,特别是如果您无法修改页面代码本身。 所以这是一个解决方法:

基地样式

 .test{color:red;} 

特定于浏览器的覆盖

IE <8: html >/**/body .test { color: green; } html >/**/body .test { color: green; }
IE 9:: :root .test{color:green \ ;}
IE 8和9: .test{color:green \ ;}
IE 9和Opera :root .test {color: green\0;}

笔记

以上将不适用于backgroundfont-* ,任何\0\9黑客通常是不稳定的。 有关CSS黑客的完整列表,请参阅http://mynthon.net/howto/-/webdev/CSS-big-list-of-css-hacks.txt

IE9相当符合标准。 你不应该需要破解它。

另外,您应该使用IE条件注释来加载不同的样式。 对于IE 9,你可以这样做:

 <!--[if IE 9]> <!-- conditional content goes here --> <![endif]-->