萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網頁製作 >> Div CSS 教學

CSS 預設樣式值 (User Agent Stylesheet) 與 Reset 策略

常見預設值差異

元素/屬性Chrome/FirefoxSafariIE/Edge Legacy備註
body margin8px8px15px / 10px需統一歸零
ul, ol padding40px (left)40px20px / 0列表縮進差異大
h1-h6 font-size2em / 1.5em / 1.17em / 1em / 0.83em / 0.67em類似類似相對單位受瀏覽器預設字體影響
button, input font不繼承 body不繼承不繼承需 font:inherit
background-colortransparenttransparenttransparent一致

Reset 策略比較

1. 硬核 Reset (Eric Meyer Reset)

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;
}

徹底歸零,但需大量重寫樣式、檔案大。

2. Normalize.css (推薦)

  • **保留有用的預設** (如 sup 上標、sub 下標、b 粗體)
  • **修正瀏覽器 Bug** (HTML5 元素顯示、表單一致性、圖片垂直對齊等)
  • **模組化**、文檔完整、社群維護活躍
  • 安裝:npm install normalize.css → @import "normalize.css";

3. 精簡自訂 Reset (專案級)

/* 專案常用最小集 */
*, *::before, *::after { box-sizing:border-box; }
html { -webkit-text-size-adjust:100%; -moz-tab-size:4; tab-size:4; }
body { margin:0; line-height:1.5; font-family:system-ui, sans-serif; }
img, video, canvas { display:block; max-width:100%; height:auto; }
ul, ol { margin:0; padding:0; list-style:none; }
a { color:inherit; text-decoration:none; }
button, input, select, textarea { font:inherit; color:inherit; }
table { border-collapse:collapse; }

建議選擇

  • **新專案**:直接引入 Normalize.css + 專案精簡補充。
  • **舊專案遷移**:逐步替換硬核 Reset 為 Normalize,避免大範圍樣式崩壞。
  • **極簡專案 / 原型**:僅用上述「精簡自訂 Reset」。

開發工具檢視 UA 樣式

  • Chrome DevTools → Elements → Computed → 勾選 "Show all" → 灰色字體即 UA 樣式
  • Firefox → Inspector → Computed → "Browser styles"
copyright © 萬盛學電腦網 all rights reserved