CSS |仅将样式应用于Internet Explorer

时间:2015-04-29 10:15:54

标签: css internet-explorer

目前我正在Internet Explorer 10和11上测试该网站。

目前我有一个样式表,其中有一些样式,只适用于访问者使用Internet Explorer浏览网站时。我尝试使用条件注释,但Internet Explorer 10和11不支持它们。

我想知道是否有人之前处理过这个问题并且可能会让我朝着正确的方向前进。非常感谢所有帮助。

4 个答案:

答案 0 :(得分:0)

以下是IE10和IE11的代码。它会将类添加到html标记。

<!--[if !IE]><!-->
<script>
if (/*@cc_on!@*/false) {  
    document.documentElement.className+=' ie10';  
}  
</script>
<!--<![endif]--> 

<script>
var ua = navigator.userAgent,
      doc = document.documentElement;
if((ua.match(/rv:11.0/i))){
    doc.className = doc.className + " ie11";
  }
</script>

答案 1 :(得分:0)

不再支持条件评论

在Internet Explorer 10标准和怪癖模式中删除了对条件注释的支持,以提高互操作性并符合HTML5。

This Applies to Internet Explorer 10 and later.

了解详情 Read here

答案 2 :(得分:0)

将用户代理添加到html中,如下所示:

var doc = document.documentElement;
doc.setAttribute('data-useragent', navigator.userAgent);

IE10的用户代理是:

Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)

这将导致:

<html data-useragent="Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)">

然后你可以风格化:

html[data-useragent*='MSIE 10.0'] h1 {
 color: blue;
}

所以标题在IE10中只是蓝色。

答案 3 :(得分:0)

@media screen and (-ms-high-contrast: active), (-ms-high-contrast: none) {
  #myClass { color: red; }
}

请参阅CSS hacks http://www.paulirish.com/2009/browser-specific-css-hacks/