雖然 IE 已退出歷史舞台,但維護老項目、理解歷史差異對掌握 CSS 本質仍有價值。本文系統對比 Firefox (Gecko) 與 IE (Trident) 在盒模型、浮動、選擇器、屬性支持等核心差異,並提供標準化解決方案。
| 差異點 | Firefox (標準) | IE6/7 (Quirks/舊標準) | IE8+ (標準模式) | 解決方案 |
|---|---|---|---|---|
| 盒模型解析 | W3C 標準:width = content | IE 盒模型:width = content+padding+border | DOCTYPE 觸發標準模式同 W3C | box-sizing: border-box 統一 |
| 浮動父級高度塌陷 | 父級高度不包含浮動子元素 | hasLayout 觸發時自動包含 (IE6/7 特有) | 同 Firefox | Clearfix / overflow:hidden / display:flow-root |
| 浮動寬度 100% 計算 | 基於包含塊 content box | 三列浮動 100% 會換行 (邊框/內邊距導致) | 同 Firefox | box-sizing:border-box + 留白間距 |
| 非浮動元素避開浮動 | 背景/邊框延伸至浮動下方,內容環繞 | 背景/邊框不延伸 (hasLayout 觸發不同) | 同 Firefox | 觸發 BFC (overflow:hidden 等) 或 clear |
| text-decoration 位置 | 下劃線貼近文字底部 | 下劃線較低,可能穿過下行字符 | 改善但仍有差異 | text-underline-offset (現代) 或 border-bottom 模擬 |
| margin:auto 居中 | 塊級元素生效 | IE6 需父級 text-align:center | 標準生效 | DOCTYPE + margin:0 auto + 定寬 |
| padding 撐開寬高 | 標準盒模型:撐開 | IE 盒模型:不撐開 (width 含 padding) | 標準模式撐開 | box-sizing:border-box |
!important 支持 | ✅ 完整支持 | ❌ 忽略 | ✅ 支持 | IE6 用屬性 Hack (_prop, *prop) |
cursor:pointer | ✅ 手型 | hand 專有 | ✅ 標準 | 統一用 pointer |
| 鏈接邊框/背景 | 需 display:block/inline-block | 行內元素也可直接加邊框背景 | 同 Firefox | 統一 display:inline-block |
| 垂直居中 | line-height = height | 同 | 同 | 現代用 Flex/Grid |
腳本 language 屬性 | 不支持 (XHTML 1.1) | 支持 | 不支持 | 用 type="text/javascript" |
.box {
width: 200px; /* 所有瀏覽器 */
_width: 220px; /* 只有 IE6 識別 (下劃線) */
*width: 210px; /* IE6/7 識別 (星號) */
width: 200px\9; /* IE8/9/10 識別 (\9) */
/* 現代瀏覽器最終應用 200px */
}
/* IE6 */
* html .box { /* ... */ }
/* IE7 */
*+html .box { /* ... */ }
*:first-child+html .box { /* ... */ }
/* IE8/9/10 */
@media all and (-ms-high-contrast: none), (-ms-high-contrast: active) {
.box { /* IE10/11 */ }
}
@media \0screen\,screen\9 { .box { /* IE6/7/8 */ } }
@media \0screen { .box { /* IE8 */ } }
/* WebKit/Blink (Chrome/Safari/新Edge) */
@media screen and (-webkit-min-device-pixel-ratio:0) {
.box { /* ... */ }
}
/* Firefox */
@-moz-document url-prefix() {
.box { /* ... */ }
}
/* Edge (舊版 EdgeHTML) */
@supports (-ms-ime-align:auto) {
.box { /* Edge 18- */ }
}
<!--[if IE 6]>
<link rel="stylesheet" href="ie6.css">
<![endif]-->
<!--[if lte IE 7]>
<link rel="stylesheet" href="ie7-.css">
<![endif]-->
<!--[if gt IE 8]>
<link rel="stylesheet" href="modern.css">
<![endif]-->
<!--[if !IE]>>
<link rel="stylesheet" href="not-ie.css">
<!--<![endif]-->
IE10+ 已移除條件註釋支持,現代項目不再適用。
*, *::before, *::after {
box-sizing: border-box; /* 所有瀏覽器 IE8+ 支持 */
}
.clearfix::after {
content: '';
display: table;
clear: both;
}
/* 現代:建立 BFC 且不產生副作用 */
.container {
display: flow-root; /* Firefox 53+, Chrome 58+, Edge 79+, Safari 15.4+ */
/* 回退 */
overflow: hidden; /* 經典,但會剪裁陰影/絕對定位子元素 */
}
/* 三列等高佈局 - Float 時代痛苦,Flex 一行搞定 */
.layout {
display: flex;
gap: 20px;
}
.sidebar { flex: 0 0 260px; }
.main { flex: 1; }
/* 響應式網格 - Grid 時代 */
.grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
gap: 24px;
}
@supports (display: grid) {
.modern-grid { display: grid; ... }
}
@supports not (display: grid) {
.fallback-flex { display: flex; ... }
}
/* npm install normalize.css */
@import 'normalize.css'; /* 或構建工具引入 */
/* 統一跨瀏覽器默認樣式,修正已知 Bug,保留有用默認值 */
早期 CSS 開發充斥 Hack,現代開發遵循:**編寫標準代碼 → 構建工具自動適配 → @supports 優雅降級 → Polyfill 填補行為缺口**。理解歷史差異是為了更好地欣賞現代標準的優雅,並在維護舊代碼時游刃有餘。