【问题标题】:How to define specific CSS rules for IE9 alone?如何单独为 IE9 定义特定的 CSS 规则?
【发布时间】:2011-11-13 23:02:32
【问题描述】:

朋友,请帮我定义IE9的特定css规则? 比如这样的

/* IE 6 fix */
* html .twit-post .delete_note a { background-position-y: 2px; }
* html .twit-post .delete_note a:hover { background-position-y: -14px; }

【问题讨论】:

  • 我没有给你答案,但你真的不需要 ie9 特定的 CSS hack。这是一个兼容的浏览器,应该像其他浏览器一样编写。
  • @Grillz :它可能是一个投诉浏览器。但是我们不能告诉用户放弃这个站点的 IE 并为这个站点下载并使用另一个。
  • 合规。形容词:倾向于同意他人或遵守规则,尤其是。过度;默许。我的意思是,随着开发的进行,IE9 与 safari 和 firefox 相同。不需要针对它的样式。下面的人有你的答案,所以你应该标记一个接受。
  • 此兼容浏览器不支持最小高度。
  • 这里有点晚了,但@Gregg 说 IE9 是兼容的浏览器是一个非常大胆的声明。我宁愿完全支持 TOs 错字;-)

标签: css internet-explorer-9 css-hack


【解决方案1】:

使用IE conditional comments

<!--[if ie 9]>
    your stuff here
<![endif]-->

【讨论】:

  • 对于任何其他复制粘贴者,您可能希望跳过
【解决方案2】:

我认为你可以这样做,就像你想为 IE6 编写特定代码但改用 IE9 :)

<!--[if IE 9]>
Special instructions for IE 9 here
<![endif]-->

【讨论】:

    【解决方案3】:

    使用条件 CSS: (将代码放在 html 上的 &lt;head&gt; 上方,IE9 会读取那个额外的 CSS 文件)

    <!--[if (gte IE 9)|!(IE)]><!-->
    place the link to the CSS file here
    <![endif]-->
    

    这意味着该方法是使用新的 CSS 文件而不是在类中修改,这样可以保证 CSS 是有效的。

    【讨论】:

      【解决方案4】:

      您不需要针对 IE9。它能够处理现代 css,不应该被黑客入侵。这是一种过时的开发方法。

      【讨论】:

      • 当然,您应该先创建一个 CSS 标准网站,不要进行任何黑客攻击。然后用IE9打开。有时可能还好。但很多时候它不会,你需要破解它。
      • 对微软说这个
      • 是的 - 我看到了 IE9 特有的错误 - IE9 的 32 位窗口和 64 位窗口之间也存在一些差异。
      • 作为评论会更好
      【解决方案5】:

      您可以在 CSS 样式前添加

      :root
      

      使其特定于 IE9,如下所示:

      :root #element { color:pink \0/IE9; }  /* IE9 */
      

      【讨论】:

      • 请注意,虽然这是有效的 CSS,但它不适用于 less.css。
      • 还应该注意最重要的部分是'\0/IE9'位。仅在开头添加 ':root' 并不能使其仅限于 IE9。至少,Chrome 仍然选择以 ':root' 为前缀的选择器。可能还有其他浏览器。
      • 这不仅仅是IE9。它也兼容 IE10。 (提出问题时不可用)
      【解决方案6】:

      \9 是 Internet Explorer 特有的“CSS hack”。

      这只是意味着 CSS 的一个特定行以 \9 结尾;

      在你的例子中, 如果你的 CSS 看起来像这样......

      html .twit-post .delete_note a 
      { 
      background-position-y: 2px\9; 
      
      }
      html .twit-post .delete_note a:hover 
      { 
       background-position-y: -14px\9;
       }
      

      结果是 background-position-y: -14px;在 IE 9 中

      【讨论】:

        【解决方案7】:

        请注意,接受的答案也针对 IE10。因此,要获得更完整的列表:

        IE 6

        * html .ie6 {property:value;}
        

        .ie6 { _property:value;}
        

        IE 7

        *+html .ie7 {property:value;}
        

        *:first-child+html .ie7 {property:value;}
        

        IE 6 和 7

        @media screen\9 {
            .ie67 {property:value;}
        }
        

        .ie67 { *property:value;}
        

        .ie67 { #property:value;}
        

        IE 6、7 和 8

        @media \0screen\,screen\9 {
            .ie678 {property:value;}
        }
        

        IE 8

        html>/**/body .ie8 {property:value;}
        

        @media \0screen {
            .ie8 {property:value;}
        }
        

        仅限 IE 8 标准模式

        .ie8 { property /*\**/: value\9 }
        

        IE 8,9 和 10

        @media screen\0 {
            .ie8910 {property:value;}
        }
        

        仅限 IE 9

        @media screen and (min-width:0) and (min-resolution: .001dpcm) { 
         // IE9 CSS
         .ie9{property:value;}
        }
        

        IE 9 及以上

        @media screen and (min-width:0) and (min-resolution: +72dpi) {
          // IE9+ CSS
          .ie9up{property:value;}
        }
        

        IE 9 和 10

        @media screen and (min-width:0) {
            .ie910{property:value;}
        }
        

        仅限 IE 10

        _:-ms-lang(x), .ie10 { property:value\9; }
        

        IE 10 及以上

        _:-ms-lang(x), .ie10up { property:value; }
        

        @media all and (-ms-high-contrast: none), (-ms-high-contrast: active) {
           .ie10up{property:value;}
        }
        

        IE 11(及更高版本..)

        _:-ms-fullscreen, :root .ie11up { property:value; }
        

        Javascript 替代品

        Modernizr

        Modernizr 在页面加载时快速运行以检测功能;然后 使用结果创建一个 JavaScript 对象,并将类添加到 html元素

        User agent selection

        Javascript:

        var b = document.documentElement;
                b.setAttribute('data-useragent',  navigator.userAgent);
                b.setAttribute('data-platform', navigator.platform );
                b.className += ((!!('ontouchstart' in window) || !!('onmsgesturechange' in window))?' touch':'');
        

        将以下内容添加(例如)html 元素:

        data-useragent='Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C)'
        data-platform='Win32'
        

        允许非常有针对性的 CSS 选择器,例如:

        html[data-useragent*='Chrome/13.0'] .nav{
            background:url(img/radial_grad.png) center bottom no-repeat;
        }
        

        脚注

        如果可能,请避免浏览器定位。识别并修复您发现的任何问题。支持progressive enhancementgraceful degradation。考虑到这一点,这是一个“理想世界”场景,并非总是可以在生产环境中获得,因此,以上内容应该有助于提供一些不错的选择。


        署名/必读

        【讨论】:

        • 也不需要将来可能会崩溃的黑客攻击-因此是更稳定的选择
        • Chrome 为我选择了仅限 IE9 的样式,即:@media screen and (min-width:0) and (min-resolution: +72dpi) — Chrome 查找并使用其中的样式。 :- ( "(Up)vote locked" 虽然答案不再有效,但在当前版本的 IE 和 Chrome 中
        • @KajMagnus nb。答案与 IE 上下文中的定位有关,而不是其他浏览器(可能会以不同的方式解释某些规则,如 Chrome 的情况) - 如果我没有最大化字数,我会在问题中澄清,道歉
        • 这是我见过的最好的 StackOverflow 答案之一。谢谢!
        • 由于这些答案假设用户正在运行 IE,因此需要基于用户代理的 JS 解决方案。因此,该解决方案也可以用来完成整个任务。
        【解决方案8】:

        我发现在某些情况下使用负值(当使用编译器编译 LESS 文件时)使用:

        margin-right: -15px\9; /* This fails */
        margin-right: ~"-18px\9"; /* This passes */
        

        【讨论】:

        • 它与 OP 询问有关吗? IE9 的特定 CSS 规则?
        • @user7294900,我不确定你的意思。你能澄清你的问题吗:)
        • 原问题是如何为IE9指定css规则,你的回答在这里无关
        • 我发现这种技术对我有用,我正在尝试专门为 IE9 编写样式,而我正忙于编译 LESS 文件的项目。所以我认为这可以帮助那些想要尝试并实现我正在做的事情的人。
        猜你喜欢
        • 2016-10-31
        • 2021-10-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-05
        相关资源
        最近更新 更多