| 維度 | 表現型命名 | 語意化命名 |
|---|---|---|
| 範例 | .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 |
/* 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>
| 風格 | Element 分隔 | Modifier 分隔 | 範例 |
|---|---|---|---|
| 標準 BEM | `__` | `--` | .block__element--modifier |
| Harry Roberts 改良版 | `-` | `--` | .block-element--modifier |
| Bootstrap 5 | `-` | `-` (前綴) | .btn-primary, .btn-lg |
團隊統一選擇一種並配置 Lint 規則即可。
針對「永不變更語意」的單一功能類別,可用表現型命名:
/* 佈局工具 */
.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 |