/* ❌ IE6 忽略 !important */
#test { color: red !important; color: green; }
/* ✅ IE6 生效:分成兩個規則集 */
#test { color: red !important; }
#test { color: green; }
原因:IE6 解析器在同一聲明塊內遇到重複屬性時,**最後一個值直接覆蓋前面所有**,連帶丟棄 `!important` 標記。分離規則集則正常按層疊順序處理。
#test {
color: red !important; /* 現代瀏覽器:生效 */
color: green; /* IE6:覆蓋上一行,顯示綠色 */
}
同理可用於 `background`、`margin` 等屬性的瀏覽器分叉。
| 需求 | 現代方案 | 優勢 |
|---|---|---|
| IE 專用樣式 | 條件註釋 `` | 不污染主 CSS、易維護 |
| 特性檢測 | Modernizr / `@supports` | 基於能力而非瀏覽器版本 |
| CSS 變數回退 | `color: var(--brand, red);` | 原生、無 Hack |
| PostCSS autoprefixer | 自動補前綴、語法降級 | 構建時處理,源碼乾淨 |