各瀏覽器對 HTML 元素有不同的預設樣式(user agent stylesheet),例如 body 有 margin、ul 有 padding-left、h1-h6 字體大小不同。CSS Reset 旨在「歸零」這些差異,建立一致的跨瀏覽器基準。
* { margin: 0; padding: 0; }
缺點:
input、button、select 等表單元素也被重置,導致原生外觀消失,需大量補回ul/ol 失去縮排、sup/sub 失去垂直對齊/* https://meyerweb.com/eric/tools/css/reset/ v2.0 */
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;
}
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; }
Normalize.css 不盡歸零,而是:
sup/sub、input 聚焦樣式)audio:not([controls]) 隱藏、button 字體繼承)/* 安裝方式 */
npm install normalize.css
/* 或 CDN */
@import url('https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.1/normalize.min.css');
/* 1. 引入 Normalize.css */
@import 'normalize.css';
/* 2. 專案通用基礎樣式 */
:root {
--font-base: 16px;
--line-height-base: 1.5;
}
html { font-size: var(--font-base); }
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans TC", sans-serif;
line-height: var(--line-height-base);
color: #212529;
-webkit-font-smoothing: antialiased;
}
img, video { max-width: 100%; height: auto; display: block; }
a { color: inherit; text-decoration: none; }
button, input, select, textarea { font: inherit; color: inherit; }
| 專案類型 | 建議方案 | 理由 |
|---|---|---|
| 傳統企業官網、後台管理 | Eric Meyer Reset | 完全控制、無預設干擾 |
| 現代響應式網站、元件庫 | Normalize.css | 保留語意、無障礙友善、維護成本低 |
| 極簡專案、單頁面 | 自訂輕量 Reset | 僅歸零必要屬性,體積最小 |
奠定基礎樣式後,建議學習 CSS 下拉選單實作技巧,打造互動式導覽介面。