瀏覽器差異是前端開發永恆的課題。本文匯總多年實戰經驗,涵蓋 Reset/Normalize、盒模型統一、廠商前綴、特性查詢、現代 Polyfill 策略,構建穩健兼容體系。
/* 重置所有元素 margin/padding,統一盒模型基線 */
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
}
/* HTML5 語義標籤顯示為塊級 */
article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section {
display: block;
}
body { line-height: 1; }
ol, ul { list-style: none; }
blockquote, q { quotes: none; }
blockquote:before, blockquote:after, q:before, q:after { content: ''; content: none; }
table { border-collapse: collapse; border-spacing: 0; }
input 行高、svg 溢出、表單元素一致性)npm install normalize.css → import 'normalize.css'*, *::before, *::after {
box-sizing: border-box; /* 現代標準,IE8+ 全支持 */
}
body {
-webkit-font-smoothing: antialiased; /* macOS/iOS 字體平滑 */
-moz-osx-font-smoothing: grayscale; /* Firefox macOS */
text-rendering: optimizeLegibility; // 輔助字距/連字 (慎用,性能敏感)
}
* {
-webkit-tap-highlight-color: transparent; /* iOS Safari/Chrome Android */
-webkit-touch-callout: none; /* 禁用長按菜單 */
-webkit-user-select: none; /* 禁用選中 (按需) */
user-select: none;
}
input, textarea, select, button {
-webkit-appearance: none; /* 關鍵:移除 iOS/Android 原生圓角/陰影/漸變 */
-moz-appearance: none;
appearance: none;
border-radius: 0; /* 配合 appearance:none 徹底重置 */
background: #fff;
border: 1px solid #ddd;
font: inherit; /* 繼承字體 */
color: inherit;
}
img, video, canvas, audio, iframe {
vertical-align: middle; /* 或 bottom / top */
/* display: block; */ /* 替代方案:轉為塊級 */
}
:focus {
outline: none; /* 移除默認藍框 */
}
:focus-visible { /* 現代標準:僅鍵盤聚焦顯示 */
outline: 2px solid #007bff;
outline-offset: 2px;
}
/* 兼容不支持 :focus-visible 的舊瀏覽器 */
.js-focus-visible :focus:not(.focus-visible) {
outline: none;
}
.clearfix::after {
content: '';
display: table;
clear: both;
}
/* 或現代寫法 */
.clearfix { display: flow-root; } /* IE 不支持,需回退 */
.fl { float: left; }
.fr { float: right; }
.tal { text-align: left; }
.tar { text-align: right; }
.tac { text-align: center; }
.ofh { overflow: hidden; }
.ofa { overflow: auto; }
.dn { display: none; }
.db { display: block; }
.dib { display: inline-block; }
.df { display: flex; }
.dg { display: grid; }
.m0a { margin: 0 auto; }
.p0 { padding: 0; }
.bn { border: 0; }
.rel { position: relative; }
.abs { position: absolute; }
.fixed { position: fixed; }
.z1 { z-index: 1; }
.z999 { z-index: 999; }
/* package.json */
"browserslist": [
"> 1%",
"last 2 versions",
"not dead",
"not ie <= 11" /* 如需支持 IE11 移除此行 */
]
/* postcss.config.js */
module.exports = {
plugins: {
autoprefixer: {}
}
};
**寫標準語法,構建時自動添加前綴**,無需手寫。
| 特性 | 標準 | WebKit/Blink | Firefox | IE/Edge |
|---|---|---|---|---|
| 漸變 | linear-gradient() | -webkit-linear-gradient() | -moz-linear-gradient() | -ms-linear-gradient() (IE10+) |
| 轉換 | transform | -webkit-transform | -moz-transform | -ms-transform (IE9) |
| 動畫 | animation | -webkit-animation | -moz-animation | IE10+ 無前綴 |
| 彈性盒 | display: flex | display: -webkit-flex | 無 (舊語法 -moz-box) | display: -ms-flexbox (IE10) |
| 邊框圖片 | border-image | -webkit-border-image | -moz-border-image | IE11+ 無前綴 |
/* 檢測支持再應用高級特性 */
@supports (display: grid) {
.layout { display: grid; grid-template-columns: 1fr 2fr; }
}
/* 不支持 Grid 回退 Flex */
@supports not (display: grid) {
.layout { display: flex; }
.sidebar { flex: 0 0 280px; }
.main { flex: 1; }
}
/* 檢測多條件 */
@supports (display: flex) and (gap: 1rem) {
.flex-gap { gap: 1rem; } /* 無需 margin hack */
}
/* 檢測自定義屬性 (CSS Variables) */
@supports (--custom: property) {
:root { --main-color: #007bff; }
.btn { background: var(--main-color); }
}
/* 檢測特定值 */
@supports (background: url("img.webp") format("webp")) {
.hero { background-image: url("hero.webp"); }
}
/* 屬性 Hack */
.ie-hack {
color: red; /* 所有瀏覽器 */
_color: blue; /* IE6 只有下劃線前綴生效 */
*color: green; /* IE6/7 識別星號 */
color: orange\9; /* IE8/9/10 識別 \9 (IE10+ 部分版本失效) */
}
/* 選擇器 Hack */
:root .selector { color: red; } /* IE9+ 及現代瀏覽器 */
* html .selector { color: blue; } /* IE6 */
*+html .selector { color: green; } /* IE7 */
@media all and (-ms-high-contrast: none), (-ms-high-contrast: active) {
.selector { color: orange; } /* IE10/11 */
}
@media screen and (-webkit-min-device-pixel-ratio:0) {
.selector { color: purple; } /* WebKit/Blink (Chrome/Safari/新Edge) */
}
@-moz-document url-prefix() {
.selector { color: brown; } /* Firefox */
}
<!-- polyfill.io 智能服務:根據 UA 返回所需 polyfill -->
<script src="https://polyfill.io/v3/polyfill.min.js?features=Element.prototype.closest,NodeList.prototype.forEach,Promise,Object.assign,Array.prototype.includes"></script>
<!-- 或本地構建:core-js + regenerator-runtime -->
import 'core-js/stable';
import 'regenerator-runtime/runtime';