萬盛學電腦網

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

CSS 常用縮寫屬性速查手冊:色碼、盒模型、背景、字體、列表一表掌握

五大類縮寫總覽

1. 色碼縮寫(十六進位)

完整縮寫條件
#000000#000每兩碼相同
#336699#369每兩碼相同
#ff0000#f00每兩碼相同
#f2f2f2不可縮寫每兩碼不同
rgb(255,255,255)#fff / white關鍵字/縮寫

現代補充:#rgba(4碼)、#rrggbbaa(8碼)支援透明度,如 #0000ff80 = 50% 透明藍。

2. 盒模型:Border(邊框)

/* 縮寫 */
border: 2px solid #333;
/* 等效 */
border-width: 2px; border-style: solid; border-color: #333;

/* 單邊 */
border-bottom: 1px dashed #ccc;
border-left: 3px solid #0066cc; /* 常用於側邊標籤 */

3. 盒模型:Margin / Padding(外距/內距)

/* 四值:上 右 下 左(順時針) */
margin: 10px 20px 30px 40px;

/* 三值:上 右左 下 */
padding: 1rem 2rem 1.5rem;

/* 兩值:上下 左右 */
margin: 0 auto;          /* 區塊水平置中經典寫法 */
padding: 1rem 2rem;

/* 單值:四邊同值 */
padding: 16px;

/* ⚠️ 值為 0 可省略單位:margin: 0; */
/* ⚠️ 非 0 值必須帶單位:margin: 10px; */

4. 背景 Background

/* 縮寫順序:color image repeat attachment position/size */
background: #fff url("bg.png") no-repeat fixed center top / cover;

/* 等效完整寫法 */
background-color: #fff;
background-image: url("bg.png");
background-repeat: no-repeat;
background-attachment: fixed;
background-position: center top;
background-size: cover;

/* 多重背景(現代) */
background: 
  url("pattern.png") repeat,
  linear-gradient(135deg, #667eea, #764ba2) fixed center / cover;

5. 字體 Font

/* 縮寫順序:style variant weight size/line-height family */
font: italic small-caps bold 1rem/1.5 "Noto Sans TC", sans-serif;

/* 必備最小值:size + family */
font: 1rem/1.5 sans-serif;

/* 等效完整寫法 */
font-style: italic;
font-variant: small-caps;
font-weight: bold;
font-size: 1rem;
line-height: 1.5;
font-family: "Noto Sans TC", sans-serif; 

6. 列表 List-style

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

/* 等效 */
list-style-type: square;
list-style-position: inside;
list-style-image: url("bullet.svg");

四邊屬性值個數對應規律(通用)

/* 1 值 */  margin: 10px;        → 上=右=下=左=10px
/* 2 值 */  margin: 10px 20px;   → 上下=10px、左右=20px
/* 3 值 */  margin: 10px 20px 30px; → 上=10、右左=20、下=30
/* 4 值 */  margin: 10px 20px 30px 40px; → 上=10、右=20、下=30、左=40

避坑提醒

  • 縮寫會重置未指定子屬性為初始值(如 background: url() 清除 color)
  • font 縮寫缺 font-family 會回退瀏覽器預設字體
  • border 必須包含 style(solid/dashed/dotted 等),否則不顯示
  • 邏輯屬性(margin-inline-start 等)無縮寫形式

延伸閱讀

掌握縮寫後,建議學習 CSS 控制滑鼠游標樣式完整指南,豐富互動回饋細節。

copyright © 萬盛學電腦網 all rights reserved