/* 不推薦 */ color: #f3a;
/* 推薦 */ color: #FF33AA;
雖無確切數據證明效能差異,但遵循規範可避免潛在解析差異。
display: none:元素不佔空間,瀏覽器跳過佈局與繪製,**效能最佳**visibility: hidden:元素仍佔空間,瀏覽器仍需計算佈局,**較耗資源**border: none:不保留邊框空間,瀏覽器無需處理 border-color/styleborder: 0:雖隱藏邊框,但保留 border-color/style 計算權利,**略耗資源**1px 圖片平鋪 500px 區域需重複 250,000 次。**建議**:背景圖尺寸適中(如 50x50px),檔案大小控制在 70KB 內。
IE 專屬 filter 極度消耗資源且有相容性問題。PNG 透明請改用:
/* 不推薦:* 強制初始化所有標籤 */
* { margin:0; padding:0; }
/* 推薦:針對性重置 */
body, h1, h2, h3, h4, h5, h6, p, ul, ol, li, table, th, td, form, input, textarea {
margin: 0; padding: 0;
}
避免對 div, span, button, b, table 等差異極小或已棄用標籤浪費渲染資源。
/* 不推薦 */
button#backButton {}
.menu-left #newMenuIcon {}
/* 推薦:ID 唯一,無需標籤限定 */
#backButton {}
#newMenuIcon {}
/* 不推薦:極深後代選擇器 */
treeitem[mailfolder="true"] > treerow > treecell {}
/* 推薦:單一 class */
.treecell-mailfolder {}
後代選擇器是 **CSS 中最耗效能** 的選擇器。除非明確需要,嚴禁在 UI CSS 中使用。
/* 不推薦 */
treehead > treerow > treecell {}
/* 推薦 */
.treecell-header {}
/* 不推薦 */
treeitem[IsImapServer="true"] > treerow > .tree-folderpane-icon {}
/* 推薦:利用模板複製 RDF 屬性到子元素 */
.tree-folderpane-icon[IsImapServer="true"] {}
CSS 效能優化核心在於減少選擇器匹配次數與降低渲染樹構建複雜度。遵循上述 11 條原則,配合現代佈局技術,能讓頁面渲染速度顯著提升。