/* ❌ */
padding: 10px 5px 0px 0px;
/* ✅ */
padding: 10px 5px 0 0;
0 不需單位,0px、0em、0% 皆為多餘。
/* ❌ */
div#logo-wrap { ... }
/* ✅ */
#logo-wrap { ... }
ID 選擇器專一性極高,標籤前綴純屬贅餘,增加專一性風險。
/* ❌ 污染全域 */
* { margin: 0; }
/* ✅ 限制範圍 */
#menu * { margin: 0; }
/* ✅ 現代重置用 :where() 降低專一性 */
:where(*) { margin: 0; }
/* ❌ */
background-image: url(logo.png);
background-position: top center;
background-repeat: no-repeat;
/* ✅ */
background: url(logo.png) no-repeat top center;
/* 現代擴充 */
background:
linear-gradient(45deg, #fff, #f0f0f0) padding-box,
url(pattern.png) repeat-x border-box,
#fafafa content-box;
#000000 → #000
#336699 → #369
#ff0000 → #f00
/* 非成對不可縮寫:#010203, #abcdef */
/* 基礎合併 */
margin: 0 10px 0 10px;
/* 進階:上下相同、左右相同 */
margin: 0 10px; /* 上下 0,左右 10px */
padding: 1rem 2rem; /* 上下 1rem,左右 2rem */
/* 統一四邊 */
border: 1px solid #000;
/* 四邊不同寬度 */
border-width: 1px 2px 3px 4px;
/* 僅顯示右、下邊框(技巧) */
border: 1px solid #000;
border-width: 0 1px 1px 0;
/* ❌ 分散宣告 */
font-style: italic;
font-variant: small-caps;
font-weight: bold;
font-size: 1em;
line-height: 150%;
font-family: "Microsoft JhengHei", sans-serif;
/* ✅ 一行搞定 */
font: italic small-caps bold 1em/150% "Microsoft JhengHei", sans-serif;
list-style: square inside url(bullet.png);
/* type position image */
/* ❌ */
h1 { color: #333; }
h2 { color: #333; }
h3 { color: #333; }
/* ✅ */
h1, h2, h3 { color: #333; }
declaration-block-no-duplicate-properties、shorthand-property-no-redundant-values、color-hex-length。/* 原始 1.2KB */
.card { ... 30 行分散屬性 ... }
/* 優化後 0.4KB */
.card{background:#fff;border:1px solid #ddd;border-radius:4px;box-shadow:0 2px 4px rgba(0,0,0,.1);color:#333;display:flex;flex-direction:column;font:1rem/1.5 system-ui,sans-serif;margin:1rem;padding:1.5rem;transition:box-shadow .2s}
體積減少 67%,Gzip 後更小。配合 HTTP/2 多路複用,效能提升顯著。
將這十大技巧內化為肌肉記憶,並納入團隊 Lint 規則,讓每一行 CSS 都精簡有力。更多 CSS 效能優化實戰,請關注本站工程化系列。