萬盛學電腦網

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

CSS 可讀性工程:格式化規範、屬性排序、架構註解與自動化落地

可讀性 = 維護性 = 團隊效率

CSS 無邏輯分支,可讀性完全依賴結構組織。統一格式化規範是專業團隊基礎設施。

三層可讀性架構

層級 1:聲明格式化 (微觀)

/* ✅ 推薦:多行展開,屬性分組,尾隨分號 */
.component {
  /* Layout */
  display: flex;
  flex-direction: column;
  gap: 1rem;
  
  /* Box Model */
  padding: 1.5rem;
  border: 1px solid var(--color-border);
  border-radius: 8px;
  
  /* Visual */
  background: var(--color-surface);
  box-shadow: var(--shadow-md);
  
  /* Typography */
  font-size: 0.9375rem;
  line-height: 1.6;
  color: var(--color-text);
}

/* ❌ 避免:單行擠壓、無分組、無分號 */
.component{display:flex;flex-direction:column;padding:1.5rem;border:1px solid #eee;border-radius:8px;background:#fff}

層級 2:規則組織 (中觀)

/* 區塊分隔註解:等號錨點支援編輯器大綱跳轉 */
@import "reset.css";
@import "variables.css";

/* =RESET ============================ */
/* =VARIABLES ======================== */

/* =LAYOUT =========================== */
.layout { display: grid; }
.header { grid-area: header; }
.main { grid-area: main; }
.sidebar { grid-area: sidebar; }
.footer { grid-area: footer; }

/* =COMPONENTS ======================= */
/* == Button ========================= */
.btn { ... }
.btn--primary { ... }

/* == Card =========================== */
.card { ... }
.card__header { ... }
.card__body { ... }
.card__footer { ... }

/* =UTILITIES ======================== */
.visually-hidden { ... }

層級 3:架構文檔 (宏觀)

/*!
 * Design System v3.2.0
 * Architecture: ITCSS (Settings → Tools → Generic → Elements → Objects → Components → Utilities)
 * Tokens: colors, spacing, typography, breakpoints, shadows, z-index, transitions
 * Methodology: BEM + Utility-first hybrid
 * Browser Support: >0.5%, last 2 versions, not dead, not IE 11
 * Build: Vite + PostCSS (Autoprefixer, cssnano, PostCSS Nesting)
 * Lint: Stylelint (BEM pattern, logical properties, no ID selectors)
 * Docs: Storybook + Style Dictionary
 */

屬性排序:功能分組序 (業界主流)

  1. 佈局與定位:display, position, inset, z-index, float, clear, flex/grid 相關
  2. 盒模型:width, height, margin, padding, border, border-radius, box-sizing, overflow
  3. 視覺樣式:background, box-shadow, opacity, filter, clip-path, transform
  4. 字體文字:font, font-family, font-size, font-weight, line-height, color, text-*, letter-spacing, white-space
  5. 互動動畫:transition, animation, cursor, pointer-events, user-select, touch-action
  6. 偽類/偽元素/嵌套規則:放在規則末尾

自動化強制落地

/* package.json scripts */
"scripts": {
  "lint:css": "stylelint \"**/*.css\" --fix",
  "format:css": "prettier --write \"**/*.css\"",
  "check:css": "stylelint \"**/*.css\" && prettier --check \"**/*.css\""
}

/* .github/workflows/ci.yml */
- name: CSS Lint & Format
  run: npm run check:css

/* Husky pre-commit 鉤子 */
npx husky add .husky/pre-commit "npx lint-staged"

/* lint-staged.config.js */
module.exports = {
  '*.{css,scss,less}': ['stylelint --fix', 'prettier --write']
};

代碼審查清單

  • [ ] 格式化工具已跑通 (無 Prettier/Stylelint 錯誤)
  • [ ] 選擇器遵循 BEM/專案命名規範
  • [ ] 無 ID 選擇器用於樣式
  • [ ] 無 !important (或有明確註解理由)
  • [ ] 屬性按功能分組排序
  • [ ] 關鍵區塊有架構註解
  • [ ] 複雜邏輯有 @see 註解連結設計稿/文檔
copyright © 萬盛學電腦網 all rights reserved