萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網頁製作 >> Div CSS 教學

CSS Hack 區分 IE6/IE7/IE8/Firefox/Chrome 完整速查表

核心符號對照表

Hack 符號識別瀏覽器備註
\9(反斜線九)IE6/7/8/9/10屬性值結尾
*(星號)IE6/7屬性名前綴
_(底線)IE6屬性名前綴
!importantIE7+、現代瀏覽器IE6 忽略

實戰範例:同一屬性針對不同瀏覽器給不同值

#box {
  background: blue;        /* Firefox, Chrome, Safari, Edge, IE11+ */
  background: red\9;       /* IE6~IE10 */
  *background: black;      /* IE6, IE7 */
  _background: orange;     /* IE6 only */
}

讀取順序:由上而下,後蓋前。IE6 會依序讀到藍→紅→黑→橘,最終呈橘色。

進階區分 IE6/IE7/Firefox(兩種寫法)

方法一:* + _

#tip {
  background: blue;        /* FF */
  *background: black;      /* IE7 */
  _background: orange;     /* IE6 */
}

方法二:* + !important

#tip {
  background: blue;              /* FF */
  *background: green !important; /* IE7 */
  *background: orange;           /* IE6 */
}

IE7 讀得懂 * 也懂 !important → 綠色;IE6 只懂 * → 橘色;FF 忽略 * 但懂 !important → 藍色。

現代建議

  • **棄用 Hack**:採用功能偵測、漸進增強、Polyfill。
  • **條件註釋** 載入修補樣式表(僅 IE9- 支援):<!--[if lte IE 9]><link href="ie-fix.css"><![endif]-->
  • **PostCSS Autoprefixer** 自動補前綴,避免手寫 Hack。
copyright © 萬盛學電腦網 all rights reserved