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

Xai*_*oft 2 css

如果我想根据用户正在查看页面的浏览器添加填充,有没有一种方法可以在CSS中执行以下操作:

如果IE填充:5px; 否则如果不是IE填充10px;

Mar*_*urd 11

这是一个很好的参考:Quirksmode.org条件评论.

虽然控制结构在标记而不是CSS中,但它实现了您的目标,并且在提供特定于浏览器的样式表时通常被认为是最佳实践.


Luk*_*uke 7

最佳实践方法是为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]-->
Run Code Online (Sandbox Code Playgroud)

然后只需覆盖文件中特定的导致问题的样式ie-styles.css.


Mat*_*ija 5

定位IE的所有版本

<!--[if IE]>
<link rel="stylesheet" type="text/css" href="all-ie-only.css" />
<![endif]-->
Run Code Online (Sandbox Code Playgroud)

除IE之外的一切目标

<!--[if !IE]><!-->
<link rel="stylesheet" type="text/css" href="not-ie.css" />
<!--<![endif]-->
Run Code Online (Sandbox Code Playgroud)

仅针对IE 7

<!--[if IE 7]>
<link rel="stylesheet" type="text/css" href="ie7.css">
<![endif]-->
Run Code Online (Sandbox Code Playgroud)

仅针对IE 6

<!--[if IE 6]>
<link rel="stylesheet" type="text/css" href="ie6.css" />
<![endif]-->
Run Code Online (Sandbox Code Playgroud)

仅针对IE 5

<!--[if IE 5]>
<link rel="stylesheet" type="text/css" href="ie5.css" />
<![endif]-->
Run Code Online (Sandbox Code Playgroud)

仅针对IE 5.5

<!--[if IE 5.5000]>
<link rel="stylesheet" type="text/css" href="ie55.css" />
<![endif]-->
Run Code Online (Sandbox Code Playgroud)

目标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]-->
Run Code Online (Sandbox Code Playgroud)

目标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]-->
Run Code Online (Sandbox Code Playgroud)

目标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]-->
Run Code Online (Sandbox Code Playgroud)

目标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]-->
Run Code Online (Sandbox Code Playgroud)

目标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]-->
Run Code Online (Sandbox Code Playgroud)

目标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]-->
Run Code Online (Sandbox Code Playgroud)

有关该主题的完整参考,Chris Coyier:如何创建仅IE样式表