萬盛學電腦網

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

CSS 命名最佳實踐:語意化 vs 表現型、BEM 規範、工具類別與遷移策略

命名的兩大流派對比

維度表現型命名語意化命名
範例.red-text, .left-sidebar, .big-title, .clearfix.text-danger, .sidebar, .heading-hero, .flow-root
修改顏色時.red-text { color: blue; } ❌ 語意破壞.text-danger { color: blue; } ✅ 語意保持
佈局調整時.left-sidebar 移到右側 ❌ 名稱失真.sidebar 配合 CSS 佈局切換 ✅
團隊溝通需查看樣式才知用途名稱即文檔,降低認知負荷
重構風險高:HTML+CSS 雙重修改低:僅修改 CSS

語意化命名核心原則

  1. 描述「是什麼」而非「長什麼樣」:`.btn-primary` > `.blue-btn`
  2. 描述「功能角色」而非「視覺狀態」:`.alert-error` > `.red-box`
  3. 使用領域語言:電商用 `.product-card`、`.price`、`.add-to-cart`
  4. 扁平化、低耦合:避免 `.header .nav .list .item .link` 深層選擇器

BEM 命名規範詳細規則

/* Block: 獨立功能塊 */
.card { }

/* Element: Block 內部組成部分,雙底線 */
.card__title { }
.card__image { }
.card__price { }
.card__action { }

/* Modifier: 狀態或變體,雙連字符 */
.card--featured { }
.card--compact { }
.card__title--large { }
.card__price--sale { }
.card__action--disabled { }

/* 組合使用 */
<article class="card card--featured">
  <h2 class="card__title card__title--large">...</h2>
  <div class="card__price card__price--sale">...</div>
  <button class="card__action card__action--disabled">...</button>
</article>

BEM 變體:兩種常見風格

風格Element 分隔Modifier 分隔範例
標準 BEM`__``--`.block__element--modifier
Harry Roberts 改良版`-``--`.block-element--modifier
Bootstrap 5`-``-` (前綴).btn-primary, .btn-lg

團隊統一選擇一種並配置 Lint 規則即可。

固定不變的工具類別 (Utility Classes)

針對「永不變更語意」的單一功能類別,可用表現型命名:

/* 佈局工具 */
.d-flex { display: flex; }
.d-grid { display: grid; }
.d-none { display: none; }
.flex-1 { flex: 1; }
.justify-center { justify-content: center; }
.align-center { align-items: center; }
.gap-4 { gap: 1rem; }

/* 間距系統 (4px 基準) */
.m-0 { margin: 0; } .mt-2 { margin-top: 0.5rem; } .mb-4 { margin-bottom: 1rem; }
.p-0 { padding: 0; } .px-3 { padding-inline: 0.75rem; } .py-2 { padding-block: 0.5rem; }

/* 文字工具 */
.text-center { text-align: center; }
.text-left { text-align: left; }
.text-right { text-align: right; }
.fw-bold { font-weight: 700; }
.fs-sm { font-size: 0.875rem; }
.text-muted { color: #6b7280; }

/* 顯示/隱藏 */
.sr-only { 
  position: absolute; width: 1px; height: 1px; 
  padding: 0; margin: -1px; overflow: hidden; 
  clip: rect(0,0,0,0); white-space: nowrap; border: 0; 
}

/* 定位 */
.relative { position: relative; }
.absolute { position: absolute; }
.fixed { position: fixed; }
.sticky { position: sticky; }

遷移策略:從混亂命名到規範化

/* 階段 1:建立新規範文檔 + Stylelint 配置 */
/* 階段 2:新代碼強制新規範 */
/* 階段 3:高頻修改模組重構 (Boy Scout Rule) */
/* 階段 4:全專案批次重命名 (配合 IDE 重構工具) */
/* 階段 5:CI 攔截不合規,文檔化命名字典 */

命名字典範例 (團隊共享文檔)

功能區域Block 名稱常用 Element常用 Modifier
頁面殼層layout, header, footer, main, aside__container, __inner, __wrapper--fluid, --narrow, --sticky
導航nav, menu, breadcrumb, pagination, tabs__list, __item, __link, __indicator--vertical, --horizontal, --active, --disabled
表單form, field, input, select, textarea, checkbox, radio, button__label, __control, __help, __error, __group--required, --invalid, --disabled, --readonly, --inline
資料展示table, list, card, badge, avatar, tag, chip, tooltip, popover__header, __body, __footer, __row, __cell, __action--striped, --bordered, --hoverable, --compact, --dense
回饋alert, toast, modal, drawer, progress, spinner, skeleton__title, __message, __action, __icon, __close--success, --warning, --error, --info, --loading, --open
copyright © 萬盛學電腦網 all rights reserved