萬盛學電腦網

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

CSS 語法縮寫規則完整速查:色彩、盒模型、邊框、背景、字體、列表六大核心

CSS 縮寫核心價值

  • 減少 30–50% 代碼體積
  • 強制理解屬性關聯與順序
  • 集中維護、降低不一致風險
  • 配合 CSS 變數實現設計系統

六大縮寫規則速查表

1. 色彩縮寫

#RRGGBB → #RGB  (當 R=G、G=G、B=B 時)
#000000 → #000
#336699 → #369
#ff0000 → #f00
#cc9966 → #c96

/* 適用所有  型別屬性 */
color, background-color, border-color, outline-color, 
text-shadow, box-shadow, caret-color, accent-color, 
column-rule-color, text-decoration-color, etc.

2. 盒模型縮寫

適用:`margin`、`padding`、`border-width`、`border-radius`、`outline-width`、`gap`、`grid-gap`、`scroll-padding`、`scroll-margin`

/* 順時針:上 → 右 → 下 → 左 (TRBL = Trouble) */
property: value1;                    /* 4 邊同值 */
property: value1 value2;             /* 上下 | 左右 */
property: value1 value2 value3;      /* 上 | 左右 | 下 */
property: value1 value2 value3 value4; /* 上 | 右 | 下 | 左 */

/* 邏輯屬性 (支援 RTL,現代首選) */
margin-block: 1rem 2rem;   /* block-start | block-end (上 | 下) */
margin-inline: 1rem 2rem;  /* inline-start | inline-end (左 | 右) */

3. 邊框縮寫

/* 單邊完整:width | style | color (style 必填) */
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;        /* 水平半徑 / 垂直半徑 */

4. 背景縮寫

/* 完整順序 (CSS3) */
background: 
  [color] 
  [image] 
  [repeat] 
  [attachment] 
  [position] 
  [/ size] 
  [origin] 
  [clip];

/* 現代常用 */
background: url('hero.jpg') no-repeat center / cover;
/* 等價:color=transparent, image=url, repeat=no-repeat, 
   attachment=scroll, position=center, size=cover, 
   origin=padding-box, clip=border-box */

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

5. 字體縮寫

/* 必填:font-size | font-family (順序固定,不可亂序) */
font: [style] [variant] [weight] / ;

/* 範例 */
font: italic small-caps bold 1rem/1.5 "Noto Sans", sans-serif;
font: 600 1.25rem/1.3 system-ui;
font: 1rem monospace;  /* 最簡 */

/* 系統字體關鍵字 (不可拆分混用) */
font: caption | icon | menu | message-box | small-caption | status-bar;

6. 列表縮寫

/* 順序:type | position | image */
list-style: none;                           /* 最常用:移除符號 */
list-style: disc outside none;              /* 預設值 */
list-style: square inside url('bullet.svg'); /* 自訂圖片 + 內側符號 */

/* 子屬性預設 */
list-style-type: disc;
list-style-position: outside;
list-style-image: none;

/* ⚠️ 圖片存在時優先於 type 顯示 */

補充:Outline、Transition、Animation、Flex、Grid 縮寫

Outline

outline: 2px solid #3b82f6;  /* width | style | color */
outline-offset: 4px;         /* 獨立屬性,不佔空間 */

Transition

transition: property duration timing-function delay;
/* 多屬性逗號分隔 */
transition: opacity 0.2s ease, transform 0.15s ease-out, box-shadow 0.2s;

Animation

animation: name duration timing-function delay iteration-count direction fill-mode play-state;
/* 實用簡化 */
animation: fadeIn 0.3s ease-out forwards;
animation: slideUp 0.4s cubic-bezier(0.16,1,0.3,1) 0.1s 1 normal both;

Flex

flex:   ;
flex: 1;           /* 1 1 0% - 平均分配剩餘空間 */
flex: auto;        /* 1 1 auto - 內容基準分配 */
flex: none;        /* 0 0 auto - 固定尺寸 */
flex: 0 0 300px;   /* 固定 300px,不伸縮 */

Grid

grid:  / ;
grid: "header header" auto
      "sidebar main" 1fr
      "footer footer" auto
      / 250px 1fr;  /* 包含 grid-template-areas + rows + columns */

避坑指南:縮寫會重置未指定的子屬性

縮寫隱式重置正確做法
fontline-height → normal明確寫 size/line-height 或分開聲明
backgroundsize → auto, origin → padding-box, clip → border-box用斜線語法指定 size
bordercolor → currentColor明確指定色彩
margin/padding覆蓋所有單邊屬性統一用縮寫或邏輯屬性
flexbasis → 0 (單值時)建議寫完整三值
copyright © 萬盛學電腦網 all rights reserved