【譯】條件注釋( conditional comment)

原文(譯者在原文基礎(chǔ)上添加了部分內(nèi)容):https://css-tricks.com/how-to-create-an-ie-only-stylesheet/

參考:微軟官方文檔:About conditional comments(強烈建議親自看一下微軟官方文檔)

這篇文章更新自一篇舊文(原來是 2007 年 9 月 24 日)。我只是想擴展這篇舊文并解釋得更清楚一些。

原文中有兩個術(shù)語:條件注釋conditional comment(貌似這個更加符合其意義)、條件樣式表conditional stylesheet

如果你愿意閱讀這篇博客,那么你 99% 是在 IE 上有過煩躁到抓頭發(fā)的經(jīng)歷。But if you are worth your salt as a CSS coder,你應(yīng)該可以處理這種問題。我的觀點是:即使不使用 hack,你也可以處理 IE 拋給你的任何問題。hack 是危險的,因為 hack 基于非標準的漏洞,你無法預(yù)測這些 hack 在未來的瀏覽器上會表現(xiàn)成什么樣子。處理 IE 問題的首選工具是條件樣式表(conditional stylesheet)。IE 提供了注釋標簽,支持指定特定的版本(從更低版本一直到 IE 8),也支持一次性指定多個版本(高于greater than某個版本或低于less than某個版本)(譯者注:這篇文章是 2010年 1 月 20 日寫的)。

1. 為什么使用條件樣式表(conditional stylesheet)?

  • You got problems, they need fixin'
  • Keeps your code hack-free and valid(讓你的代碼免于使用 hack 方法,并在未來的瀏覽器中保持有效)
  • Keeps your main stylesheet clean(讓你的主樣式表保持純凈)
  • Perfectly acceptable technique, sanctioned by Microsoft(這是完全可以接受的技術(shù),是微軟認可的)

記住,這些條件樣式表不僅僅可以用于 CSS,你可以用來加載 JavaScript,甚至可以在網(wǎng)站的內(nèi)容中用來顯示特殊的 僅用于 IE 的信息。

2. 代碼

這 與所有其他的常規(guī)外聯(lián) CSS 文件一樣 放在 <head> 標簽中。你應(yīng)該很熟悉它的開始標簽和結(jié)束標簽,它們只是常規(guī)的 HTML 注釋。然后就是,中括號里的 ifIE,它們的意思很明顯了吧,不用我多說了。需要注意的語法是:! 代表 not,所以 !IE 表示 not IEgt 表示 greater than,gte 表示 greater than or equal,lt 表示 less than,lte 表示 less than or equal。

注意:IE 10 及以上版本不支持條件注釋(conditional comment)

針對所有版本的 IE(Target ALL VERSIONS of IE)

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

針對除了 IE 之外的所有瀏覽器(Target everything EXCEPT IE)

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

只針對 IE 7(Target IE 7 ONLY)

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

只針對 IE 6(Target IE 6 ONLY)

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

只針對 IE 5(Target IE 5 ONLY)

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

只針對 IE 5.5(Target IE 5.5 ONLY)

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

針對 IE 6 以及更低版本(Target IE 6 and 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]-->

針對 IE 7 以及更低版本(Target IE 7 and 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]-->

針對 IE 8 以及更低版本(Target IE 8 and 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]-->

針對 IE 6 以及更高版本(Target IE 6 and HIGHER)

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

針對 IE 7 以及更高版本(Target IE 7 and HIGHER)

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

針對 IE 8 以及更高版本(Target IE 8 and HIGHER)

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

3. 針對 IE 10(Target IE 10)

參見這里

條件注釋在 IE 10 中消失了。這是好事兒。IE 10 是一個非常好的瀏覽器。功能檢測(feature detection)是幾乎所有方案中最好的一種方案。但如果你發(fā)現(xiàn)有一些樣式需要針對 IE 10,該怎么辦呢?我想你將不得不面對這種情況。

通過一小段 JavaScript 代碼就可以把客戶端(User Agent)添加到 <html> 元素:

var doc = document.documentElement;
doc.setAttribute('data-useragent', navigator.userAgent);

IE 10 的客戶端字符串(User Agent string) 為:

Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)

這將導致:

<html data-useragent="Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)">

然后你就可以像下面這樣書寫樣式:

html[data-useragent*='MSIE 10.0'] h1 {
  color: blue;
}

4. 反對條件樣式表的觀點

我們不應(yīng)該需要它們。它們違背了 web 標準的精神。

5. 支持條件樣式表的觀點

雖然它們違背了 web 標準的精神,但我們確實需要它們。

6. 其他資源


7. 實際應(yīng)用

打算拋棄 IE 8,但希望 IE 8 及更低版本 的用戶在瀏覽本站時,可以得到一個體驗更好的不兼容提示,發(fā)現(xiàn)這個網(wǎng)站(http://www.uhouzz.com/)使用了條件注釋的方法,所以學習了。

Paste_Image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 問答題47 /72 常見瀏覽器兼容性問題與解決方案? 參考答案 (1)瀏覽器兼容問題一:不同瀏覽器的標簽?zāi)J的外補...
    _Yfling閱讀 14,104評論 1 92
  • 問題 一、如何調(diào)試 IE 瀏覽器? 使用高版本的ie控制臺查看盒模型 低版本ie使用border:1px soli...
    吳晗君閱讀 458評論 0 1
  • 1.什么是 CSS hack 不同廠家的瀏覽器,或者同一廠商的不同版本的瀏覽器對css的解析認識可能存在差別,因此...
    molly的紅草帽閱讀 249評論 0 0
  • 什么是 CSS hack CSS hack是通過在CSS樣式中加入一些特殊的符號,讓不同的瀏覽器識別不同的符號(什...
    怎么昵稱閱讀 526評論 0 1
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,516評論 19 139

友情鏈接更多精彩內(nèi)容