在样式表中使用IE条件注释 [英] Using IE conditional comments inside a stylesheet

查看:93
本文介绍了在样式表中使用IE条件注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道您可以在HTML内使用IE条件注释:

I know that you can use an IE conditional comment inside HTML:

<!--[if IE]>
  <link href="ieOnlyStylesheet.css" />
<![endif]-->

但是如果我只想定位样式表中的IE,该怎么办?在html中为特定元素设置CSS规则。例如,您可以在css文件中使用此Chrome / Safari hack 作为css代码...

But what if I want to target only IE in a stylesheet, setting up a css rule for a specific element inside the html. For example, you can use this Chrome/Safari hack inside the css file as css code...

@media screen and (-webkit-min-device-pixel-ratio:0) {
    .myClass{
        background: #fff;
        background:rgba(255,0,255,0.7);
    }
}

但是在CSS样式表中使用IE hack就像这样:

But using the IE hack inside the CSS stylesheet like this:

 <!--[if IE]>
      .myClass{
        background: #fff;
        background:rgba(255,0,255,0.7);
    }
 <![endif]-->

不起作用。我如何在样式表内使用 来仅定位IE?

does not work. What do I use inside a stylesheet to target IE only?

推荐答案

条件注释在此范围内不起作用样式表。相反,您可以在HTML中使用条件注释,以将不同的CSS类或ID应用于可以使用CSS定位的元素。

Conditional comments do not work within stylesheets. Instead, you can use conditional comments in your HTML to apply different CSS classes or IDs to elements that you can then target with CSS.

例如:

<!--[if IE]>
  <div id="wrapper" class="ie">
<![endif]-->
<!--[if !IE]>
  <div id="wrapper">
<![endif]-->

此外,还有诸如 Modernizr 以非常相似的方式进行功能检测(通过向< html> 元素添加类)。您可以使用它来逐步增强新浏览器的设计/脚本,同时仍支持旧浏览器。

Also, there are tools such as Modernizr that do feature detection in a very similar way (by adding classes to the <html> element). You can use it to progressively enhance your design/script for newer browsers while still supporting older browsers.

这篇关于在样式表中使用IE条件注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆