.class-name { property: value; }
. 開頭,後接自訂名稱(字母開頭、可含數字、連字號、底線)<div class="card shadow primary">p.right { text-align: right; }
p.center { text-align: center; }
缺點:耦合標籤,改標籤需改 CSS,複用性低。
.text-right { text-align: right; }
.text-center { text-align: center; }
優點:任意元素可用,<h1 class="text-center">、<p class="text-center"> 同樣生效。
.btn { padding: .5em 1em; border-radius: 4px; font-size: 1rem; }
.btn-primary { background: #0066cc; color: #fff; }
.btn-secondary { background: #eee; color: #333; }
.btn-danger { background: #cc0000; color: #fff; }
.btn-lg { padding: .75em 1.5em; font-size: 1.125rem; }
.btn-sm { padding: .25em .75em; font-size: .875rem; }
使用:<button class="btn btn-primary btn-lg">提交</button>
.btn、.card、.modal.btn__icon、.card__title.btn--primary、.card--highlighted.is-active、.is-hidden、.is-loading.js-submit-btn(絕不在 CSS 中定義樣式)| 錯誤寫法 | 問題 | 修正 |
|---|---|---|
.red { color: red; } | 語意綁定視覺,換膚需改 HTML | .text-danger、.text-primary |
.fl { float: left; } | 縮寫不可讀,且 float 已過時 | 改用 Flex/Grid 佈局工具類 |
.m10 { margin: 10px; } | 魔術數字,維護困難 | .m-md 配合設計系統間距標準 |
Class 選擇器是 CSS 復用的核心。**語意化、扁平化、解耦標籤** 是三大鐵律。配合設計系統的 Design Tokens,讓樣式真正可維護、可擴展、可主題化。