每個瀏覽器都內建一套「使用者代理程式樣式表」,為 HTML 元素提供預設樣式。了解這些預設值,能幫助你在撰寫 Reset/Normalize 時做出更精準的決策,避免跨瀏覽器差異。
| 屬性 | 預設值 | 備註 |
|---|---|---|
margin | 0 | 大多數區塊元素預設有外距(如 body、h1-h6、p、ul、ol、blockquote、figure 等) |
padding | 0 | 極少數元素(如 button、input、select、textarea、table、th/td)有預設內距 |
background-color | transparent | 所有元素預設透明背景 |
border | none / medium / currentColor | 預設無邊框;若設定 border-style 則寬度為 medium(約 3-4px),顏色為文字色 |
font-size | medium(約 16px) | 根元素 html 預設 16px,其他元素繼承或依標題層級縮放 |
line-height | normal(約 1.2) | 瀏覽器依字體計算 |
display | 依元素類型(block/inline/inline-block/table 等) | |
list-style | disc outside none | ul/ol 的 li 預設實心圓項目符號 |
quotes | 依語系 | q 元素預設引號 |
雖然 W3C 有建議預設樣式,但各瀏覽器實作細節仍有出入,例如:
body 的 margin:Chrome/Firefox 約 8px,IE 可能不同。input、select、button)的 padding、border、font 差異極大。hr、pre、code 的字體、間距也各異。不要盲目使用通用選擇器重置(* { margin:0; padding:0; }),這會影響不需重置的元素(如 button、input 的原生外觀)且效能較差。建議採用:
/* 僅重置專案會用到的元素 */
body, h1, h2, h3, h4, h5, h6, p, ul, ol, li, figure, blockquote, dl, dd,
table, th, td, form, fieldset, legend, input, textarea, button, select {
margin: 0;
padding: 0;
}
img, video, canvas { display: block; max-width: 100%; }
ul, ol { list-style: none; }
a { text-decoration: none; color: inherit; }
button, input, select, textarea { font: inherit; color: inherit; }
開發者工具 → Elements → Computed 面板,勾選「Show all」,即可看到該元素所有屬性的「User Agent Stylesheet」預設值來源。
了解預設值,才能寫出精準、輕量、跨瀏覽器一致的 CSS。