| 時代 | 目標瀏覽器 | 核心策略 | 典型手段 |
|---|---|---|---|
| 2008-2015 | IE6/7/8/9/10、FF、Chrome、Safari、Opera | 像素級完美一致 | CSS Hack (*zoom, _property, \9)、條件註釋、Polyfill庫 |
| 2015-2020 | IE11、Edge(舊)、現代瀏覽器 | 優雅降級、核心功能可用 | Autoprefixer、@supports特性查詢、Polyfill按需加載 |
| 2020-至今 | 現代瀏覽器基線 (Chrome 90+/FF 88+/Safari 14+/Edge 90+) | 漸進增強、特性檢測 | 原生CSS特性、PostCSS preset-env、Interop 2024基線 |
/* package.json 或 .browserslistrc */
browserslist:
- "> 0.5% in CN" /* 國內市場份額 > 0.5% */
- "not dead" /* 仍在維護的瀏覽器 */
- "not IE 11" /* 明確排除 IE11 */
- "not op_mini all" /* 排除 Opera Mini */
/* PostCSS 配置 */
module.exports = {
plugins: {
'postcss-preset-env': { stage: 3, browsers: 'browserslist' },
autoprefixer: { grid: 'autoplace' } /* Grid 自動前綴 */
}
};
效果:自動添加-webkit-、-moz-前綴,語法降級(gap→margin、aspect-ratio→padding-top hack),零手工Hack。
/* 基礎樣式:所有瀏覽器生效 */
.card { display: block; padding: 16px; border: 1px solid #eee; }
/* 增強:支持 Grid 的瀏覽器啟用雙欄佈局 */
@supports (display: grid) {
.card { display: grid; grid-template-columns: 1fr 2fr; gap: 16px; }
}
/* 增強:支持容器查詢的瀏覽器實現組件級響應式 */
@supports (container-type: inline-size) {
.card-container { container-type: inline-size; }
@container (min-width: 400px) {
.card { grid-template-columns: 1fr 1fr; }
}
}
/* 增強:支持 :has() 的瀏覽器實現父選擇器邏輯 */
@supports selector(:has(.child)) {
.parent:has(.child--active) { border-color: var(--color-primary); }
}
/* ❌ 物理屬性:RTL 需手動覆蓋 */
margin-left: 16px; padding-right: 24px; border-left: 1px solid; text-align: left;
/* ✅ 邏輯屬性:自適應書寫方向 */
margin-inline-start: 16px;
padding-inline-end: 24px;
border-inline-start: 1px solid;
text-align: start; /* LTR=left, RTL=right */
/* 尺寸邏輯屬性 */
inline-size: 300px; /* width (水平書寫) / height (垂直書寫) */
block-size: 200px; /* height / width */
min-inline-size: 100px; /* min-width / min-height */
| 問題 | 舊 Hack 方案 | 現代標準方案 |
|---|---|---|
| 盒模型差異 | * { box-sizing: border-box } + IE hack | 統一 box-sizing: border-box (全瀏覽器支持) |
| Flexbox 老語法 | display: -webkit-box; -webkit-box-flex | Autoprefixer 自動轉換,直接寫標準語法 |
| Grid 佈局 | 不支持 → float 降級 | @supports (display: grid) 漸進增強 |
| CSS 變量 | Less/Sass 變量編譯 | 原生 Custom Properties + PostCSS 降級 (ie11 不支持) |
| 圖片縮放模式 | background-size hack | object-fit: cover; object-position: center; |
| 滾動吸頂 | JS 監聽 scroll 事件 | position: sticky; top: 0; |
| 文本截斷省略號 | -webkit-line-clamp (需 -webkit-box) | 標準 line-clamp (Chrome 94+, Safari 16.4+) |
| 表單原生樣式 | 大量 Hack 隱藏 | appearance: none; + 自定義樣式 |
/* 父容器塌陷問題 */
.parent { overflow: hidden; } /* BFC 清除浮動,副作用:剪切陰影/absolute子元素 */
.parent::after { content: ""; display: table; clear: both; } /* Clearfix Hack */
/* 現代:Flex/Grid 完全替代浮動佈局 */
.layout { display: flex; gap: 24px; }
.sidebar { flex: 0 0 260px; }
.main { flex: 1; min-width: 0; } /* 防止內容撐爆 */
/* 僅在文字環繞圖片場景使用 float */
.article img.pull-left { float: left; margin: 0 16px 16px 0; }
.article img.pull-right { float: right; margin: 0 0 16px 16px; }
.scroll-container {
overflow: auto; /* 內容超出時出現滾動條 */
/* overflow: hidden; */ /* 強制剪切,配合 text-overflow: ellipsis */
/* 避免 IE 老版本「內容沖出容器」bug,現代瀏覽器無此問題 */
}
margin-inline、padding-block 等邏輯屬性結論:拋棄 Hack,擁抱特性查詢與漸進增強。現代CSS工程化鏈路已能自動處理 95%+ 兼容性問題,開發者應專注於「優雅降級策略設計」而非「具體Hack語法記憶」。