萬盛學電腦網

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

CSS Reset 與 Base 樣式規範:base.css 核心代碼解析、現代 Normalize.css 與自定義重置策略

什麼是 Base.css / Reset.css?

消除瀏覽器預設樣式差異、建立一致渲染基線、定義專案通用工具類的**基礎樣式文件**。現代專案常分為:

  • Reset.css:極簡重置(margin/padding/border/list-style 歸零)。
  • Normalize.css:保留有用預設、修正瀏覽器 Bug、統一語義元素樣式。
  • Base.css:專案定制,包含 Reset/Normalize + 通用工具類 + 設計令牌。

經典 Reset 代碼解析(梅耶重置精簡版)

/* 1. 核心重置 */
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. HTML5 語義元素顯示修正 */
article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section {
  display: block;
}

/* 3. 列表重置 */
ol, ul { list-style: none; }

/* 4. 表格重置 */
table {
  border-collapse: collapse;
  border-spacing: 0;
}

/* 5. 表單元素重置 */
input, textarea, select, button {
  font: inherit;
  color: inherit;
  margin: 0;
  padding: 0;
  border: none;
  background: none;
  border-radius: 0;
  -webkit-appearance: none;
  appearance: none;
}

現代 Normalize.css 核心價值(推薦直接引用)

  • 保留 sup/sub、code/pre 等有用預設樣式。
  • 修正 audio/video/canvas 顯示問題。
  • 統一 input[type="search"]、button 跨瀏覽器外觀。
  • 修正 fieldset、legend 間距 Bug。
  • 設定 html { line-height: 1.15; -webkit-text-size-adjust: 100%; }。
/* 安裝 */
npm install normalize.css
/* 引入 */
@import 'normalize.css';
/* 或 CDN */
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/normalize.css@8.0.1/normalize.min.css">

專案級 Base.css 結構建議

/* base.css */
@import 'normalize.css';           /* 1. 瀏覽器一致性基線 */
@import 'design-tokens.css';       /* 2. 設計令牌:色彩、間距、字體、斷點 */
@import 'typography.css';          /* 3. 字體系統:標題、正文、代碼、鏈接 */
@import 'utilities.css';           /* 4. 工具類:間距、顯示、定位、Flex/Grid 輔助 */
@import 'components-base.css';     /* 5. 組件基礎:按鈕、表單、表格、卡片重置 */

設計令牌範例 (design-tokens.css)

:root {
  /* 色彩 */
  --color-primary: #3498db;
  --color-primary-hover: #2980b9;
  --color-text: #2c3e50;
  --color-text-muted: #7f8c8d;
  --color-border: #ecf0f1;
  --color-bg: #ffffff;
  --color-bg-alt: #f8f9fa;

  /* 間距 */
  --space-xs: 0.25rem;  /* 4px */
  --space-sm: 0.5rem;   /* 8px */
  --space-md: 1rem;     /* 16px */
  --space-lg: 1.5rem;   /* 24px */
  --space-xl: 2rem;     /* 32px */
  --space-2xl: 3rem;    /* 48px */

  /* 字體 */
  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", "Microsoft JhengHei", sans-serif;
  --font-mono: "SF Mono", "Fira Code", monospace;
  --font-size-base: 1rem;
  --line-height-base: 1.6;

  /* 圓角 */
  --radius-sm: 2px;
  --radius-md: 4px;
  --radius-lg: 8px;
  --radius-full: 9999px;

  /* 陰影 */
  --shadow-sm: 0 1px 2px rgba(0,0,0,.05);
  --shadow-md: 0 4px 6px rgba(0,0,0,.1);
  --shadow-lg: 0 10px 15px rgba(0,0,0,.1);

  /* 斷點 */
  --bp-sm: 640px;
  --bp-md: 768px;
  --bp-lg: 1024px;
  --bp-xl: 1280px;
}

通用工具類模式 (utilities.css)

/* 顯示 */
.d-none { display: none !important; }
.d-block { display: block !important; }
.d-flex { display: flex !important; }
.d-inline-flex { display: inline-flex !important; }
.d-grid { display: grid !important; }

/* Flex 輔助 */
.flex-1 { flex: 1 1 auto; }
.flex-auto { flex: 1 1 auto; }
.flex-initial { flex: 0 1 auto; }
.flex-none { flex: none; }
.justify-center { justify-content: center; }
.items-center { align-items: center; }
.gap-sm { gap: var(--space-sm); }
.gap-md { gap: var(--space-md); }

/* 間距 */
.m-0 { margin: 0 !important; }
.mt-md { margin-top: var(--space-md) !important; }
.mb-lg { margin-bottom: var(--space-lg) !important; }
.mx-auto { margin-left: auto !important; margin-right: auto !important; }
.p-md { padding: var(--space-md) !important; }

/* 文字 */
.text-center { text-align: center !important; }
.text-left { text-align: left !important; }
.text-right { text-align: right !important; }
.fw-bold { font-weight: 700 !important; }
.fs-sm { font-size: 0.875rem !important; }
.lh-base { line-height: var(--line-height-base) !important; }

/* 定位 */
.relative { position: relative !important; }
.absolute { position: absolute !important; }
.fixed { position: fixed !important; }
.sticky { position: sticky !important; }

/* 邊框/圓角/陰影 */
.rounded { border-radius: var(--radius-md) !important; }
.rounded-lg { border-radius: var(--radius-lg) !important; }
.shadow { box-shadow: var(--shadow-md) !important; }
.border { border: 1px solid var(--color-border) !important; }

避坑指南與最佳實踐

  1. 不要過度重置:保留 strong/em、code/pre、blockquote 等語義樣式。
  2. 避免全局 * 選擇器重置:* { margin:0; padding:0 } 破壞表單元素原生外觀、效能差。
  3. Box-sizing 統一:*, *::before, *::after { box-sizing: border-box; } 放在最頂層。
  4. 設計令牌優先:所有數值(顏色、間距、字體、圓角、陰影、斷點)抽離為 CSS 變數。
  5. 工具類命名規範:採用 property[-side]-size 格式(如 mt-md、px-lg),配合 !important 確保覆蓋。
  6. 版本管理:Base.css 列入版本控制,重大變更發布 Major 版本。

總結

Base.css 是專案 CSS 架構的基石。**現代最佳實踐:Normalize.css + 設計令牌 + 字體系統 + 功能性工具類**。拒絕傳統大而全的重置文件,改用模組化、可配置、語義化的基礎樣式體系,配合 PostCSS/Stylelint 自動化檢查,確保團隊風格一致、維護低成本、擴展高彈性。

copyright © 萬盛學電腦網 all rights reserved