`、`` 邊框合併失效
過度重置:許多元素本身 margin/padding 為 0,無需重置
繼承問題:導致 `font: inherit` 等現代最佳實踐難以生效
現代 Reset 三大流派
1. Normalize.css (業界標準、保留語意)
2. Josh Comeau's Modern CSS Reset (現代、精簡)
*, *::before, *::after { box-sizing: border-box; }
* { margin: 0; } /* 僅 margin,保留 padding 由組件控制 */
html { -webkit-text-size-adjust: 100%; }
body {
line-height: 1.5;
-webkit-font-smoothing: antialiased;
min-height: 100vh;
}
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; }
3. Andy Bell's "A Modern CSS Reset" (平衡派)
/* Box sizing */
*, *::before, *::after { box-sizing: border-box; }
/* Remove default margin */
* { margin: 0; }
/* Body defaults */
body {
line-height: 1.5;
-webkit-font-smoothing: antialiased;
min-height: 100vh;
}
/* Image defaults */
img, picture, video, canvas, svg {
display: block;
max-width: 100%;
}
/* Form elements */
input, button, textarea, select { font: inherit; }
/* Text wrapping */
p, h1, h2, h3, h4, h5, h6 { overflow-wrap: break-word; }
/* Root stacking context */
#root, #__next { isolation: isolate; }
自訂企業級 Base.css 架構
/* ========================================
1. CSS Variables (設計令牌)
======================================== */
:root {
/* Color */
--color-primary: #2563eb;
--color-primary-hover: #1d4ed8;
--color-text: #111827;
--color-text-muted: #6b7280;
--color-border: #e5e7eb;
--color-bg: #ffffff;
--color-bg-muted: #f9fafb;
/* Spacing (4px base) */
--space-1: 4px; --space-2: 8px; --space-3: 12px;
--space-4: 16px; --space-5: 20px; --space-6: 24px;
--space-8: 32px; --space-10: 40px; --space-12: 48px;
/* Typography */
--font-family-base: "Noto Sans TC", system-ui, sans-serif;
--font-family-mono: "JetBrains Mono", "Fira Code", monospace;
--font-size-xs: 0.75rem; --font-size-sm: 0.875rem;
--font-size-base: 1rem; --font-size-lg: 1.125rem;
--font-size-xl: 1.25rem; --font-size-2xl: 1.5rem;
--font-size-3xl: 1.875rem; --font-size-4xl: 2.25rem;
--line-height-tight: 1.2; --line-height-base: 1.6;
--line-height-loose: 1.8;
/* Border Radius */
--radius-sm: 4px; --radius-md: 6px; --radius-lg: 8px; --radius-full: 9999px;
/* Shadows */
--shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
--shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1);
--shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.1);
/* Transitions */
--transition-fast: 100ms ease;
--transition-base: 200ms ease;
--transition-slow: 300ms ease;
}
/* ========================================
2. Normalize / Reset (引入或內聯)
======================================== */
@import "normalize.css";
/* 或內聯上述 Modern Reset 核心規則 */
/* ========================================
3. 基礎排版
======================================== */
html { font-size: 16px; }
body {
font-family: var(--font-family-base);
font-size: var(--font-size-base);
line-height: var(--line-height-base);
color: var(--color-text);
background: var(--color-bg);
}
h1, h2, h3, h4, h5, h6 {
font-weight: 600;
line-height: var(--line-height-tight);
color: var(--color-text);
}
h1 { font-size: var(--font-size-4xl); }
h2 { font-size: var(--font-size-3xl); }
h3 { font-size: var(--font-size-2xl); }
h4 { font-size: var(--font-size-xl); }
h5 { font-size: var(--font-size-lg); }
h6 { font-size: var(--font-size-base); }
a { color: var(--color-primary); text-decoration: none; }
a:hover { text-decoration: underline; }
/* ========================================
4. 表單基礎 (配合元件層擴充)
======================================== */
input, button, select, textarea {
font-family: inherit;
font-size: inherit;
line-height: inherit;
color: inherit;
}
/* ========================================
5. 通用工具類 (高優先權,最後載入)
======================================== */
.sr-only { /* 隱藏但可讀 */ }
.visually-hidden { /* 同 sr-only */ }
.text-center { text-align: center; }
.d-flex { display: flex; }
.flex-1 { flex: 1; }
.gap-4 { gap: var(--space-4); }
/* ... 間距、顯示、定位工具類 */
引入順序架構 (ITCSS 思想)
@import "settings/variables"; /* 1. 設計令牌 */
@import "tools/mixins"; /* 2. 函數混入 */
@import "generic/normalize"; /* 3. Reset/Normalize */
@import "generic/box-sizing"; /* 4. 盒模型統一 */
@import "elements/typography"; /* 5. 裸標籤樣式 */
@import "elements/forms"; /* 6. 表單基礎 */
@import "objects/container"; /* 7. 佈局物件 */
@import "objects/grid"; /* 8. 網格系統 */
@import "components/"; /* 9. UI 元件 (按資料夾) */
@import "utilities/"; /* 10. 高優先權工具類 */
避坑檢查清單
- ❌ 使用 `* { margin:0; padding:0; }` 通用重置
- ❌ 重置 `font-family` 導致表單字體異常
- ❌ 重置 `line-height: 1` 破壞垂直節奏
- ❌ 重置 `list-style` 導致語意列表失去符號
- ✅ 使用 `box-sizing: border-box` 通用
- ✅ 僅重置有差異的屬性 (margin、border-collapse 等)
- ✅ 表單元素 `font: inherit` 而非重置字體
- ✅ 保留 `sup`/`sub`、`abbr`、`dfn` 等語意元素預設
copyright © 萬盛學電腦網 all rights reserved
|