合并CSS IE调用

时间:2012-01-23 20:41:39

标签: css internet-explorer conditional

有没有办法从CSS中调用IE条件?我想避免从我的代码中调用一长串IE css文件。

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

并希望有一个电话

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

并在css文件中包含条件。这可能还是Paul Irish要走的路?

我尝试应用this article

中建议的“高级规则”
#column_right {
float:left;
width:250px;
margin-left:-250px;
[if IE 6] ie6: 100%;
[if lt IE 6] lt-ie6: 100%;
[if lte IE 6] lte-ie6: 100%;
[if ! lte IE 6] not-lte-ie6: 100%;
}
#footer {
clear: left;
width: 100%;
[if IE 5.0] padding: 10px 0px;
[if IE 5.5] margin-bottom: 40px;
}

[if IE 5] .box {
width: 200px;
padding: 100px;
margin: 100px;
}

但这对我不起作用。我尝试了一些基本的东西,比如

[if IE 7] background-color: red;
[if IE 8] background-color: yellow;

3 个答案:

答案 0 :(得分:3)

使用条件代码将类添加到HTML元素。然后使用该类仅为ie。

的版本选择元素
<!doctype html>

<!--[if lt IE 7 ]> <html class="no-js ie" lang="en"> <![endif]-->
<!--[if IE 7 ]>    <html class="no-js ie7" lang="en"> <![endif]-->
<!--[if IE 8 ]>    <html class="no-js ie8" lang="en"> <![endif]-->
<!--[if (gte IE 9)|!(IE)]><!--> <html class="no-js" lang="en"> <!--<![endif]-->

.ie8 body {width: 98%}
.ie7 body {width: 100%}
.ie body {width: 50%}

在ie8中,主体元素的宽度为98%,宽度为7%,宽度为100%,IE6为低于50%。

答案 1 :(得分:0)

没有。这是不可能的。但是(有时)可以为所有IE编码并合并代码。 IE中不存在条件注释,但有hacks I would not recommend these, ever!

答案 2 :(得分:-1)

您还可以尝试在服务器端动态加载CSS。从请求对象获取用户代理,如果他们使用IE版本x,则将CSS文件1,2,3连接在一起。只需确保响应的mime类型是CSS或纯文本...