10

我知道不应该这样做,但我现在只想快速修复,这将使我有时间找到合适的修复方法。

我如何使用 CSS 单独定位 IE8,因为我尝试过附加\9,例如:

margin:100px\9;

但是,它也会影响 IE9,我不希望这样,因为在 IE9 上,整个站点看起来都很好。

4

4 回答 4

19

来自HTML5 Boilerplate,最初来自Paul Irish

将您的<html>标记更改为:

<!--[if lt IE 7 ]> <html lang="en" class="ie6"> <![endif]-->
<!--[if IE 7 ]>    <html lang="en" class="ie7"> <![endif]-->
<!--[if IE 8 ]>    <html lang="en" class="ie8"> <![endif]-->
<!--[if IE 9 ]>    <html lang="en" class="ie9"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--> <html lang="en"> <!--<![endif]-->

然后 IE8 会在 html 标签中添加一个 .ie8 类。所有其他版本的 IE 都一样。然后你可以这样做:

.ie8 {
    margin:100px;
}

编辑: 删除了no-js类,并将lang=""属性更新为您的语言。谢邀,眼不见为净。

于 2011-05-02T01:30:42.457 回答
9

这应该只适用于 IE9(也可能是较新的版本):

:root #element-id {
  margin: 400px\9;
}

这是因为:root在 IE9 之前的版本中没有实现伪类(参见http://msdn.microsoft.com/en-us/library/cc351024%28v=vs.85%29.aspx)。

于 2011-12-07T22:12:31.470 回答
3

您可以使用根功能

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

仅在 IE 上破解

#element {
color:orange;
}
#element {
*color: white;    /* IE6 + 7, doesn't work in IE8/9 as IE7 */
}
#element {
_color: red;     /* IE6 */
}
#element {
color: green\0/IE8+9; /* IE8 + 9 + IE10pp4  */
}
:root #element { color:pink \0/IE9; }  /* IE9 + IE10pp4 */
于 2013-02-04T07:41:34.283 回答
1

有三种方法可以做到这一点,

IE 条件注释

IE 条件注释可能是修复特定版本(IE6、IE7、IE8)的 IE 错误的最常用方法。下面是一些针对不同版本 Internet Explorer 的示例代码:

    <!--[if IE 8]> = IE8
    <!--[if lt IE 8]> = IE7 or below
    <!--[if gte IE 8]> = greater than or equal to IE8

<!--[if IE 8]>
<style type="text/css">
    /* css for IE 8 */
</style>
<![endif]-->

<!--[if lt IE 8]>
    <link href="ie7.css" rel="stylesheet" type="text/css" />
<![endif]-->

特定于资源管理器的 CSS 规则(IE CSS hacks)

另一种选择是声明只能由 Explorer 读取的 CSS 规则。例如,在 CSS 属性以 IE7 为目标之前添加星号 (*) 或在属性以 IE6 为目标之前添加下划线。但是,不推荐使用此方法,因为它们不是有效的 CSS 语法。

IE8 or below: to write CSS rules specificially to IE8 or below, add a backslash and 9 (\9) at the end before the semicolon.
IE7 or below: add an asterisk (*) before the CSS property.
IE6: add an underscore (_) before the property.

.box {

    background: gray; /* standard */

    background: pink\9; /* IE 8 and below */

    *background: green; /* IE 7 and below */

    _background: blue; /* IE 6 */

}

条件 HTML 类

第三种选择,由 Paul Irish 创立,是通过使用 IE 条件注释在 HTML 标签中添加一个带有 IE 版本的 CSS 类。基本上,它会检查它是否是 IE,然后在 html 标签中添加一个类。因此,要针对特定​​的 IE 版本,只需使用 IE 类作为父选择器(例如 .ie6 .box)。这是一个聪明的方法,它不会导致任何验证错误。

<!--[if lt IE 7 ]> <html class="ie6"> <![endif]-->
<!--[if IE 7 ]> <html class="ie7"> <![endif]-->
<!--[if IE 8 ]> <html class="ie8"> <![endif]-->
<!--[if IE 9 ]> <html class="ie9"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--> <html> <!--<![endif]-->
于 2014-07-25T11:15:48.317 回答