#RRGGBB → #RGB (當 RR=R、GG=G、BB=B 時)
#000000 → #000
#336699 → #369
#FFA500 → #FA5 (FF→F, AA→A, 00→0)
/* 不可縮寫:#010203、#ABCDEF(非成對重複) */
現代建議:統一 6 位數(或 8 位 #RRGGBBAA),避免團隊混用認知負擔。
/* 1 值:四邊同值 */
margin: 1rem;
/* 2 值:上下 | 左右 */
padding: 1rem 2rem;
/* 3 值:上 | 左右 | 下 */
border-width: 1px 2px 3px;
/* 4 值:上 | 右 | 下 | 左 (順時針) */
border-radius: 4px 8px 12px 16px;
記憶:「上右下左,順時針」。
/* 語法:border: width style color; 順序固定不可亂 */
border: 1px solid #333;
/* 單邊單獨設定 */
border-top: 2px dashed red;
border-bottom: 1px solid #eee;
/* 進階技巧:先設全邊預設,再覆寫寬度 */
border: 1px solid #000;
border-width: 0 1px 1px 0; /* 只顯示右、下邊框 */
/* 標準語法:color image repeat attachment position */
background: #f5f5f5 url(bg.jpg) no-repeat fixed center top;
/* CSS3 Backgrounds 擴展:position/size、多背景 */
background:
linear-gradient(45deg, #fff, #f0f0f0) 0 0 / cover no-repeat,
url(pattern.png) 0 0 / 100px 100px repeat,
#fff;
/* 預設值補充:
color: transparent
image: none
repeat: repeat
attachment: scroll
position: 0% 0% */
/* 必須順序:style → variant → weight → size/line-height → family */
font: italic small-caps bold 1rem/1.6 "PingFang TC", "Microsoft JhengHei", sans-serif;
/* 最小必要:size + family */
font: 1rem/1.5 sans-serif;
/* ⚠️ 縮寫會重置所有未指定子屬性為預設值:
font-style: normal, font-variant: normal, font-weight: normal,
line-height: normal, font-family: inherit */
建議:元件內部文字樣式用長屬性(font-size、font-weight),避免繼承鏈被意外重置。
/* 語法:list-style: type position image; */
list-style: disc outside none; /* 預設值 */
list-style: square inside url(bullet.svg);
list-style: none; /* 移除標記 */
.card {
/* 盒模型 */
margin: 0 auto 1.5rem;
padding: 1.5rem;
border: 1px solid var(--border-color);
border-radius: var(--radius-lg);
background: var(--bg-color) url(card-pattern.svg) no-repeat right bottom / 120px auto;
box-shadow: var(--shadow-md);
/* 字體 */
font: 1rem/1.6 var(--font-sans);
color: var(--text-primary);
/* 列表重置 */
list-style: none;
}
margin-top),完整重設用縮寫。margin: var(--space-md) 0; 兼具可維護與簡潔。font 縮寫破壞繼承:全域排版用長屬性,元件根節點才用 font 重設基準。center / cover 需 CSS3 支援,IE9+ 可用。CSS 縮寫是「精簡」與「陷阱」並存。掌握六大類固定順序規則、理解縮寫會重置未指定子屬性的副作用、配合CSS 變數與建構工具自動優化,才能在團隊協作中真正受益。原則:**源碼保持可讀、長屬性表達單一意圖、縮寫表達完整重設、交給建構工具做最終壓縮**。