萬盛學電腦網

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

CSS Reset / Normalize 基礎樣式表:Base.css 現代化重構指南

為什麼需要基礎樣式表?

瀏覽器預設樣式不一致(margin、padding、font-size、list-style 等),導致跨瀏覽器差異。基礎樣式表統一基準,分為兩大流派:

  • CSS Reset:歸零所有預設,開發者完全掌控
  • Normalize.css:保留有用預設、修正錯誤、統一表現(現代推薦)

現代 Base.css 核心內容

1. 盒模型統一

*, *::before, *::after {
  box-sizing: border-box;  /* 寬高包含 padding + border */
}

2. 邊距歸零與基礎排版

body, h1, h2, h3, h4, h5, h6, p, ul, ol, li, 
figure, blockquote, dl, dd, pre, form, fieldset, legend, textarea {
  margin: 0;
  padding: 0;
}

body {
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", 
               "Noto Sans TC", "Microsoft JhengHei", sans-serif;
  font-size: 16px;
  line-height: 1.6;
  color: #333;
  background: #fff;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

3. 列表與表格重置

ul, ol { list-style: none; }
table { border-collapse: collapse; border-spacing: 0; }
th, td { text-align: left; vertical-align: top; }

4. 圖片與媒體元素

img, video, canvas, audio, iframe, embed, object {
  display: block;
  max-width: 100%;
  height: auto;
  border: 0;
}

5. 表單元素統一

input, button, select, textarea {
  font: inherit;           /* 繼承字體 */
  color: inherit;          /* 繼承顏色 */
  border: none;
  background: none;
  border-radius: 0;        /* 移除 iOS 預設圓角 */
  -webkit-appearance: none; /* 移除原生外觀 */
}

button, input[type="submit"], input[type="button"] {
  cursor: pointer;
}

:focus-visible {
  outline: 2px solid #007bff;
  outline-offset: 2px;
}

6. 語意化標籤預設

article, aside, details, figcaption, figure, footer, header, 
main, menu, nav, section, summary { display: block; }

h1, h2, h3, h4, h5, h6 { font-weight: 600; line-height: 1.3; }
h1 { font-size: 2.5rem; }
h2 { font-size: 2rem; }
h3 { font-size: 1.5rem; }
h4 { font-size: 1.25rem; }
h5 { font-size: 1rem; }
h6 { font-size: 0.875rem; }

a { color: inherit; text-decoration: none; }
a:hover { text-decoration: underline; }

code, pre { font-family: "SF Mono", "Fira Code", monospace; font-size: 0.9em; }

實用工具類別

/* 文字 */
.text-center { text-align: center; }
.text-left { text-align: left; }
.text-right { text-align: right; }
.fw-bold { font-weight: 700; }
.fs-sm { font-size: 0.875rem; }
.fs-lg { font-size: 1.25rem; }

/* 間距系統 (4px 基準) */
.m-0 { margin: 0; } .mt-1 { margin-top: 4px; } .mb-2 { margin-bottom: 8px; }
.p-0 { padding: 0; } .px-3 { padding-left: 12px; padding-right: 12px; }

/* 佈局 */
.d-flex { display: flex; }
.flex-1 { flex: 1; }
.justify-between { justify-content: space-between; }
.align-center { align-items: center; }
.grid { display: grid; }
.gap-4 { gap: 16px; }

/* 顯示/隱藏 */
.d-none { display: none; }
.sr-only { 
  position: absolute; width: 1px; height: 1px; 
  padding: 0; margin: -1px; overflow: hidden; 
  clip: rect(0,0,0,0); white-space: nowrap; border: 0; 
}

/* 清除浮動 */
.clearfix::after { content: ""; display: table; clear: both; }

引入順序與架構建議

/* styles.css */
@import "base/reset.css";      /* Normalize/Reset */
@import "base/variables.css";  /* CSS 變數:色彩、間距、斷點 */
@import "base/typography.css"; /* 字體系統 */
@import "layout/grid.css";     /* 佈局系統 */
@import "components/";         /* 元件樣式 */
@import "utilities.css";       /* 工具類別(最後載入,最高優先權) */

推薦成熟方案

copyright © 萬盛學電腦網 all rights reserved