萬盛學電腦網

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

CSS Background 背景屬性完整指南:單屬性、縮寫、多重背景與漸層應用

背景八大子屬性

屬性說明預設值
background-color背景顏色transparent
background-image背景圖片 (可多張,逗號分隔)none
background-repeat平鋪方式repeat
background-attachment固定/捲動scroll
background-position定位位置0% 0% (left top)
background-size圖片尺寸auto auto
background-origin定位基準區域padding-box
background-clip繪製裁切區域border-box

background 縮寫語法

background: [color] [image] [repeat] [attachment] [position] [/ size] [origin] [clip];

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

關鍵屬性深度解析

background-size:響應式關鍵

值行為適用場景
auto原始尺寸 (預設)圖示、固定尺寸裝飾
cover縮放填滿容器,保持比例,可能裁切Hero Banner、全螢幕背景、卡片縮圖
contain縮放完整顯示,保持比例,可能留白Logo、產品圖、完整呈現優先
100% 100%拉伸填滿,不保持比例紋理、幾何圖案
50% auto寬度 50%,高度等比側邊裝飾圖

background-origin vs background-clip

.demo {
  border: 10px dashed red;
  padding: 20px;
  background: url('pattern.png') repeat;
  background-origin: padding-box;  /* 從 padding 邊緣開始鋪貼 */
  background-clip: content-box;    /* 僅在 content 區域顯示背景 */
}

/* origin 決定 background-position: 0 0 的參考點
   clip 決定背景實際繪製到哪裡為止 */

background-attachment:視差與固定

值行為效能注意
scroll隨元素捲動 (預設)標準,效能佳
fixed相對視口固定,視差效果⚠️ 移動端可能卡頓、iOS 需 -webkit-
local隨元素內容捲動 (如 textarea)特定場景

多重背景 (CSS3)

.multi-bg {
  background: 
    /* 第 1 層 (最上層) */
    url('icon.png') no-repeat 20px 20px,
    /* 第 2 層 */
    linear-gradient(135deg, rgba(255,255,255,0.1), transparent),
    /* 第 3 層 (最底層) */
    url('texture.png') repeat left top #f5f5f5;
  
  /* 每層可獨立指定 size/position/repeat 等 */
  background-size: 
    32px 32px,      /* icon */
    auto,           /* gradient 無 size */
    auto;           /* texture */
  background-position:
    20px 20px,
    center center,
    left top;
  background-repeat:
    no-repeat,
    no-repeat,
    repeat;
}

💡 多重背景按聲明順序疊加,**第一層最上層**,最後一層最底層。

CSS 漸層作為背景圖片

/**** 線性漸層 ****/
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
background: linear-gradient(to right, red, orange, yellow, green, blue, indigo, violet);
background: linear-gradient(45deg, transparent 50%, rgba(255,255,255,0.1) 50%) repeat;
background-size: 20px 20px;  /* 斜紋圖案 */

/**** 徑向漸層 ****/
background: radial-gradient(circle at center, #fff, #000);
background: radial-gradient(ellipse 80% 50% at 20% 30%, #f00, #00f);

/**** 重複漸層 (無需圖片實現圖案) ****/
background: repeating-linear-gradient(
  45deg,
  transparent,
  transparent 10px,
  rgba(0,0,0,0.05) 10px,
  rgba(0,0,0,0.05) 20px
);

實戰應用模式

1. 全螢幕 Hero Section

.hero {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: 
    linear-gradient(rgba(0,0,0,0.5), rgba(0,0,0,0.5)),
    url('hero.jpg') no-repeat center / cover;
  color: white;
  text-align: center;
}

2. 卡片懸浮光暈效果

.card {
  position: relative;
  background: white;
  border-radius: 12px;
  overflow: hidden;
}
.card::before {
  content: '';
  position: absolute;
  inset: -2px;
  background: linear-gradient(135deg, #667eea, #764ba2);
  border-radius: 14px;
  z-index: -1;
  opacity: 0;
  transition: opacity 0.3s;
}
.card:hover::before { opacity: 1; }

3. 文字漸層 (Background Clip Text)

.gradient-text {
  background: linear-gradient(135deg, #667eea, #764ba2);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  color: transparent;  /* 回退 */
}

效能優化建議

  • 大圖使用 WebP/AVIF,配合 `` 響應式載入
  • 漸層替代圖片:紋理、幾何圖案、簡單裝飾
  • 關鍵背景圖 ``
  • 避免在 `body` 使用 `background-attachment: fixed` 大圖 (捲動卡頓)
  • 動畫背景優先用 `transform`/`opacity` 而非改變 `background-position`
copyright © 萬盛學電腦網 all rights reserved