* { margin:0; padding:0; }?input、button、select 等表單元素失去原生外觀,需大量代碼恢復hr、pre、blockquote 等語義元素默認間距被清零,語義丟失| 方案 | 定位 | 體積 | 特點 | 推薦場景 |
|---|---|---|---|---|
| Eric Meyer Reset 2.0 | 硬重置 | ~1.2KB | 歸零所有差異,提供乾淨畫布 | 高度定制設計系統、需精確像素控制 |
| Normalize.css v8 | 標準化 | ~4.5KB | 保留有用默認、修正瀏覽器 Bug、HTML5 元素支援 | 通用專案、團隊協作、現代瀏覽器 |
| Modern CSS Reset (Andy Bell) | 現代極簡 | ~0.8KB | 針對現代瀏覽器、配合 box-sizing: border-box | 綠地項目、極簡主義 |
| Sanitize.css | 增強標準化 | ~3KB | Normalize 基礎上添加常用基礎樣式 | 快速原型、內部工具 |
/* 1. 結構元素歸零 */
body, h1, h2, h3, h4, h5, h6, hr, p, blockquote,
dl, dt, dd, ul, ol, li,
pre,
form, fieldset, legend, button, input, textarea,
th, td,
img {
margin: 0;
padding: 0;
border: 0; /* 關鍵:img 去除邊框 */
font-size: 100%; /* 繼承字體大小 */
font: inherit; /* 繼承字體家族 */
vertical-align: baseline;
}
/* 2. 列表樣式重置 */
ul, ol { list-style: none; }
/* 3. 表單元素統一 */
button, input, select, textarea {
font: inherit; /* 關鍵:繼承父級字體 */
margin: 0; /* 修正 IE margin Bug */
}
/* 4. 表格合併邊框 */
table {
border-collapse: collapse;
border-spacing: 0;
}
/* 5. 連結重置 */
a { text-decoration: none; color: inherit; }
a:hover { text-decoration: underline; }
sup/sub、code/pre、blockquote 等語義元素默認樣式audio/video/canvas 等 HTML5 元素顯示input[type="search"] 盒模型、button 字體繼承svg:not(:root) 溢出隱藏// postcss.config.js
module.exports = {
plugins: {
'postcss-normalize': { /* 自動注入目標瀏覽器所需 normalize */ },
'postcss-preset-env': { stage: 0 }, // 包含自動前綴、現代語法降級
cssnano: { preset: 'default' } // 壓縮時保留 @license 註釋
}
};
/* main.css */
@import 'normalize.css/normalize.css'; /* 構建工具自動內聯 */
/* 或 ES Module 風格 */
import 'normalize.css'; // Webpack/Vite/Rollup 自動處理
:root {
--font-sans: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif;
--font-mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, monospace;
}
*, *::before, *::after { box-sizing: border-box; }
html {
font-size: 62.5%; /* 1rem = 10px */
-webkit-text-size-adjust: 100%; /* 禁止 iOS 文字縮放 */
}
body {
margin: 0;
font-family: var(--font-sans);
line-height: 1.6;
color: #1a1a1a;
background: #fff;
}
img, video, canvas, svg {
display: block;
max-width: 100%;
height: auto;
}
input, button, textarea, select { font: inherit; }
a { color: inherit; text-decoration: none; }
/* 專案特定重置 */
.your-component { /* ... */ }
需要支援 IE11? → 是 → Normalize.css / Eric Meyer Reset
↓ 否
追求極簡體積? → 是 → Modern CSS Reset / 自定義最小集
↓ 否
需要表單統一樣式? → 是 → Sanitize.css
↓ 否
Normalize.css (最穩妥通用選擇)
Reset 不是越多越好,而是精準匹配項目瀏覽器支援範圍與設計系統需求。現代綠地項目推薦:postcss-normalize + 自定義設計令牌基礎樣式。