縮寫優勢總覽
- 減少代碼行數 40-60%
- 降低檔案體積、加快解析速度
- 集中管理相關屬性,維護更直觀
六大核心屬性完整對照表
1. Font(字體)
/* 縮寫 */
font: italic small-caps bold 12px/1.5em "Microsoft JhengHei", sans-serif;
/* 等效完整寫法 */
font-style: italic;
font-variant: small-caps;
font-weight: bold;
font-size: 12px;
line-height: 1.5em;
font-family: "Microsoft JhengHei", sans-serif;
/* 順序:style → variant → weight → size/line-height → family */
/* ⚠️ size 與 line-height 必須用 / 連接,不可分開 */
/* ⚠️ 至少需包含 font-size 與 font-family 兩個必要值 */
2. Background(背景)
/* 縮寫 */
background: #fff url("bg.png") no-repeat fixed left top;
/* 等效完整寫法 */
background-color: #fff;
background-image: url("bg.png");
background-repeat: no-repeat;
background-attachment: fixed;
background-position: left top;
/* 順序:color → image → repeat → attachment → position */
/* 現代擴展:background-size、background-origin、background-clip 需單獨寫或用 / 分隔 */
3. Margin / Padding(外距/內距)
/* 四值:上 右 下左 */
margin: 10px 20px 30px 40px;
/* 等效 */
margin-top: 10px; margin-right: 20px; margin-bottom: 30px; margin-left: 40px;
/* 三值:上 右左 下 */
padding: 1rem 2rem 1.5rem;
/* 兩值:上下 左右 */
margin: 0 auto;
/* 單值:四邊同值 */
padding: 16px;
/* 順序:順時針(上→右→下→左) */
/* padding 語法與 margin 完全相同 */
4. Border(邊框)
/* 統一四邊 */
border: 2px solid #333;
/* 等效 */
border-width: 2px; border-style: solid; border-color: #333;
/* 單邊 */
border-bottom: 1px dashed #ccc;
/* 等效 */
border-bottom-width: 1px; border-bottom-style: dashed; border-bottom-color: #ccc;
/* 順序:width → style → color */
5. List-style(列表樣式)
/* 縮寫 */
list-style: square outside url("bullet.svg");
/* 等效 */
list-style-type: square;
list-style-position: outside;
list-style-image: url("bullet.svg");
/* 順序:type → position → image */
6. Color(顏色)十六進位縮寫
| 完整 | 縮寫 | 條件 |
| #FFFFFF | #FFF | 每兩碼相同 |
| #336699 | #369 | 每兩碼相同 |
| #F2F2F2 | 不可縮寫 | 每兩碼不同 |
| rgb(255,0,0) | red / #f00 | 關鍵字/縮寫 |
四邊屬性縮寫規律總結(Margin/Padding/Border-width/Color)
| 值個數 | 對應關係 | 記憶口訣 |
| 1 | 上=右=下=左 | 一值全蓋 |
| 2 | 上下=第一值、左右=第二值 | 上下、左右 |
| 3 | 上=第一、右左=第二、下=第三 | 上、右左、下 |
| 4 | 上、右、下、左 | 順時針繞一圈 |
避坑提醒
- 縮寫會重置未寫入的子屬性為初始值(如
background: url() 會清除 color)
font 縮寫若缺 font-family 會導致字體回退
border 縮寫必須包含 style(solid/dashed 等),否則不顯示
- 邏輯屬性(
margin-inline-start 等)無縮寫,需單獨寫
延伸閱讀
掌握縮寫後,建議深入 CSS em 與 px 單位差異與換算策略,建立彈性字體系統。