如果浏览器是IE,则应用CSS规则

May*_*yur 37 css internet-explorer

可能重复:
如何在CSS中执行IE条件?

我如何才能将以下规则应用于IE?

.abc {

float:left;
height:0;
margin:0 10px;
width:0;

/*Apply these rules for IE only*/
position:absolute;
left:30;
top:-10;
/*Apply these rules for IE only*/
}
Run Code Online (Sandbox Code Playgroud)

Pek*_*ica 29

在IE9以及包括IE9在内的浏览器中,这是通过条件注释完成的.

<!--[if IE]>
<style type="text/css">
  IE specific CSS rules go here
</style>
<![endif]-->
Run Code Online (Sandbox Code Playgroud)

  • 这对IE10及更高版本无效. (17认同)
  • 这不再是答案了 (2认同)

DAS*_*RiD 23

避免加载多个CSS文件或使用内联CSS的好方法是根据Internet Explorer的版本将类交给body标记.如果你只需要一般的IE黑客,你可以做这样的事情,但它可以扩展为特定于版本:

<!--[if IE ]><body class="ie"><![endif]-->
<!--[if !IE]>--><body><!--<![endif]-->
Run Code Online (Sandbox Code Playgroud)

现在在你的css代码中,你可以简单地做:

.ie .abc {
  position:absolute;
  left:30;
  top:-10;
}
Run Code Online (Sandbox Code Playgroud)

这也使您的CSS文件有效,因为您不必使用脏(和无效)CSS黑客.

  • Jon:IE10不再支持条件评论了.另一方面,你也不应该再需要它们了. (2认同)

Joh*_*Del 18

一种快速的方法是使用以下内容,即你想要聚焦(检查注释),在你的css文件里面(margin-top,设置你喜欢的任何css属性):

margin-top: 10px\9; /*It will apply to all ie from 8 and below */
*margin-top: 10px; /*It will apply to ie 7 and below */
_margin-top: 10px; /*It will apply to ie 6 and below*/
Run Code Online (Sandbox Code Playgroud)

更好的方法是检查用户代理或条件if,以避免在其他浏览器中加载不必要的CSS.

  • \ 9适用于9和10 (3认同)
  • 试试我的ie10 + hack _: - ms-fullscreen,:root .selector {margin-top:10px; 和我的ie11 + hack:_: - ms-lang(x),.selector {margin-top:10px; 他们应该帮助你.在这里测试它们:http://browserstrangeness.bitbucket.org/css_hacks.html#msie (3认同)

小智 5

如前所述,我更喜欢使用单独的文件来表示规则.

<!--[if IE]><link rel="stylesheet" type="text/css" href="ie-style.css"/><![endif]-->
Run Code Online (Sandbox Code Playgroud)

在其中,您可以为不同版本设置规则,即使用:

.abc {...} /* ALL MSIE */
*html *.abc {...} /* MSIE 6 */
*:first-child+html .abc {...} /* MSIE 7 */
Run Code Online (Sandbox Code Playgroud)