IE8 css选择器

45 css internet-explorer css-hack

要仅在IE浏览器中定位元素,我将使用

IE6:

* html #nav li ul {
    left: -39px !important;
    border: 1px solid red;
}
Run Code Online (Sandbox Code Playgroud)

IE7:

*+html #nav li ul  {
    left: -39px! important;
}
Run Code Online (Sandbox Code Playgroud)

有谁知道如何定位IE8?

Nic*_*ick 84

我不打算讨论是否应该使用这个方法,但这只会让你为IE8-9设置特定的css属性(注意:它不是一个选择器,所以有点不同于你问):

在每个css声明后使用'\ 0 /',所以:

#nav li ul  {
  left: -39px\0/ !important;
}
Run Code Online (Sandbox Code Playgroud)

为了建立另一个答案,你可以这样做为IE6,IE7和IE8分配各种样式:

#nav li ul  {
   *left: -7px    !important; /* IE 7 (IE6 also uses this, so put it first) */
   _left: -6px    !important; /* IE 6 */
    left: -8px\0/ !important; /* IE 8-9 */
}
Run Code Online (Sandbox Code Playgroud)

来源:http: //dimox.net/personal-css-hacks-for-ie6-ie7-ie8/

  • 我怎样才能定位IE8?因为\ 0 /和\ 9适用于IE 8和9. (2认同)

Rex*_*x M 75

2013更新:IE10 +不再支持条件评论.

原始答案:

有些人似乎很困惑,因为这不回答问题的字母,只有精神 - 所以澄清:

没有浏览器选择器这样的东西.有些黑客可以利用特定浏览器的CSS解析器中的错误和/或故障,但依靠这些可能会让自己陷入失败之中.有一种标准的,可接受的方式来解决这个问题:

使用条件注释仅针对IE.

例:

<!--[if gte IE 8]>
<style>
(your style here)
</style>
<![endif]-->
Run Code Online (Sandbox Code Playgroud)

<!-->所有非IE浏览器都会忽略两者中的所有内容作为注释,并且IE版本小于IE8将会跳过它.只有IE8和更高版本才能处理它.2013年更新: IE10 +也将忽略它作为评论.

  • @jon"CSS hacks因为某种原因被称为hacks - 它们是非专业的编程." 好吧,我想如果像IE8这样的浏览器是"专业编程"我们就不需要黑客了,现在我们呢? (35认同)
  • 我不认为hacky代码排除了更好的代码.这不是开发者的错,需要黑客来处理笨拙和遗留的平台而不用拔掉头发 - 我很乐意接受带有hacky表现代码的鄙视的目光,只要它在我可以抛出的所有内容中都能正确显示在它,特别是考虑到大多数将要查看它的人甚至不知道什么是HTML .. (10认同)
  • 我猜你的条件语句可以解决这个问题,尽管这并没有真正回答Chris想要知道一个仅用于解决IE8的选择器的问题. (7认同)
  • 我甚至会说"使用条件评论来定位IE的任何版本". (3认同)
  • 有点晚了,但这不是问题;)它不是关于条件注释,而是关于CSS选择器.我想这里的每个人都知道那些都在附近,选择器是很难找到的东西. (3认同)
  • 可能要提一下IE10不再支持条件评论,所以"IE8和更大"应该是"IE8和IE9" (3认同)

Gum*_*mbo 26

看看这些:

/* IE8 Standards-Mode Only */
.test { color /*\**/: blue\9 }

/* All IE versions, including IE8 Standards Mode */
.test { color: blue\9 }
Run Code Online (Sandbox Code Playgroud)

(来源:David Bloom对IE8标准模式的CSS攻击)

  • 它只针对IE7(和IE 6),如果你添加分号';' 到最后一行 (4认同)

ima*_*e72 14

你可以这样使用.它比

<link rel="stylesheet" type="text/css" media="screen" href="css/style.css" />
<!--[if IE 7]><link rel="stylesheet" type="text/css" media="screen" href="css/ie7.css"  /><![endif]-->
<!--[if IE 6]><link rel="stylesheet" type="text/css" media="screen" href="css/ie6.css"  /><![endif]-->
-------------------------------------------------------------

<!--[if lt IE 7 ]> <body class="ie6"> <![endif]-->
  <!--[if IE 7 ]> <body class="ie7"> <![endif]-->
  <!--[if IE 8 ]> <body class="ie8"> <![endif]-->
  <!--[if !IE]>--> <body> <!--<![endif]-->
Run Code Online (Sandbox Code Playgroud)

div.foo {color:inherit;} .ie7 div.foo {color:#ff8000; }


Sir*_*eno 11

仅适用于IE8的CSS样式:

.divLogRight{color:Blue; color:Red\9; *color:Blue;}
Run Code Online (Sandbox Code Playgroud)

只有IE8才会是Red.

第一个Blue:适用于所有浏览器.

红色:仅限IE6,7,8

第二蓝:仅限IE6,7


所以Red =仅适用于IE8.

有关浏览器黑客的完整摘要(包括Internet Explorer(IE),Safari,Chrome,iPhone和Opera),请访问此链接:http: //paulirish.com/2009/browser-specific-css-hacks/

  • IE9-10怎么样? (2认同)

小智 11

这个问题很古老但是......

在打开身体标签后...

<!--[if gte IE 8]>
<div id="IE8Body">
<![endif]-->
Run Code Online (Sandbox Code Playgroud)

就在关闭身体标签之前..

<!--[if gte IE 8]>
</div>
<![endif]-->
Run Code Online (Sandbox Code Playgroud)

CSS ..

#IE8Body #nav li ul {}
Run Code Online (Sandbox Code Playgroud)

您可以使用条件语句为所有IE浏览器执行此操作,或者通过使用浏览器名称+版本服务器端封装div中的所有内容来定位所有浏览器


MrF*_*ion 11

基于image72的优秀答案,你可以拥有像这样的高级CSS选择器:

<!--[if lt IE 7]><body class="IE IE7down IE8down IE9down IE10down"><![endif]-->
<!--[if IE 7]><body class="IE IE7 IE7down IE8down IE9down IE10down IE7up"><![endif]-->
<!--[if IE 8]><body class="IE IE8 IE8down IE9down IE10down IE7up IE8up"><![endif]-->
<!--[if IE 9]><body class="IE IE9 IE9down IE10down IE7up IE8up IE9up"><![endif]-->
<!--[if gte IE 10]><body class="IE IE10 IE10down IE7up IE8up IE9up IE10up"><![endif]-->
<!--[if !IE]>--><body class="notIE"><!--<![endif]-->
Run Code Online (Sandbox Code Playgroud)

所以在你的CSS中你可以这样做:

.notIE   .foo { color: blue;   } /* Target all browsers except IE */
.IE9up   .foo { color: green;  } /* Taget IE equal or greater than 9 */
.IE8     .foo { color: orange; } /* Taget IE 8 only */
.IE7down .foo { color: red;    } /* Target IE equal or less than 7 */

.IE8 .foo, .IE9 .foo {
    font-size: 1.2em;            /* Target IE8 & IE9 only */
}

.bar { background-color: gray; } /* Applies to all browsers, as usual */

/* Maybe show a message only to IE users? */
.notIE #betterBrowser { display: none;  } /* Any browser except IE */
.IE    #betterBrowser { display: block; } /* All versions of IE */
Run Code Online (Sandbox Code Playgroud)

这很棒,因为:

  • 它完全符合标准(没有丑陋/危险的CSS攻击)
  • 无需单独的样式表
  • 您可以轻松定位任何版本的IE以及复杂的组合