萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網頁製作 >> Div CSS 教學

CSS 縮寫屬性完整手冊:減少 50% 代碼量的核心技巧

縮寫核心價值

  • 文件體積減少 30-50%(Gzip 後仍有 15-25% 提升)
  • 可讀性提升:相關屬性聚合,邏輯分組清晰
  • 維護效率:修改一處同步生效,減少遺漏

六大核心縮寫屬性詳解

1. 顏色值縮寫

/* 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 */

2. 盒模型四邊值縮寫(margin/padding/border-width/outline-width)

/* 順時針順序:上 右 下左 (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;    /* 邏輯右 */

3. 邊框綜合縮寫

/* 順序: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;

4. 背景屬性綜合縮寫

/* 順序: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);

默認值速查:

屬性默認值
colortransparent
imagenone
repeatrepeat
attachmentscroll
position0% 0% (left top)
sizeauto auto

5. 字體綜合縮寫

/* 必須順序: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;   /* 系統狀態欄字體 */

6. 列表樣式縮寫

/* 順序: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; */

其他常用縮寫速查

屬性縮寫範例展開
outlineoutline: 2px solid #f00;width/style/color
transitiontransition: all 0.3s ease;property/duration/timing/delay
animationanimation: fadeIn 1s ease forwards;name/duration/timing/delay/iteration/direction/fill-mode/play-state
flexflex: 1 0 200px;grow/shrink/basis
gridgrid: 1fr 2fr / auto 1fr;template-rows / template-columns
place-itemsplace-items: center;align-items + justify-items
place-contentplace-content: space-between;align-content + justify-content
text-decorationtext-decoration: underline 2px red;line/style/color/thickness (CSS Text Decoration 4)
offsetoffset: path('M0,0 L100,0') 50%;path/distance/rotate/anchor (Motion Path)

避坑指南

  1. 順序錯誤導致無效:font: 1rem bold ❌ → font: bold 1rem ✅
  2. 遺漏必要值:font: italic serif ❌ (缺 size) → font: italic 1rem serif ✅
  3. background-size 位置:必須在 position 後用 / 分隔
  4. 邏輯屬性不支援縮寫:margin-block: 1rem 2rem 只能寫雙值,無四值縮寫
  5. 遺產屬性陷阱:border: 0 會重置 style/color,用 border: none 更明確

工程化建議

  • 配置 Stylelint 規則 shorthand-property-no-redundant-values 檢測冗餘值
  • 使用 cssnano 生產構建自動合併縮寫
  • 團隊約定:優先使用縮寫,除非需要覆蓋單一子屬性
  • CSS-in-JS 場景:對象語法天然避免順序錯誤 { margin: { top: 1, right: 2 } }

熟練掌握縮寫語法,是 CSS 開發者從「寫樣式」進階到「寫高效樣式」的分水嶺。

copyright © 萬盛學電腦網 all rights reserved