27

对于 Internet Explorer 8 (IE8),我想使用以下 CSS:

color : green;

我想应用一个只影响 IE8 的 hack,而不是 IE9、IE6 和 7。

4

11 回答 11

44

在 HTML 中使用条件注释,如下所示:

<!--[if IE 8]>
<style>...</style>
<![endif]-->

见这里:http ://www.quirksmode.org/css/condcom.html

您可以可靠地测试 IE 版本,并确保不会混淆其他浏览器。

于 2010-06-15T06:57:19.863 回答
35

使用媒体查询来分隔每个浏览器:

/* IE6/7 uses media, */
@media, { 
        .dude { color: green; } 
        .gal { color: red; }
} 

/* IE8 uses \0 */
@media all\0 { 
        .dude { color: brown; } 
        .gal { color: orange; }
} 

/* IE9 uses \9 */
@media all and (monochrome:0) { 
          .dude { color: yellow\9; } 
          .gal { color: blue\9; }
} 

/* IE10 and IE11 both use -ms-high-contrast */
@media all and (-ms-high-contrast:none)
 {
 .foo { color: green } /* IE10 */
 *::-ms-backdrop, .foo { color: red } /* IE11 */
 }

参考

于 2012-01-11T01:03:02.323 回答
15

使用\0.

color: green\0;

但是,我确实建议使用条件注释,因为您也想排除 IE9,而且目前还无法预测这种 hack 是否也会影响 IE9。

无论如何,我从来不需要 IE8 特定的 hack。它是什么,您想要解决的 IE8 特定问题?它是否以 IE8 标准模式呈现?它的渲染器非常好。

于 2010-06-16T01:44:59.723 回答
8

如果需要对 CSS 进行小的更改,请使用\9它针对 IE8 及以下版本(IE6、IE7、IE8)

.element { 
   color:green \9;
 }

最好的方法是在 HTML 中使用条件注释,如下所示:

<!--[if IE 8]>
<style>...</style>
<![endif]-->
于 2014-06-30T10:54:08.503 回答
2

做这样的事情应该对你有用。

<!--[if IE 8]> 
<div id="IE8">
<![endif]--> 

*Your content* 

<!--[if IE 8]> 
</div>
<![endif]--> 

然后你的 CSS 可以如下所示:

#IE8 div.something
{ 
    color: purple; 
}
于 2010-12-06T18:25:16.280 回答
2

所以最近的一个问题促使我注意到一个选择器集黑客用于排除 IE 8 。

.selector, #excludeIE8::before {}将导致 IE 8 丢弃整个选择器集,而 5-7 和 9-11 会很好地读取它。任何::选择器 ( ::first-line, ::before, ::first-letter, ::selection) 都可以工作,我只是选择::before了这样一行才能准确读取。请注意,假::before选择器的目标假的,因此如果您确实有一个带有 ID 的元素,请务必将其更改为其他内容excludeIE8

有趣的是,在现代浏览器(FF 45-52、GC 49-57、Edge 25/13)中,一个糟糕::的选择器会吃掉整个选择器集(dabblet 演示)。似乎最后一个 Windows 版本的 Safari(和 LTE IE 7,lol)在仍然理解的同时没有这种行为::before此外,我在规范中找不到任何表明这是预期行为的内容,并且因为它会导致任何包含以下内容的选择器损坏:::future-legitimate-pseudoelement...我倾向于说这是一个错误-并且会在未来蚕食我们的后腿。


但是,如果您只想要属性级别(而不是规则级别)的东西,上面的 Ziga 通过附加 \9(空格是关键;不要复制粘贴该内联,因为它使用 nbsp):

/*property-level hacks:*/
/*Standards, Edge*/
prop:val;
/*lte ie 11*/
prop:val\9;
/*lte ie 8*/
prop:val \9;
/*lte ie 7*/
*prop:val;
/*lte ie 6*/
_prop:val;

/*other direction...*/
/*gte ie 8, NOT Edge*/
prop:val\0;

旁注,我觉得自己像个肮脏的死灵法师——但我想在某个地方记录我今天发现的仅排除 IE8 的选择器集黑客,这似乎是最合适的地方。

于 2016-04-04T19:14:45.623 回答
0

你能不使用你已经展示过的hack,然后使用IE7及以下的hack来覆盖它吗?

于 2010-06-15T07:01:19.577 回答
0

There are various ways to get a class onto the HTML element, identifying which IE version you're contending with: Modernizr, the HTML 5 Boilerplate, etc - or just roll your own. Then you can use that class (eg .lt-ie9) in a normal CSS selector, no hack needed. If you only want to affect IE8 and not previous versions, put the old value back using a .lt-ie8 selector.

于 2014-03-07T12:15:07.673 回答
0

仅对于 IE8 原生浏览器:

.classname{
    *color: green; /* This is for IE8 Native browser alone */
}
于 2015-10-29T06:12:02.487 回答
-1

我一直在为 IE10 及以下 CSS 样式寻找一个不错的选择(但它也仅适用于 IE8)并且我想出了这个想法:

<!--[if lt IE 10]><div class="column IE10-fix"><![endif]-->
<!--[if !lt IE 10]><!--><div class="column"><!--<![endif]-->

我用一个额外的类声明我的 div 或任何你想要的东西,这允许我以非常易读的方式在同一个样式表中拥有额外的 CSS。

它是 W3C 有效的,而不是奇怪的 CSS hack。

于 2016-09-30T15:08:10.833 回答
-2

这适用于 Bellow IE8 版本

.lt-ie9 #yourID{

你的CSS代码

}

于 2015-10-27T13:29:53.370 回答