/* 差:* { margin:0; padding:0; } 破壞表單元素、效能差 */
/* 好:針對性重置(參考 Normalize.css / Modern CSS Reset) */
*, *::before, *::after { box-sizing: border-box; }
html { -moz-text-size-adjust: none; -webkit-text-size-adjust: none; text-size-adjust: none; }
body, h1, h2, h3, h4, h5, h6, p, figure, blockquote, dl, dd { margin: 0; }
ul, ol { list-style: none; padding: 0; margin: 0; }
img, picture, video, canvas, svg { display: block; max-width: 100%; }
input, button, textarea, select { font: inherit; }
a { text-decoration: none; color: inherit; }
| 反模式 | 推薦 | 理由 |
|---|---|---|
.red-btn | .btn-primary | 顏色變更時 class 名失真 |
.left-sidebar | .sidebar + 佈局類控制位置 | 位置可能改變 |
.font-14 | .text-sm / 設計系統 token | 尺寸調整時 class 名失真 |
.clear | .clearfix / flow-root | 語意不明 |
採用 BEM:.block__element--modifier;狀態類用 is-/has-;JS 掛載用 js-;工具類用 u-。
/* 之前 */
.card {
margin-top: 16px; margin-right: 24px; margin-bottom: 16px; margin-left: 24px;
padding-top: 12px; padding-right: 16px; padding-bottom: 12px; padding-left: 16px;
border-top: 1px solid #eee; border-right: 1px solid #eee;
border-bottom: 1px solid #eee; border-left: 1px solid #eee;
font-family: system-ui; font-size: 1rem; line-height: 1.5; font-weight: 400;
background-color: #fff; background-image: none; background-repeat: no-repeat;
background-position: center; background-size: cover;
}
/* 之後 */
.card {
margin: 16px 24px;
padding: 12px 16px;
border: 1px solid #eee;
font: 400 1rem/1.5 system-ui;
background: #fff center/cover no-repeat;
}
/* 之前 */
.article h1, .article h2, .article p, .article li { color: #333; font-family: Georgia; }
/* 之後 */
.article { color: #333; font-family: Georgia; }
/* 只在需要覆蓋處單獨設定 */
.article .meta { color: #999; font-size: 0.875rem; }
可繼承屬性:color, font-*, line-height, text-*, letter-spacing, word-spacing, visibility, cursor 等。
/* 之前 */
h1 { font-family: Inter; font-weight: 600; }
h2 { font-family: Inter; font-weight: 600; }
h3 { font-family: Inter; font-weight: 600; }
/* 之後 */
h1, h2, h3 { font-family: Inter; font-weight: 600; }
h1 { font-size: 2.5rem; }
h2 { font-size: 2rem; }
h3 { font-size: 1.5rem; }
/* ==========================================================================
1. Reset & Base
2. Layout
3. Components
3.1 Button
3.2 Card
3.3 Modal
4. Utilities
5. Page Specific
========================================================================== */
/* 2. Layout */
.layout { display: grid; ... }
/* 3.1 Button */
/* ---- Button Base ---- */
.btn { ... }
/* ---- Button Variants ---- */
.btn--primary { ... }
.btn--secondary { ... }
規則:
=== 分隔大區塊。---- 分隔子區塊。.card {
align-items: center;
background: #fff;
border: 1px solid #eee;
border-radius: 8px;
box-shadow: 0 2px 8px rgb(0 0 0 / 0.1);
display: flex;
flex-direction: column;
gap: 16px;
margin: 16px 24px;
padding: 16px;
width: 100%;
}
stylelint-config-rational-order、prettier-plugin-css-order、IDE 插件(CSScomb、Stylelint)。lint-staged + husky 提交前自動檢查修復。高效整潔的 CSS 不是天生,而是規約 + 工具 + 習慣的產物。從 Reset 開始精準、命名語意化、簡寫極致化、繼承最大化、選擇器合併化、註釋結構化、屬性排序化,七步走讓樣式表從「寫時只讀、改時崩潰」進化為「讀時清晰、改時自信、擴展從容」。