縮寫前後對比
/* 重構前:冗長、難維護 */
li {
font-family: Arial, Helvetica, sans-serif;
font-size: 1.2em;
line-height: 1.4em;
padding-top: 5px;
padding-bottom: 10px;
padding-left: 5px;
}
/* 重構後:精簡、語意清晰 */
li {
font: 1.2em/1.4em Arial, Helvetica, sans-serif;
padding: 5px 0 10px 5px;
}
六大核心縮寫屬性群組
1. Font 字體系統
font: [style] [variant] [weight] / ;
/* 實用組合 */
font: 1rem/1.6 system-ui, sans-serif; /* 基礎內文 */
font: 600 1.25rem/1.3 "Noto Sans", sans-serif; /* 標題 */
font: italic 0.875rem/1.5 monospace; /* 代碼、引用 */
2. Margin / Padding 間距系統
/* 順時針:上 右 下左 */
margin: 1rem; /* 四邊 */
margin: 1rem 2rem; /* 上下 | 左右 */
margin: 1rem 2rem 3rem; /* 上 | 左右 | 下 */
margin: 1rem 2rem 3rem 4rem; /* 上 | 右 | 下 | 左 */
/* 邏輯屬性 (現代、支援 RTL) */
margin-block: 1rem 2rem; /* 上 | 下 */
margin-inline: 1rem 2rem; /* 左 | 右 (LTR) */
3. Border 邊框系統
/* 完整:width style color */
border: 1px solid #e5e7eb;
/* 單邊 */
border-bottom: 2px solid #3b82f6;
/* 多值拆分 */
border-width: 1px 2px;
border-style: solid dashed;
border-color: red blue green yellow;
/* 圓角 */
border-radius: 8px; /* 全圓角 */
border-radius: 8px 0 0 8px; /* 左側圓角 */
border-radius: 50%; /* 圓形 */
4. Background 背景系統
/* 單層 */
background: #f9fafb url('pattern.svg') repeat-x center / 100px;
/* 多層 (第一層最上) */
background:
linear-gradient(135deg, #667eea, #764ba2),
url('texture.png') repeat,
#f3f4f6;
/* 關鍵:background-size 需在 position 後用斜線分隔 */
background: url('img.jpg') no-repeat center / cover;
5. List Style 列表系統
list-style: none; /* 移除符號,最常用 */
list-style: disc outside; /* 預設值 */
list-style: square inside url('bullet.svg'); /* 自訂圖片 + 位置 */
6. Outline 外框系統
outline: 2px solid #3b82f6;
outline-offset: 4px; /* 與邊框間距,不佔空間 */
/* 焦點樣式最佳實踐 */
:focus-visible {
outline: 2px solid #3b82f6;
outline-offset: 2px;
}
進階縮寫技巧
CSS 變數配合縮寫
:root {
--btn-padding: 0.5rem 1rem;
--btn-border: 1px solid transparent;
--btn-radius: 6px;
--btn-font: 600 0.875rem/1.5 var(--font-family);
}
.btn {
padding: var(--btn-padding);
border: var(--btn-border);
border-radius: var(--btn-radius);
font: var(--btn-font);
}
/* 變體只需覆蓋變數 */
.btn-primary { --btn-border-color: #3b82f6; --btn-bg: #3b82f6; --btn-color: white; }
.btn-secondary { --btn-border-color: #d1d5db; --btn-bg: white; --btn-color: #374151; }
動畫/過渡縮寫
/* transition: property duration timing-function delay */
transition: all 0.2s ease-out;
transition: opacity 0.2s, transform 0.15s ease-in, box-shadow 0.2s;
/* animation: name duration timing-function delay iteration-count direction fill-mode play-state */
animation: fadeIn 0.3s ease-out forwards;
animation: slideUp 0.4s cubic-bezier(0.16, 1, 0.3, 1) 0.1s 1 normal both running;
Grid/Flex 縮寫
/* Grid 區域命名 */
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
/* Grid 縮寫 */
grid: "header header" auto
"sidebar main" 1fr
"footer footer" auto
/ 250px 1fr;
/* Flex 縮寫 */
flex: 1 1 auto; /* grow shrink basis */
flex: 0 0 300px; /* 固定寬度 */
縮寫重置陷阱總結表
| 縮寫屬性 | 會重置的子屬性 | 注意事項 |
font | line-height → normal、font-style/variant/weight → normal | 必須寫 line-height 或分開聲明 |
background | size → auto、origin → padding-box、clip → border-box、attachment → scroll | 需 size 時用斜線語法 |
border | color → currentColor、style → none (若未寫) | style 必填 |
margin/padding | 覆蓋所有單邊屬性 | 避免混用單邊與縮寫 |
list-style | image → none、position → outside、type → disc | 圖片優先於 type |
flex | basis → 0 (若只寫一值)、shrink → 1 | 建議完整三值 |
grid | 隱式軌道、間距、流向等全重置 | 複雜佈局建議分屬性 |
團隊規範建議
- **基礎重置層** 用分開聲明,避免縮寫副作用
- **元件層** 使用縮寫 + CSS 變數,集中管理、易主題化
- **工具類層** 使用單一用途類 (`.mt-4`, `.p-2`),不使用縮寫
- 配置 Stylelint `shorthand-property-no-redundant` 檢查冗餘縮寫
- Code Review 關注 `font`、`background`、`border` 縮寫正確性