高性能、可維護 CSS 的 12 條核心準則,源自 MDN、Google、Steve Souders 等權威指南實戰總結。
ul#nav {} → #nav {};.menu#id {} → #id {}。ID 已唯一,標籤/類名多餘。html div tr td {} → 扁平化類名 .cell {}。耦合 HTML 結構、效能差、難維護。.menu.left.icon {} → .menu-left-icon {}。單一類名語意更清、解析更快。#nav li a {} → #nav {} 並在 #nav 內定義通用樣式。利用繼承減少規則。padding: 20px 10px; background: #000 url(...) repeat-x bottom;。.wrap table tr.other td.rule {} → .wrap .other td.rule {}。移除標籤限定。.a, .b { color:red; background:blue; font-size:15px; }。.error-message)而非「長什麼樣」(.red-text),利於重構與主題切換。.btn,
.btn-primary,
.btn-secondary {
/* Positioning */
position: relative;
z-index: 1;
/* Display & Box Model */
display: inline-flex;
align-items: center;
padding: 0.5rem 1rem;
border-radius: 0.25rem;
/* Typography */
font: 500 0.875rem/1.25 system-ui, sans-serif;
color: #fff;
text-decoration: none;
/* Decoration */
background: linear-gradient(180deg, #2563eb, #1d4ed8);
box-shadow: 0 1px 2px rgba(0,0,0,.05);
/* Transitions */
transition: background-color .15s ease, box-shadow .15s ease;
}
/* State variants */
.btn:hover { background: linear-gradient(180deg, #1d4ed8, #1e40af); }
.btn:active { background: linear-gradient(180deg, #1e40af, #1e3a8a); }
遵循這 12 條,CSS 體積縮減 30%+,維護成本大幅下降。