萬盛學電腦網

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

瀏覽器預設 User Agent 樣式表詳解:HTML 元素預設 display、margin、font-size 與繼承行為

為什麼要了解 UA 樣式

  • 重置/Normalize 時需知道預設值。
  • 排查「莫名其妙的間距/字體」根因。
  • 理解 initial / revert 關鍵字行為。

核心預設值速查(W3C HTML5 UA 樣式表節錄)

Display 類型

html, body, div, p, h1-h6, ul, ol, li, table, form, header, footer, section, article, aside, nav, main { display: block; }
li { display: list-item; }
head { display: none; }
table { display: table; }
tr { display: table-row; }
thead { display: table-header-group; }
tbody { display: table-row-group; }
tfoot { display: table-footer-group; }
td, th { display: table-cell; }
caption { display: table-caption; }

Margin / Font-size(重點)

body { margin: 8px; line-height: 1.12; }
h1 { font-size: 2em; margin: .67em 0; }
h2 { font-size: 1.5em; margin: .75em 0; }
h3 { font-size: 1.17em; margin: .83em 0; }
h4, p, blockquote, ul, fieldset, form, ol, dl, dir, menu { margin: 1.12em 0; }
h5 { font-size: .83em; margin: 1.5em 0; }
h6 { font-size: .75em; margin: 1.67em 0; }
blockquote { margin-left: 40px; margin-right: 40px; }
ol, ul, dir, menu, dd { margin-left: 40px; }

其他常見預設

b, strong { font-weight: bolder; }
i, cite, em, var, address { font-style: italic; }
pre, code, kbd, samp { font-family: monospace; }
pre { white-space: pre; }
sub { vertical-align: sub; }
sup { vertical-align: super; }
table { border-spacing: 2px; }
hr { border: 1px inset; }
a:link, a:visited { text-decoration: underline; }
:focus { outline: thin dotted invert; }

實務建議

  • 使用 Normalize.css 而非暴力 *{margin:0;padding:0},保留有用的預設(如 ul 縮排、strong 加粗)。
  • 針對專案需求,僅覆寫必要屬性(如 body { margin:0; })。
  • 利用開發者工具「Computed」面板查看最終生效值與 UA 來源。
copyright © 萬盛學電腦網 all rights reserved