隨著專案增長,單一規則內的屬性可能多達 20+ 行。無序的屬性令查找、修改、除錯變成「大海撈針」。字母排序提供確定性、一致性、低認知負荷的組織方式,團隊協作尤其受益。
/* 無序:難以快速定位 */
.disorder {
color: #666;
z-index: 10;
margin: 10px 5px;
font: 1.2em/1.4em Arial, sans-serif;
background-color: #3399cc;
height: 300px;
width: 30%;
padding: 5px 0 10px 5px;
}
/* 字母排序:一眼掃描 */
.order {
background-color: #3399cc;
color: #666;
font: 1.2em/1.4em Arial, sans-serif;
height: 300px;
margin: 10px 5px;
padding: 5px 0 10px 5px;
width: 30%;
z-index: 10;
}
CSScomb、Stylelint(order/properties-alphabetical 規則)並設定存檔自動排序。stylelint --fix,強制規範通過才能合併。.stylelintrc,新成員零學習成本。margin、padding、border、font、background)應優先使用,拆解為單一屬性反而增加行數。排序針對「最終展開後的屬性清單」。{
"plugins": ["stylelint-order"],
"rules": {
"order/properties-alphabetical": true,
"order/order": ["custom-properties", "dollar-variables", "declarations"]
}
}
養成字母排序習慣,讓 CSS 程式碼像字典一樣好查、好改、好審查。更多程式碼規範建議,請關注本站工程化系列。