如果浏览器是 IE,则应用 CSS 规则

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3541982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 22:38:37  来源:igfitidea点击:

Apply CSS rules if browser is IE

cssinternet-explorer

提问by Mayur

Possible Duplicate:
How do I do IE conditionals in CSS?

可能的重复:
如何在 CSS 中执行 IE 条件?

How can I apply the rules below to IE only?

如何将以下规则仅应用于 IE?

.abc {

float:left;
height:0;
margin:0 10px;
width:0;

/*Apply these rules for IE only*/
position:absolute;
left:30;
top:-10;
/*Apply these rules for IE only*/
}

采纳答案by Pekka

In browsers up to and including IE9, this is done through conditional comments.

在 IE9 及以下的浏览器中,这是通过条件注释完成的。

<!--[if IE]>
<style type="text/css">
  IE specific CSS rules go here
</style>
<![endif]-->

回答by DASPRiD

A good way to avoid loading multiple CSS files or to have inline CSS is to hand a class to the body tag depending on the version of Internet Explorer. If you only need general IE hacks, you can do something like this, but it can be extended to be version specific:

避免加载多个 CSS 文件或使用内联 CSS 的一个好方法是根据 Internet Explorer 的版本将类传递给 body 标记。如果你只需要一般的 IE hacks,你可以做这样的事情,但它可以扩展为特定于版本:

<!--[if IE ]><body class="ie"><![endif]-->
<!--[if !IE]>--><body><!--<![endif]-->

Now in your css code, you can simply do:

现在在您的 css 代码中,您可以简单地执行以下操作:

.ie .abc {
  position:absolute;
  left:30;
  top:-10;
}

This also keeps your CSS files valid, as you do not have to use dirty (and invalid) CSS hacks.

这也使您的 CSS 文件有效,因为您不必使用脏(和无效)的 CSS hack。

回答by JohnDel

A fast approach is to use the following according to ie that you want to focus (check the comments), inside your css files (where margin-top, set whatever css attribute you like):

一种快速的方法是根据 ie 您想要关注的内容(检查注释)在您的 css 文件中使用以下内容(在 margin-top 处,设置您喜欢的任何 css 属性):

margin-top: 10px; /*It will apply to all ie from 8 and below */
*margin-top: 10px; /*It will apply to ie 7 and below */
_margin-top: 10px; /*It will apply to ie 6 and below*/

A better approach would be to check user agent or a conditional if, in order to avoid the loading of unnecessary CSS in other browsers.

更好的方法是检查用户代理或条件 if,以避免在其他浏览器中加载不必要的 CSS。

回答by angryobject

I prefer using a separate file for ie rules, as described earlier.

如前所述,我更喜欢为 ie 规则使用单独的文件。

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

And inside it you can set up rules for different versions of ie using this:

在其中,您可以使用以下命令为不同版本的 ie 设置规则:

.abc {...} /* ALL MSIE */
*html *.abc {...} /* MSIE 6 */
*:first-child+html .abc {...} /* MSIE 7 */