縮寫核心原則
- 減少代碼量 30–50%
- 提升可讀性與維護性
- 強制理解屬性關係與順序
- 注意:縮寫會重置未指定的子屬性為預設值
1. 色彩縮寫
/* 6 位 → 3 位:每兩位相同才可縮寫 */
#000000 → #000
#336699 → #369
#ff0000 → #f00
#cc9966 → #c96
/* 不可縮寫範例 */
#123456 → 無法縮寫 (每兩位不同)
#abcdef → 無法縮寫
適用所有 `` 型別屬性:color、background-color、border-color、outline-color、text-shadow、box-shadow、caret-color、accent-color 等。
2. 盒模型縮寫
適用:margin、padding、border-width、border-radius、outline-width、gap (Grid/Flex)。
順時針順序:上 → 右 → 下 → 左
/* 1 值:四邊相同 */
margin: 1rem; /* = 1rem 1rem 1rem 1rem */
/* 2 值:上下 | 左右 */
margin: 1rem 2rem; /* = 1rem 2rem 1rem 2rem */
/* 3 值:上 | 左右 | 下 */
margin: 1rem 2rem 3rem; /* = 1rem 2rem 3rem 2rem */
/* 4 值:上 | 右 | 下 | 左 */
margin: 1rem 2rem 3rem 4rem; /* 完整指定 */
邏輯屬性現代寫法 (支援 RTL)
margin-block: 1rem 2rem; /* margin-top | margin-bottom */
margin-inline: 1rem 2rem; /* margin-left | margin-right (LTR) */
padding-block-start: 1rem; /* 邏輯屬性:塊級起始邊 */
padding-inline-end: 2rem; /* 邏輯屬性:內聯結束邊 */
3. 邊框縮寫
/* 完整順序:width | style | color */
border: 1px solid #000;
/* 單邊縮寫 */
border-top: 2px dashed red;
border-right: 1px solid #ccc;
border-bottom: 3px double #333;
border-left: 1px dotted transparent;
/* 單屬性多值 (順時針) */
border-width: 1px 2px 3px 4px;
border-style: solid dashed dotted double;
border-color: red green blue yellow;
/* 邊框半徑 */
border-radius: 8px; /* 四角同半徑 */
border-radius: 8px 16px; /* 左上/右下 | 右上/左下 */
border-radius: 8px 16px 24px; /* 左上 | 右上/左下 | 右下 */
border-radius: 8px 16px 24px 32px; /* 左上 | 右上 | 右下 | 左下 */
border-radius: 50%; /* 圓形/橢圓 */
border-radius: 20px / 10px; /* 水平半徑 / 垂直半徑 (橢圓角) */
⚠️ 必須指定 border-style,否則無邊框
border: 5px red; ❌ 無效,缺少 style
border: solid; ✅ 3px solid currentColor
border: 1px solid; ✅ 1px solid currentColor
4. 背景縮寫
/* 順序:color | image | repeat | attachment | position | / size | origin | clip */
background: #f00 url('bg.png') no-repeat fixed center center / cover content-box padding-box;
/* 常用簡化 */
background: url('hero.jpg') no-repeat center / cover; /* 無色彩、預設 repeat/scroll/position */
background: #fff; /* 僅色彩,其餘預設 */
/* 多重背景 (用逗號分隔,第一層在最上) */
background:
url('overlay.png') no-repeat center,
linear-gradient(135deg, #667eea, #764ba2);
背景預設值
| 子屬性 | 預設值 |
background-color | transparent |
background-image | none |
background-repeat | repeat |
background-attachment | scroll |
background-position | 0% 0% (left top) |
background-size | auto auto |
background-origin | padding-box |
background-clip | border-box |
5. 字體縮寫
/* 必須順序:style | variant | weight | size/line-height | family */
/* ⚠️ size 與 family 為必填,其餘可省略 (套用預設) */
font: italic small-caps bold 1rem/1.5 "Noto Sans TC", sans-serif;
/* 斜體 小型大寫字 粗體 1rem/1.5行高 字體族群 */
/* 最小寫法 */
font: 1rem "Noto Sans TC", sans-serif;
/* 系統字體關鍵字 (無法拆分) */
font: caption; /* 系統標題字體 */
font: icon; /* 系統圖示字體 */
font: menu; /* 系統選單字體 */
font: message-box; /* 系統對話框字體 */
font: small-caption;/* 系統小標題字體 */
font: status-bar; /* 系統狀態欄字體 */
字體預設值
| 子屬性 | 預設值 |
font-style | normal |
font-variant | normal |
font-weight | normal (400) |
font-size | medium (~16px) |
line-height | normal (~1.2) |
font-family | 瀏覽器/系統預設 |
6. 列表樣式縮寫
/* 順序:type | position | image */
list-style: square inside url('bullet.svg');
/* 常用:移除預設符號 */
list-style: none;
/* 子屬性預設 */
list-style-type: disc; /* 實心圓 */
list-style-position: outside; /* 符號在內容外側 */
list-style-image: none; /* 無圖片 */
/* ⚠️ 圖片優先級高於 type */
list-style: disc inside url('custom.png'); /* 圖片存在時顯示圖片,否則顯示 disc */
Outline 縮寫 (補充)
outline: 2px solid #007bff; /* width | style | color */
/* 不佔空間、不影響佈局、不可單邊設定、通常為矩形 */
outline-offset: 4px; /* 與邊框間距 */
縮寫陷阱與避坑指南
| 陷阱 | 現象 | 解決 |
| font 縮寫重置 line-height | font: bold 1rem/1.5 sans-serif 會重置 line-height,若原本有繼承值會丟失 | 明確寫出 line-height 或分開聲明 |
| background 縮寫重置 size/origin/clip | background: url() no-repeat center 會重置 size 為 auto | 使用長屬性或完整縮寫含 size |
| border 縮寫重置顏色 | border: 1px solid 色彩變為 currentColor | 明確指定色彩或確認 currentColor 符合預期 |
| margin/padding 縮寫覆蓋單邊 | 後寫的 margin: 1rem 會覆蓋前面的 margin-top: 2rem | 避免混用、用邏輯屬性、統一用縮寫 |