2012-01-23 55 views
1

有没有办法从CSS内部调用IE条件?我想避免从代码中调用一长串IE浏览器的css文件。巩固CSS IE调用

<!--[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; 

回答

0

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

+0

这是可能的。看到我的回答 –

+0

是的,你可以做到这一点。那是我忘记的一个。但我更专注于有条件的评论_in_ CSS。这是不可能的。但是你的风格会解决问题。 – Ktash

-1

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

+0

不推荐使用用户代理嗅探,并且在这些日子里通常会皱起眉头。有更好的方法来实现这一点。 – Ktash

+0

噢,来吧,你不能离开这种劝诫,而不带走我在其他帖子上发表评论的能力? – Matt

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中,body元素将有98%的宽度,ie7,100%的宽度,IE6和更低的50%。