/* 語法 */
.block { }
.block__element { }
.block--modifier { }
.product-card { } /* 區塊 */
.product-card__image { } /* 元素 */
.product-card__title { } /* 元素 */
.product-card__price { } /* 元素 */
.product-card__price--sale { } /* 修飾詞:特價 */
.product-card__price--original { } /* 修飾詞:原價 */
.product-card--featured { } /* 區塊修飾詞:精選 */
.product-card--compact { } /* 區塊修飾詞:緊湊版 */
| 優點 | 缺點 |
|---|---|
| 極低耦合,類名獨立 | 類名較長 |
| 語意清晰,關係一目了然 | 修飾詞易濫用(如 --large, --red) |
| 無需巢狀,優先級平坦 | 需團隊共識嚴格執行 |
| 利於 JS 狀態切換 |
/* 語法 */
.u-utility { } /* 工具類 */
.ComponentName { } /* 元件 */
.ComponentName--modifierName { } /* 修飾詞 */
.ComponentName-descendantName { } /* 後代元素 */
.ComponentName.is-stateName { } /* 狀態類 */
.ProductDetails { }
.ProductDetails-price { }
.ProductDetails-title--sale { }
.ProductDetails.is-loading { }
ProductDetails(而非 product-details)-descendant(而非 __element)is- 前綴u- 前綴.s- /* 結構/佈局:structure */
.t- /* 主題/外觀:theme */
.js- /* JavaScript 掛載:行為鉤子,不定義樣式 */
.u- /* 工具類:utility */
.is- /* 狀態類:state */
.has- /* 狀態類:擁有某狀態 */
.s-container { }
.s-grid { }
.t-primary { }
.t-dark-mode { }
.js-modal-trigger { }
.u-text-center { }
.is-active { }
.has-error { }
按特異性與影響範圍分層,由低到高:
1. Settings - 變數、配置(色彩、間距、斷點)
2. Tools - 函式、Mixin
3. Generic - 重置、Normalize、box-sizing
4. Elements - 裸標籤樣式(h1, a, table)
5. Objects - 設計模式/佈局骨架(.o-container, .o-grid)
6. Components - 具體 UI 元件(.c-button, .c-card)
7. Utilities - 高優先級工具類(.u-hidden, .u-mt-4)
.stylelintrc 強制命名格式--color-primary-500).btn--large-red-rounded-shadow → ✅ 拆解為組合類