萬盛學電腦網

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

CSS 代碼高效整潔之道:Reset、命名、簡寫、繼承、合併、註釋、排序

七大原則打造可維護 CSS

1. 精準 Reset:拒絕全域重置

/* 差:* { 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; }

2. 語意化命名:拒絕外觀描述

反模式推薦理由
.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-。

3. 極致簡寫:減少代碼、提升可讀

/* 之前 */
.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;
}

4. 善用繼承:在祖先統一設定

/* 之前 */
.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 等。

5. 合併選擇器:減少重複規則集

/* 之前 */
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; }

6. 結構化註釋:建立可導航的代碼地圖

/* ==========================================================================
   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 { ... }

規則:

  • 頂層目錄註釋用 === 分隔大區塊。
  • 元件內部用 ---- 分隔子區塊。
  • 關鍵邏輯、Hack、Browser Fix 必須加單行註釋說明原因。

7. 屬性字母序排序:查找修改 O(1)

.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)。
  • 分組排序(推薦):佈局 → 盒模型 → 視覺 → 字體 → 其他,組內字母序。

工具鏈落地

  1. Stylelint:強制命名、屬性順序、禁止重複、單位規範。
  2. Prettier:統一格式化(縮進、引號、分號、尾隨逗號)。
  3. CSS Modules / CSS-in-JS:局部作用域,避免全域衝突。
  4. 設計系統 Tokens:色彩、間距、字體、陰影抽象為變數,杜絕魔術數字。
  5. CI 檢查:lint-staged + husky 提交前自動檢查修復。

總結

高效整潔的 CSS 不是天生,而是規約 + 工具 + 習慣的產物。從 Reset 開始精準、命名語意化、簡寫極致化、繼承最大化、選擇器合併化、註釋結構化、屬性排序化,七步走讓樣式表從「寫時只讀、改時崩潰」進化為「讀時清晰、改時自信、擴展從容」。

copyright © 萬盛學電腦網 all rights reserved