背景八大子屬性
| 屬性 | 說明 | 預設值 |
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`