/* 16進制 3位縮寫:每對字符相同時 */
#000000 → #000
#336699 → #369
#ffcc00 → #fc0
/* 現代 8 位十六進制(含透明度) */
#ff0000ff → #f00f /* 紅色 + 100% 不透明 */
#00ff0080 → #0f08 /* 綠色 + 50% 透明 */
/* 函數式語法(推薦可讀性) */
rgb(255, 0, 0) /* 不透明 */
rgba(255, 0, 0, 0.5) /* 半透明 */
hsl(0, 100%, 50%) /* 色相飽和度亮度 */
hwb(0 0% 0%) /* 色相白度黑度 - CSS Color 4 */
/* 順時針順序:上 右 下左 (TRBL = Trouble 記憶法) */
margin: 1rem; /* 4 邊同值 */
margin: 1rem 2rem; /* 上下 1rem | 左右 2rem */
margin: 1rem 2rem 3rem; /* 上 1rem | 左右 2rem | 下 3rem */
margin: 1rem 2rem 3rem 4rem; /* 上 1 | 右 2 | 下 3 | 左 4 */
/* 邏輯屬性(書寫模式感知) */
margin-block: 1rem 2rem; /* 上/下 (垂直) */
margin-inline: 2rem 3rem; /* 左/右 (水平) */
margin-block-start: 1rem; /* 邏輯上 */
margin-inline-end: 2rem; /* 邏輯右 */
/* 順序:width style color (任意順序均可,推薦標準順序) */
border: 1px solid #000;
border: solid 1px #000; /* 等效 */
/* 單邊縮寫 */
border-top: 2px dashed red;
border-right: 1px solid;
border-bottom: 3px double #333;
border-left: 1px dotted currentColor; /* 繼承文字顏色 */
/* 邏輯邊框 */
border-block: 1px solid;
border-inline: 2px dashed #000;
/* 順序:color image repeat attachment position / size */
/* 注意:size 必須在 position 後用 / 分隔 */
background: #f00 url(bg.jpg) no-repeat fixed center / cover;
/* 等效展開 */
background-color: #f00;
background-image: url(bg.jpg);
background-repeat: no-repeat;
background-attachment: fixed;
background-position: center;
background-size: cover;
/* 多背景層(逗號分隔,先寫的在上層) */
background:
url(icon.svg) no-repeat left top,
linear-gradient(45deg, #fff, #f0f0f0);
默認值速查:
| 屬性 | 默認值 |
|---|---|
| color | transparent |
| image | none |
| repeat | repeat |
| attachment | scroll |
| position | 0% 0% (left top) |
| size | auto auto |
/* 必須順序:style variant weight size/line-height family */
/* 至少包含 size + family,否則無效 */
font: italic small-caps bold 1rem/1.5 "Microsoft YaHei", sans-serif;
/* 常用組合 */
font: 1rem/1.5 sans-serif; /* 最小有效 */
font: bold 1.25rem/1.3 "Georgia", serif; /* 粗體標題 */
font: 0.875rem/1.6 system-ui; /* 正文優化 */
/* 關鍵字值 */
font: caption; /* 系統標題字體 */
font: icon; /* 系統圖標字體 */
font: menu; /* 系統選單字體 */
font: message-box; /* 系統對話框字體 */
font: small-caption;/* 系統小標題字體 */
font: status-bar; /* 系統狀態欄字體 */
/* 順序:type position image */
list-style: square inside url(bullet.svg);
/* 等效展開 */
list-style-type: square; /* disc/circle/square/decimal/... */
list-style-position: inside; /* outside(預設)/inside */
list-style-image: url(bullet.svg); /* 圖片標記 */
/* 重置列表 */
list-style: none; /* 等同於 type: none; position: outside; image: none; */
| 屬性 | 縮寫範例 | 展開 |
|---|---|---|
| outline | outline: 2px solid #f00; | width/style/color |
| transition | transition: all 0.3s ease; | property/duration/timing/delay |
| animation | animation: fadeIn 1s ease forwards; | name/duration/timing/delay/iteration/direction/fill-mode/play-state |
| flex | flex: 1 0 200px; | grow/shrink/basis |
| grid | grid: 1fr 2fr / auto 1fr; | template-rows / template-columns |
| place-items | place-items: center; | align-items + justify-items |
| place-content | place-content: space-between; | align-content + justify-content |
| text-decoration | text-decoration: underline 2px red; | line/style/color/thickness (CSS Text Decoration 4) |
| offset | offset: path('M0,0 L100,0') 50%; | path/distance/rotate/anchor (Motion Path) |
font: 1rem bold ❌ → font: bold 1rem ✅font: italic serif ❌ (缺 size) → font: italic 1rem serif ✅/ 分隔margin-block: 1rem 2rem 只能寫雙值,無四值縮寫border: 0 會重置 style/color,用 border: none 更明確shorthand-property-no-redundant-values 檢測冗餘值cssnano 生產構建自動合併縮寫{ margin: { top: 1, right: 2 } }熟練掌握縮寫語法,是 CSS 開發者從「寫樣式」進階到「寫高效樣式」的分水嶺。