27

朋友,请帮我定义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; }
4

8 回答 8

34

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

即 6

* html .ie6 {property:value;}

或者

.ie6 { _property:value;}

即 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;}

即 6、7 和 8

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

即 8

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

或者

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

仅限 IE 8 标准模式

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

即 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 在页面加载时快速运行以检测功能;然后它创建一个带有结果的 JavaScript 对象,并将类添加到 html 元素

用户代理选择

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;
}

脚注

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


归因/基本阅读

于 2015-01-07T08:41:00.477 回答
24

您可以在 CSS 样式前面加上

:root

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

:root #element { color:pink \0/IE9; }  /* IE9 */
于 2011-12-09T15:25:17.563 回答
19

使用IE 条件注释

<!--[if ie 9]>
    your stuff here
<![endif]-->
于 2011-09-09T16:50:05.707 回答
8

\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 中

于 2014-06-16T11:41:18.493 回答
2

我认为你可以做同样的事情,就像你想为 IE6 编写特定代码但说 IE9 一样:)

<!--[if IE 9]>
Special instructions for IE 9 here
<![endif]-->
于 2011-09-09T16:50:59.967 回答
1

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

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

这意味着该方法是使用新的 CSS 文件而不是类中的 hack,这保证了 CSS 是有效的。

于 2011-09-09T16:51:07.313 回答
0

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

margin-right: -15px\9; /* This fails */
margin-right: ~"-18px\9"; /* This passes */
于 2018-01-08T12:37:52.937 回答
-6

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

于 2011-09-09T18:12:34.033 回答