DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> CSS入門知識 >> CSS詳解
CSS Hack 整理:如何分別只對 IE6/7/8 與現代瀏覽器下樣式
編輯:CSS詳解     

以前做案子只需要顧 IE6 與 Firefox,換了團隊之後標準提高到 IE6、7、8 全都要相容。遇到必須「只對 IE8 生效」的樣式時,最直覺的做法是寫條件註解,但為了一行 CSS 多開一個檔案、或在 header 塞一整段註解,實在不太優雅。

樣式內嵌的 Hack 寫法

把差異寫在同一個規則裡,靠解析器的「讀懂/讀不懂」來分流:

selector {
  property: value;      /* 所有瀏覽器 */
  property: value\9;    /* 所有 IE */
  +property: value;     /* IE7 */
  _property: value;     /* IE6 */
}

注意撰寫順序:因為同優先權時後者覆蓋前者,所以從通用值往特定版本疊寫,最終會呈現 Firefox、IE8、IE7、IE6 各自讀到的值。

完整範例

p.ie {
  height: 60px;
  line-height: 60px;
  text-align: center;
  border: 1px dashed #bbb;
  background: #f7f7f7;
  font-size: 15px;
  color: blue;        /* 所有瀏覽器 */
  color: brown\9;     /* 所有 IE 瀏覽器 */
  +color: red;        /* IE7 */
  _color: green;      /* IE6 */
}
<body style="width:500px; margin:0 auto;">
<p class="ie">
  <span style="display:block; display:none\9;">
    嘿嘿,竟然用 Firefox,看到的是藍色文字。
  </span>
  <!--[if IE 8]>不錯嘛,用 IE8 呢!文字是褐色的。<![endif]-->
  <!--[if IE 7]>你,IE7,紅色文字!<![endif]-->
  <!--[if IE 6]>綠色文字,不過 IE6 真的該退休了。<![endif]-->
</p>
</body>

前提條件:這類 hack 依賴標準模式(standards mode)。HTML 最前面必須有完整的 DOCTYPE:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">

少了它,瀏覽器進入 quirks mode,所有推論都不成立。另外,這裡講的「IE8」不含相容模式,因為 IE8 的相容模式本質上就是 IE7。

常見 Hack 速查

區分 IE6/7 與 FF/IE8

background: blue;
*background: orange;

IE6/7 顯示 orange,FF/IE8 顯示 blue。原理:Firefox 與 IE8 不認星號前綴,IE6/7 認。

區分 IE6 與 IE7/IE8/FF

background: green;
_background: blue;

IE6 顯示 blue,其餘顯示 green。原理:只有 IE6 認得底線前綴。

同時區分 IE6 / IE7 / FF+IE8

background: orange;
+background: green;
-background: blue;

或者:

background: orange;
*background: green !important;
*background: blue;

IE6 得到 blue,IE7 得到 green,FF/IE8 得到 orange。原理:IE6 認 -,IE7 認 +,IE8 與 FF 兩者都不認;星號則是 IE8/FF 不認,IE7 優先吃 !important,IE6 不吃 !important。

只針對 IE8

.test {
  color: /*\**/ #00f\9;   /* 只有 IE8 */
  color: #00f\9;          /* 所有 IE 版本 */
}

四路分流(FF / IE8 / IE7 / IE6)

.test {
  color: #000;            /* Firefox */
  color: /*\**/ #00f\9;   /* IE8 */
  *color: #f00;           /* IE7 */
  _color: #0f0;           /* IE6 */
}

改用條件註解:更明確也更安全

如果專案本來就必須支援舊 IE,條件註解的可讀性遠勝 hack——它不會污染主樣式表,也不必在腦中模擬解析器行為。

針對不同版本載入獨立檔案:

<link rel="stylesheet" type="text/css" href="css/style.css" media="screen" />

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

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

<!--[if gte IE 8]>
<link rel="stylesheet" type="text/css" href="css/IE8style.css" media="screen" />
<![endif]-->

或者直接在頁面內嵌少量樣式:

<!--[if lt IE 8]>
<style>
  #warp { padding-bottom: 11px; }
</style>
<![endif]-->

lt(小於)、lte(小於或等於)、gte(大於或等於)是常用的條件運算子,用一個區段就能涵蓋一整段版本範圍,比堆三組 hack 乾淨。

今天的建議

以上技巧的歷史價值大於實用價值。IE11 已於 2022 年退役,IE6–8 的市佔在現代網站上基本歸零。今天的跨瀏覽器問題幾乎都出在 Safari 的個別行為、行動瀏覽器差異,或新語法的漸進增強上。與其背誦 hack 表,更值得投資的是:

  • @supports 特性查詢,用能力而非版本判斷。
  • 寫好 fallback 讓樣式自然降級,而不是精準轟炸某個版本。
  • 用 Autoprefixer 自動處理前綴,別手寫。
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved