CSS 縮寫屬性是減少代碼體積、提升編寫效率、增強可讀性的核心技能。掌握 margin、padding、border、font、background、顏色值等核心屬性的縮寫規則,並建立項目級縮寫規範,是前端工程師進階的必經之路。本文系統梳理所有縮寫語法,提供對照表、避坑指南與工具鏈接入方案。
| 屬性類別 | 縮寫屬性 | 展開屬性(順序) | 值數量變體 |
|---|---|---|---|
| 外邊距/內邊距 | margin / padding | top → right → bottom → left (順時針) | 1/2/3/4 值 |
| 邊框 | border | width → style → color | 固定 3 值(順序不強制) |
| 背景 | background | color → image → position/size → repeat → attachment → origin → clip | 任意子集,順序建議遵循規範 |
| 字體 | font | style → variant → weight → size/line-height → family | 必須包含 size 和 family |
| 列表 | list-style | type → position → image | 任意子集 |
| 輪廓 | outline | width → style → color | 同 border |
| 過渡/動畫 | transition / animation | 多屬性組合,順序固定 | 見 MDN 規範 |
/* 四值:上右下左 */
margin: 10px 20px 30px 40px;
/* 三值:上 → 左右 → 下 */
margin: 10px 20px 30px; /* 等同 10px 20px 30px 20px */
/* 兩值:上下 → 左右 */
margin: 10px 20px; /* 等同 10px 20px 10px 20px */
/* 一值:四方向相同 */
margin: 10px; /* 等同 10px 10px 10px 10px */
/* 邊框:寬度 樣式 顏色(順序可互換,建議固定順序) */
border: 1px solid #ddd;
border: solid 1px #ddd; /* 合法但不推薦,降低可讀性 */
💡 記憶口訣: 「順時針:上右下左」,缺值時「對邊補齊」。
/* 完整聲明 */
font: italic small-caps bold 16px/1.5 "Microsoft YaHei", sans-serif;
/* 依次:style → variant → weight → size/line-height → family */
/* 最小合法聲明(必須包含 size 和 family) */
font: 14px "PingFang SC", sans-serif;
font: 1rem/1.6 system-ui;
/* ❌ 非法:缺少 font-size 或 font-family */
font: bold italic; /* 缺 size/family */
font: 16px; /* 缺 family */
font: "Arial"; /* 缺 size */
⚠️ 陷阱提醒: line-height 必須緊跟 font-size 並用 / 分隔,無空格。font-family 含空格時必須加引號。
/* 現代語法(支持 background-size) */
background: #f5f5f5 url("bg.png") no-repeat center/cover fixed;
/* 等同展開 */
background-color: #f5f5f5;
background-image: url("bg.png");
background-repeat: no-repeat;
background-position: center;
background-size: cover;
background-attachment: fixed;
background-origin: padding-box; /* 默認 */
background-clip: border-box; /* 默認 */
💡 最佳實踐: 複雜背景建議展開寫入,利用 CSS 變量管理重複值;簡單場景(純色、單圖)使用縮寫。
/* 十六進制 3 位縮寫(每對字符相同時) */
#000000 → #000
#336699 → #369
#ff0000 → #f00
#aabbcc → #abc
/* 8 位十六進制(含透明度) */
#rrggbbaa → #rgba (如 #ff000088 → #f008)
/* 函數式語法(推薦現代項目) */
rgb(255, 0, 0) → rgb(255 0 0) /* 空格分隔,現代語法 */
rgba(255, 0, 0, 0.5) → rgb(255 0 0 / 0.5) /* 斜杠分隔透明度 */
hsl(0, 100%, 50%) → hsl(0 100% 50%)
| 錯誤代碼 | 問題 | 修正 |
|---|---|---|
margin: 10px 20px 30px; 用於左右不同場景 | 三值語法強制左右相同 | 改用四值 margin: 10px 20px 30px 40px; |
font: bold 14px "Arial"; | 缺少 line-height 導致無法解析 | font: bold 14px/1.4 "Arial"; |
background: url(img.jpg) no-repeat center fixed; | 缺少 background-size,fixed 被誤判為 size | 加上 /cover 或展開寫入 |
border: red 1px solid; | 順序混亂,團隊難維護 | 統一 width style color 順序 |
// .stylelintrc.json
{
"rules": {
"shorthand-property-no-redundant-values": true, // 禁止冗餘值
"declaration-block-no-shorthand-property-overrides": true, // 禁止縮寫後被展開覆蓋
"property-no-vendor-prefix": true, // 交給 Autoprefixer
"color-hex-length": "short" // 強制 3/4/8 位十六進制
}
}
Prettier 會自動將十六進制轉為短格式、統一縮寫屬性順序。配合 --write 參數接入 pre-commit hook。
// .csscomb.json 片段
{
"properties-order": [...],
"shorthand-values": {
"margin": "1 2 3 4",
"padding": "1 2 3 4",
"border": "width style color",
"font": "style variant weight size/line-height family"
}
}
CSS 縮寫不是「能縮則縮」,而是在保證語義明確、團隊共識、工具可執行的前提下追求極致精簡。建議:
精簡的代碼不僅傳輸更快,更是工程師專業素養的體現。