简体   繁体   English

我如何在CSS中进行IE条件限制?

[英]How do I do IE conditionals in CSS?

If I want to add padding based on the browser the user is viewing the page in, is there a way in CSS that I can do something like: 如果我想根据用户正在查看页面的浏览器添加填充,有没有一种方法可以在CSS中执行以下操作:

if IE do padding:5px; 如果IE填充:5px; else if not IE do padding 10px; 否则如果不是IE填充10px;

Here is a great reference: Quirksmode.org Conditional Comments . 这是一个很好的参考: Quirksmode.org条件评论

Although the control structure is in the markup and not the CSS, it accomplishes your goal and is usually considered the best practice when serving browser-specific stylesheets. 虽然控制结构在标记而不是CSS中,但它实现了您的目标,并且在提供特定于浏览器的样式表时通常被认为是最佳实践。

The best-practice way is to have a single stylesheet for IE-only fixes, like so: 最佳实践方法是为IE修复程序提供单个样式表,如下所示:

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

Then just override specific problem-causing styles in the ie-styles.css file. 然后只需覆盖ie-styles.css文件中特定的导致问题的样式。

Target ALL VERSIONS of IE 定位IE的所有版本

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

Target everything EXCEPT IE 除IE之外的一切目标

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

Target IE 7 ONLY 仅针对IE 7

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

Target IE 6 ONLY 仅针对IE 6

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

Target IE 5 ONLY 仅针对IE 5

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

Target IE 5.5 ONLY 仅针对IE 5.5

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

Target IE 6 and LOWER 目标IE 6和LOWER

<!--[if lt IE 7]>
<link rel="stylesheet" type="text/css" href="ie6-and-down.css" />
<![endif]-->

<!--[if lte IE 6]>
<link rel="stylesheet" type="text/css" href="ie6-and-down.css" />
<![endif]-->

Target IE 7 and LOWER 目标IE 7和LOWER

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

<!--[if lte IE 7]>
<link rel="stylesheet" type="text/css" href="ie7-and-down.css" />
<![endif]-->

Target IE 8 and LOWER 目标IE 8和LOWER

<!--[if lt IE 9]>
<link rel="stylesheet" type="text/css" href="ie8-and-down.css" />
<![endif]-->

<!--[if lte IE 8]>
<link rel="stylesheet" type="text/css" href="ie8-and-down.css" />
<![endif]-->

Target IE 6 and HIGHER 目标IE 6和更高

<!--[if gt IE 5.5]>
<link rel="stylesheet" type="text/css" href="ie6-and-up.css" />
<![endif]-->

<!--[if gte IE 6]>
<link rel="stylesheet" type="text/css" href="ie6-and-up.css" />
<![endif]-->

Target IE 7 and HIGHER 目标IE 7和更高

<!--[if gt IE 6]>
<link rel="stylesheet" type="text/css" href="ie7-and-up.css" />
<![endif]-->

<!--[if gte IE 7]>
<link rel="stylesheet" type="text/css" href="ie7-and-up.css" />
<![endif]-->

Target IE 8 and HIGHER 目标IE 8和更高

<!--[if gt IE 7]>
<link rel="stylesheet" type="text/css" href="ie8-and-up.css" />
<![endif]-->

<!--[if gte IE 8]>
<link rel="stylesheet" type="text/css" href="ie8-and-up.css" />
<![endif]-->

For complete reference on the topic, Chris Coyier: How To Create an IE-Only Stylesheet 有关该主题的完整参考,Chris Coyier: 如何创建仅IE样式表

Here's a cleaner way to target IE 10+ in CSS only 这是一种更简洁的方法,只在CSS中定位IE 10+

@media all and (-ms-high-contrast: none), (-ms-high-contrast: active) {

  /* IE10+ CSS styles go here */

}

(Source: Phil Newcomer ) (来源: Phil Newcomer

If you don't mind ugliness in your code, you can use something like the Holly hack: 如果你不介意代码中的丑陋,你可以使用像Holly hack这样的东西:

div { padding:5px; }
* html div { padding:10px; }

There's a neat CSS Zen Garden example that does this to present two distinct designs, but I don't recall its name. 有一个巧妙的CSS Zen Garden示例,它提供了两个不同的设计,但我不记得它的名字。

Although the IE conditional can be used only in html and not in the CSS, you can use the following in your CSS files for your quick-and-short tests/hacks. 尽管IE条件只能在html中使用而不能在CSS中使用,但您可以在CSS文件中使用以下内容进行快速和简短的测试/黑客攻击。

  p {
   /* all browsers */
   background-color: red; 

   /* for IE7 and below - append a * before the property */
   *background-color: blue; 

   /* for IE6 and below - append a _ before the property */
   _background-color: green;
  }

While you can use this for your quick and short requirements, I think, you should follow the suggestion by Mark Hurd given above for production-level heavy codes. 虽然您可以将此用于快速和短期的要求,但我认为您应该遵循上面给出的Mark Hurd对生产级重码的建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM