2012-01-23 116 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%。