萬盛學電腦網

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

CSS Reset 重置樣式最佳實務:從傳統重置到 Modern Normalize 的演進選擇

什麼是 CSS Reset?

瀏覽器預設樣式不一致(User Agent Stylesheet 差異),Reset 目的是建立一致的跨瀏覽器基準線。

三代重置方案演進

第一代:硬核重置(Eric Meyer Reset / * 選擇器)

/* 極簡版 - 不推薦生產環境 */
* {
  margin: 0;
  padding: 0;
}

/* Eric Meyer Reset 2.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;
}

缺點:破壞表單元素原生樣式、效能差、需大量覆寫。

第二代:Normalize.css(推薦主流)

  • 保留有用的預設樣式(如 sup, sub, blockquote)
  • 修正瀏覽器 Bug(HTML5 元素、表單、表格)
  • 模組化,可按需引入
  • 文件完整,解釋每條規則用途

安裝:npm install normalize.css → @import 'normalize.css';

第三代:Modern CSS Reset / Reset CSS(現代最佳)

/* Josh Comeau Modern CSS Reset 精簡版 */
*, *::before, *::after {
  box-sizing: border-box;
}

* {
  margin: 0;
}

html, body {
  height: 100%;
}

body {
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

img, picture, video, canvas, svg {
  display: block;
  max-width: 100%;
}

input, button, textarea, select {
  font: inherit;
}

p, h1, h2, h3, h4, h5, h6 {
  overflow-wrap: break-word;
}

#root, #__next {
  isolation: isolate;
}

方案選擇決策樹

專案類型 → 推薦方案
├─ 新專案、現代技術棧、團隊熟悉 CSS 變數 → Modern CSS Reset
├─ 需支援 IE11、大型既有專案遷移 → Normalize.css
├─ 極簡專案、原型驗證 → * { margin:0; padding:0; box-sizing:border-box; }
└─ Email 模板、極度受限環境 → 內聯樣式 + 精簡重置

自訂重置最佳實務

/* project-reset.css */
:root {
  /* 設計系統 Tokens */
  --color-text: #1a1a1a;
  --color-bg: #ffffff;
  --font-sans: "Noto Sans TC", system-ui, sans-serif;
  --line-height: 1.6;
}

/* 1. 盒模型統一 */
*, *::before, *::after {
  box-sizing: border-box;
}

/* 2. 移除預設間距 */
* {
  margin: 0;
  padding: 0;
}

/* 3. 基礎排版 */
html {
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
}

body {
  min-height: 100vh;
  font-family: var(--font-sans);
  font-size: 1rem;
  line-height: var(--line-height);
  color: var(--color-text);
  background-color: var(--color-bg);
  text-rendering: optimizeLegibility;
}

/* 4. 媒體元素預設 */
img, picture, video, canvas, svg {
  display: block;
  max-width: 100%;
  height: auto;
}

/* 5. 表單元素字體繼承 */
input, button, textarea, select {
  font: inherit;
  color: inherit;
}

/* 6. 文字換行優化 */
p, h1, h2, h3, h4, h5, h6 {
  overflow-wrap: break-word;
  hyphens: auto;
}

/* 7. 連結無障礙 */
a:not([class]) {
  text-underline-offset: 0.1em;
}

a:focus-visible {
  outline: 2px solid currentColor;
  outline-offset: 2px;
}

整合建議

  • 建立專案專屬 reset.css 或 _reset.scss
  • 作為樣式入口最先引入
  • 搭配 CSS 變數管理設計系統 Tokens
  • 定期同步 Normalize.css / Modern Reset 更新
copyright © 萬盛學電腦網 all rights reserved