萬盛學電腦網

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

CSS 縮寫屬性完整手冊 (進階版):多重背景、系統字體、邏輯屬性與避坑指南

進階縮寫:超越基礎六大類

多重背景縮寫 (層疊順序:第一層最上)

.hero {
  background: 
    /* 第 1 層:裝飾圖案 */
    url('pattern.svg') repeat 0 0 / 200px,
    /* 第 2 層:主視覺 */
    url('hero.jpg') no-repeat center / cover,
    /* 第 3 層:漸層疊加 */
    linear-gradient(135deg, rgba(0,0,0,0.6), transparent),
    /* 最後:背景色 (必須在最後) */
    #1a1a1a;
  
  /* 等同展開寫法 */
  background-image: url('pattern.svg'), url('hero.jpg'), linear-gradient(...);
  background-repeat: repeat, no-repeat, no-repeat;
  background-position: 0 0, center, center;
  background-size: 200px, cover, auto;
  background-color: #1a1a1a;
}

系統字體關鍵字 (font 縮寫特殊值)

/* 直接使用系統 UI 字體組合,無需下載 */
font: system-ui;           /* 當前系統預設 UI 字體 */
font: -apple-system;       /* 蘋果系統字體 (San Francisco) */
font: 'Segoe UI';          /* Windows 系統字體 */
font: 'Roboto';            /* Android 系統字體 */

/* 完整系統字體堆疊 (推薦) */
font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';

邏輯屬性縮寫 (書寫模式感知)

/* 傳統物理屬性 → 邏輯屬性對應 */
margin: 1rem 2rem 3rem 4rem;          /* 上右下左 */
margin-block: 1rem 3rem;              /* 區塊方向:開始(上) 結束(下) */
margin-inline: 4rem 2rem;             /* 內聯方向:開始(左) 結束(右) */

padding-block-start: 1rem;            /* padding-top (ltr) */
padding-inline-end: 2rem;             /* padding-right (ltr) */
border-block: 1px solid #000;         /* border-top + border-bottom */
border-inline: 2px dashed red;        /* border-left + border-right */
border-start-start-radius: 8px;       /* border-top-left-radius (ltr) */
inset-block: 10px 20px;               /* top + bottom */
inset-inline: 15px 25px;              /* left + right */

/* 好處:RTL (阿拉伯文/希伯來文) 自動鏡像,無需重寫 CSS */

動畫/轉場縮寫

/* transition: property duration timing-function delay */
transition: all 0.3s ease 0.1s;
transition: opacity 0.2s, transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);

/* animation: name duration timing-function delay iteration-count direction fill-mode play-state */
animation: slide-in 0.5s ease-out 0s 1 normal forwards running;
animation: fade 0.3s ease, slide 0.5s ease-out;  /* 多動畫逗號分隔 */

Grid/Flex 縮寫

/* Grid 容器 */
grid: 
  "header header" auto
  "sidebar main" 1fr
  "footer footer" auto
  / 250px 1fr;  /* template-areas + rows + cols */

/* Flex 項目 */
flex: 1 1 300px;  /* grow shrink basis */
flex: auto;       /* 1 1 auto */
flex: none;       /* 0 0 auto */
flex: 2;          /* 2 1 0% */

避坑指南:縮寫重置陷阱

/* ❌ 危險:background 縮寫會重置所有背景屬性為預設值 */
.element {
  background-color: #f00;
  background: url(bg.jpg);  /* 背景色被重置為 transparent! */
}

/* ✅ 安全:使用單一屬性或完整聲明 */
.element {
  background-color: #f00;
  background-image: url(bg.jpg);
}

/* 或完整縮寫一次設定 */
.element {
  background: #f00 url(bg.jpg) no-repeat center / cover;
}

其他常見重置陷阱

  • border: 1px solid red → 重置 border-image, border-radius
  • font: bold 1rem sans-serif → 重置 font-variant, font-stretch, line-height (若未指定)
  • list-style: square → 重置 position, image
copyright © 萬盛學電腦網 all rights reserved