萬盛學電腦網

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

CSS 縮寫屬性完整速查手冊:色彩、盒模型、邊框、背景、字體、列表六大類

縮寫核心原則

  • 減少代碼量 30–50%
  • 提升可讀性與維護性
  • 強制理解屬性關係與順序
  • 注意:縮寫會重置未指定的子屬性為預設值

1. 色彩縮寫

/* 6 位 → 3 位:每兩位相同才可縮寫 */
#000000 → #000
#336699 → #369
#ff0000 → #f00
#cc9966 → #c96

/* 不可縮寫範例 */
#123456 → 無法縮寫 (每兩位不同)
#abcdef → 無法縮寫

適用所有 `` 型別屬性:color、background-color、border-color、outline-color、text-shadow、box-shadow、caret-color、accent-color 等。

2. 盒模型縮寫

適用:margin、padding、border-width、border-radius、outline-width、gap (Grid/Flex)。

順時針順序:上 → 右 → 下 → 左

/* 1 值:四邊相同 */
margin: 1rem;           /* = 1rem 1rem 1rem 1rem */

/* 2 值:上下 | 左右 */
margin: 1rem 2rem;      /* = 1rem 2rem 1rem 2rem */

/* 3 值:上 | 左右 | 下 */
margin: 1rem 2rem 3rem; /* = 1rem 2rem 3rem 2rem */

/* 4 值:上 | 右 | 下 | 左 */
margin: 1rem 2rem 3rem 4rem;  /* 完整指定 */

邏輯屬性現代寫法 (支援 RTL)

margin-block: 1rem 2rem;    /* margin-top | margin-bottom */
margin-inline: 1rem 2rem;   /* margin-left | margin-right (LTR) */

padding-block-start: 1rem;  /* 邏輯屬性:塊級起始邊 */
padding-inline-end: 2rem;   /* 邏輯屬性:內聯結束邊 */

3. 邊框縮寫

/* 完整順序:width | style | color */
border: 1px solid #000;

/* 單邊縮寫 */
border-top: 2px dashed red;
border-right: 1px solid #ccc;
border-bottom: 3px double #333;
border-left: 1px dotted transparent;

/* 單屬性多值 (順時針) */
border-width: 1px 2px 3px 4px;
border-style: solid dashed dotted double;
border-color: red green blue yellow;

/* 邊框半徑 */
border-radius: 8px;                    /* 四角同半徑 */
border-radius: 8px 16px;               /* 左上/右下 | 右上/左下 */
border-radius: 8px 16px 24px;          /* 左上 | 右上/左下 | 右下 */
border-radius: 8px 16px 24px 32px;     /* 左上 | 右上 | 右下 | 左下 */
border-radius: 50%;                    /* 圓形/橢圓 */
border-radius: 20px / 10px;            /* 水平半徑 / 垂直半徑 (橢圓角) */

⚠️ 必須指定 border-style,否則無邊框

border: 5px red;      ❌ 無效,缺少 style
border: solid;        ✅ 3px solid currentColor
border: 1px solid;    ✅ 1px solid currentColor

4. 背景縮寫

/* 順序:color | image | repeat | attachment | position | / size | origin | clip */
background: #f00 url('bg.png') no-repeat fixed center center / cover content-box padding-box;

/* 常用簡化 */
background: url('hero.jpg') no-repeat center / cover;  /* 無色彩、預設 repeat/scroll/position */
background: #fff;  /* 僅色彩,其餘預設 */

/* 多重背景 (用逗號分隔,第一層在最上) */
background: 
  url('overlay.png') no-repeat center,
  linear-gradient(135deg, #667eea, #764ba2);

背景預設值

子屬性預設值
background-colortransparent
background-imagenone
background-repeatrepeat
background-attachmentscroll
background-position0% 0% (left top)
background-sizeauto auto
background-originpadding-box
background-clipborder-box

5. 字體縮寫

/* 必須順序:style | variant | weight | size/line-height | family */
/* ⚠️ size 與 family 為必填,其餘可省略 (套用預設) */

font: italic small-caps bold 1rem/1.5 "Noto Sans TC", sans-serif;
/*  斜體   小型大寫字  粗體   1rem/1.5行高      字體族群   */

/* 最小寫法 */
font: 1rem "Noto Sans TC", sans-serif;

/* 系統字體關鍵字 (無法拆分) */
font: caption;      /* 系統標題字體 */
font: icon;         /* 系統圖示字體 */
font: menu;         /* 系統選單字體 */
font: message-box;  /* 系統對話框字體 */
font: small-caption;/* 系統小標題字體 */
font: status-bar;   /* 系統狀態欄字體 */

字體預設值

子屬性預設值
font-stylenormal
font-variantnormal
font-weightnormal (400)
font-sizemedium (~16px)
line-heightnormal (~1.2)
font-family瀏覽器/系統預設

6. 列表樣式縮寫

/* 順序:type | position | image */
list-style: square inside url('bullet.svg');

/* 常用:移除預設符號 */
list-style: none;

/* 子屬性預設 */
list-style-type: disc;      /* 實心圓 */
list-style-position: outside; /* 符號在內容外側 */
list-style-image: none;     /* 無圖片 */

/* ⚠️ 圖片優先級高於 type */
list-style: disc inside url('custom.png');  /* 圖片存在時顯示圖片,否則顯示 disc */

Outline 縮寫 (補充)

outline: 2px solid #007bff;  /* width | style | color */
/* 不佔空間、不影響佈局、不可單邊設定、通常為矩形 */
outline-offset: 4px;  /* 與邊框間距 */

縮寫陷阱與避坑指南

陷阱現象解決
font 縮寫重置 line-heightfont: bold 1rem/1.5 sans-serif 會重置 line-height,若原本有繼承值會丟失明確寫出 line-height 或分開聲明
background 縮寫重置 size/origin/clipbackground: url() no-repeat center 會重置 size 為 auto使用長屬性或完整縮寫含 size
border 縮寫重置顏色border: 1px solid 色彩變為 currentColor明確指定色彩或確認 currentColor 符合預期
margin/padding 縮寫覆蓋單邊後寫的 margin: 1rem 會覆蓋前面的 margin-top: 2rem避免混用、用邏輯屬性、統一用縮寫
copyright © 萬盛學電腦網 all rights reserved