萬盛學電腦網

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

CSS 代碼可讀性工程化標準:多行展開、屬性分組、巢狀映射與自動化執行

可讀性對比:展開式 vs 壓縮式

/* ✅ 推薦:多行展開,屬性分組,巢狀映射 DOM */
.card {
  /* Layout */
  display: flex;
  flex-direction: column;
  gap: 16px;

  /* Box Model */
  width: 100%;
  max-width: 400px;
  padding: 24px;
  margin: 0 auto;

  /* Visual */
  background-color: #fff;
  border: 1px solid var(--border-color);
  border-radius: 12px;
  box-shadow: var(--shadow-md);

  /* Typography */
  font-family: var(--font-family-base);
  color: var(--text-primary);
  line-height: 1.6;
}

/* 子元素巢狀縮進,映射 DOM 結構 */
.card__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--border-color);
  padding-bottom: 16px;
  margin-bottom: 8px;
}

.card__title {
  font-size: 1.25rem;
  font-weight: 600;
  color: var(--text-heading);
}

/* ❌ 不推薦:單行壓縮,難讀、難維護、Diff 雜訊大 */
.card{display:flex;flex-direction:column;gap:16px;width:100%;max-width:400px;padding:24px;margin:0 auto;background-color:#fff;border:1px solid var(--border-color);border-radius:12px;box-shadow:var(--shadow-md);font-family:var(--font-family-base);color:var(--text-primary);line-height:1.6;}

五大可讀性鐵律

  1. 聲明拆行:每行一聲明,Git Diff 精準到屬性級。
  2. 語義分組 + 空行分隔:佈局 → 盒模型 → 視覺 → 字體 → 動畫 → 其他。組內按邏輯序(如 margin 顺時針)。
  3. 巢狀縮進映射 DOM:子組件規則集相對父組件增加一級縮進(2 或 4 空格),直觀反映結構層級。
  4. 單行例外:單一聲明規則集、工具類、關鍵幀關鍵幀 可單行:
    .u-hidden { display: none !important; }
    @keyframes spin { to { transform: rotate(360deg); } }
  5. 團隊統一 > 個人偏好:配置 Prettier/Stylelint 強制執行,Code Review 不討論格式。

屬性排序推薦順序(Concentric CSS / Mozilla 順序)

1. Positioning      : position, top, right, bottom, left, z-index, display, float, clear
2. Box Model        : width, height, max/min-width/height, margin, padding, border, box-sizing
3. Flex/Grid        : flex/grid 相關屬性
4. Visual           : background, box-shadow, opacity, transform, filter, clip-path
5. Typography       : font, color, text-align, line-height, letter-spacing, white-space
6. Animation        : animation, transition
7. Misc             : cursor, pointer-events, user-select, will-change

自動化工具鏈配置

Prettier (.prettierrc)

{
  "singleQuote": true,
  "tabWidth": 2,
  "printWidth": 120,
  "cssSingleQuote": true,
  "endOfLine": "lf"
}

Stylelint (.stylelintrc.json)

{
  "extends": ["stylelint-config-standard", "stylelint-config-recess-order"],
  "rules": {
    "block-opening-brace-space-before": "always",
    "declaration-colon-space-after": "always",
    "declaration-block-trailing-semicolon": "always",
    "declaration-block-single-line-max-declarations": 1,
    "selector-class-pattern": "^[a-z][a-z0-9]*(-[a-z0-9]+)*(__[a-z0-9]+(-[a-z0-9]+)*)?(--[a-z0-9]+(-[a-z0-9]+)*)?$"
  }
}

VS Code settings.json

{
  "editor.formatOnSave": true,
  "editor.defaultFormatter": "esbenp.prettier-vscode",
  "css.validate": false,  /* 關閉內建驗證,交給 Stylelint */
  "stylelint.enable": true
}

總結

CSS 可讀性不是主觀審美,是**協作效率的量化指標**。多行展開、語義分組、巢狀映射、單行例外、自動化強制,五位一體構建「零格式爭議、零認知負擔、零合併衝突」的樣式代碼庫。投資配置工具鏈,收獲長期複利。

copyright © 萬盛學電腦網 all rights reserved