為何拒絕 * { margin:0; padding:0 }?
- 效能:通配符匹配所有元素,瀏覽器需遍歷整個 DOM 樹
- 副作用:破壞表單元素預設外觀 (input、button、select)、移除列表語意、影響
<hr>、<pre> 等元素預設樣式
- 繼承汙染:強制繼承導致後續樣式難以預測
現代 Reset 策略對比
| 方案 | 特點 | 適用場景 | 體積 |
| Normalize.css | 保留有用預設、修正瀏覽器差異、模組化 | 傳統專案、需保留語意樣式 | ~4KB |
| Modern CSS Reset (Andy Bell) | 極簡、現代屬性、含 box-sizing、圖片響應式 | 新專案、綠地開發 | ~1KB |
| Custom Reset | 專案定製、僅重置實際用到的元素 | 成熟團隊、設計系統 | 可控 |
Modern CSS Reset 完整版 (推薦基礎)
/* 1. 盒模型重置 */
*, *::before, *::after {
box-sizing: border-box;
}
/* 2. 移除預設邊距 */
* {
margin: 0;
padding: 0;
}
/* 3. 圖片響應式預設 */
img, picture, video, canvas, svg {
display: block;
max-width: 100%;
height: auto;
}
/* 4. 表單元素字體繼承 */
input, button, textarea, select {
font: inherit;
}
/* 5. 移除列表預設樣式 (僅作用於列表語境) */
ul[role="list"], ol[role="list"] {
list-style: none;
}
/* 6. 核心體排版基準 */
html {
font-size: 100%; /* 尊重使用者設定 */
line-height: 1.5;
-webkit-text-size-adjust: 100%; /* 防止 iOS 橫屏放大 */
}
body {
min-height: 100vh;
text-rendering: optimizeLegibility;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
/* 7. 連結無障礙 */
a:not([class]) {
text-decoration-skip-ink: auto;
}
/* 8. 圖片無障礙 */
img {
vertical-align: middle;
}
/* 9. 表單無障礙 */
button, input, select, textarea {
-webkit-appearance: none;
appearance: none;
}
專案級客製化 Reset (YUI/Eric Meyer 風格精簡版)
/* 結構元素 */
body, h1, h2, h3, h4, h5, h6, hr, p, blockquote,
dl, dt, dd, ul, ol, li,
pre, figure, figcaption,
form, fieldset, legend, button, input, textarea,
th, td,
img {
margin: 0;
padding: 0;
border: 0;
}
/* 字體基準 */
body, button, input, select, textarea {
font: 16px/1.5 system-ui, -apple-system, 'Segoe UI', Roboto, 'Noto Sans TC', sans-serif;
}
h1, h2, h3, h4, h5, h6 { font-size: 100%; font-weight: inherit; }
em, i { font-style: normal; } /* 中文常不需要斜體 */
/* 列表 */
ul, ol { list-style: none; }
/* 連結 */
a { color: inherit; text-decoration: none; }
a:hover { text-decoration: underline; }
/* 圖片 */
img { border: 0; vertical-align: middle; }
/* 表格 */
table { border-collapse: collapse; border-spacing: 0; }
建置整合
/* main.css */
@import "reset.css"; /* 或 @tailwind base; */
/* 使用 PostCSS 開發環境不壓縮,生產環境自動合併壓縮 */