background-color(底層)+ 多個 background-image(上層疊加,先寫在上)CSS3 之前僅支持單背景圖;現代瀏覽器支持多背景圖片 + 漸變,按書寫順序從上到下疊加。
background-color:底色兜底.bg-color {
background-color: #caebff; /* 十六進制 */
background-color: rgb(202 235 255); /* RGB */
background-color: hsl(200 100% 90%); /* HSL */
/* 建議:始終設定底色,圖片載入失敗/透明區域有兜底 */
}
background-image:圖片與漸變.bg-image {
/* 單圖片 */
background-image: url('images/pattern.svg');
/* 多圖片:第一層在最上 */
background-image:
url('icon-star.png'),
url('texture.png'),
linear-gradient(45deg, transparent 50%, rgba(255,255,255,.1) 50%);
/* CSS 漸變本質也是圖片 */
background-image: linear-gradient(to right, #0066ff, #00ccff);
}
background-repeat:平鋪控制.repeat-x { background-repeat: repeat-x; } /* 水平平鋪:頂部橫幅 */
.repeat-y { background-repeat: repeat-y; } /* 垂直平鋪:側邊裝飾 */
.no-repeat { background-repeat: no-repeat; } /* 單次顯示:Logo、Hero 圖 */
.space { background-repeat: space; } /* 等間距平鋪,不裁切 */
.round { background-repeat: round; } /* 縮放適配整數個,無縫隙 */
background-position:精準定位(最複雜屬性)三種語法體系:
| 語法類型 | 示例 | 說明 |
|---|---|---|
| 關鍵字 | center、top right、bottom left | 相對容器定位,單值時第二值默認 center |
| 百分比 | 50% 50%、0% 100% | 容器與圖片同比例點對齊。0% 0%=左上,100% 100%=右下 |
| 長度值 | 20px 30px、-10px 50% | 圖片左上角相對容器左上角偏移。負值可實現圖標 Sprite 定位 |
| 四值語法 (CSS3) | right 20px bottom 10px | 指定參考邊 + 偏移,極其強大:background-position: right 20px bottom 10px |
background-size:尺寸控制(CSS3 關鍵屬性)| 值 | 行為 | 場景 |
|---|---|---|
auto | 原始尺寸(默認) | 圖標、精確像素圖 |
cover | 覆蓋整個容器,保持比例,可能裁切 | Hero Banner、全屏背景、卡片封面 |
contain | 完整顯示在容器內,保持比例,可能留白 | Logo、插畫、必須完整可見 |
100px 50px | 強制寬高,扭曲比例 | 極少用,破壞比例 |
50% | 相對容器寬度百分比,高度按比例 | 響應式圖案 |
⚠️ 放大小圖失真:cover 強制拉大 20px 圖片至全屏會嚴重模糊。提供多倍圖或使用 SVG/矢量。
background-attachment:滾動行為.fixed-bg {
background-attachment: fixed; /* 視差效果:背景相對視口固定 */
/* 性能警告:移動端會禁用 fixed 或導致卡頓,建議用 transform: translateZ(0) 模擬 */
}
.scroll-bg {
background-attachment: scroll; /* 默認:跟隨元素滾動 */
}
.local-bg {
background-attachment: local; /* 跟隨元素內容滾動(滾動容器內) */
}
background-clip / background-origin:繪製與定位邊界| 屬性 | 值 | 邊界範圍 |
|---|---|---|
background-clip (繪制裁切) | border-box | 含邊框(默認) |
padding-box | 含內邊距,不含邊框 | |
content-box | 僅內容區 | |
text | 文字形狀裁切(配合 text-fill-color: transparent 實現文字漸變) | |
background-origin (定位原點) | padding-box | 內邊距外緣(默認) |
border-box | 邊框外緣 | |
content-box | 內容區外緣 |
background 簡寫屬性(強大但易混淆)/* 完整順序(任意順序,但建議遵循):
/ */
.bg-full {
background:
url('hero.jpg') center / cover no-repeat fixed padding-box border-box #f0f0f0;
}
/* 多背景簡寫:每層用逗號分隔,顏色只能最後一層 */
.bg-multi {
background:
url('stars.png') top center / 200px no-repeat,
linear-gradient(180deg, #000, transparent 50%),
#0a0a2e; /* 底色 */
}
linear-gradient().linear-basic {
background: linear-gradient(45deg, #ff6b6b, #feca57); /* 角度 + 顏色站 */
}
.linear-stops {
background: linear-gradient(
to right,
#0066ff 0%, /* 0% 起始 */
#00ccff 50%, /* 50% 中間 */
#00ffcc 100% /* 100% 結束 */
);
}
.linear-hard-stops {
background: linear-gradient(
90deg,
#fff 0%, #fff 50%, /* 硬性色停:0-50% 純白 */
#000 50%, #000 100% /* 50-100% 純黑,形成銳利分界 */
);
}
.linear-transparent {
background: linear-gradient(
to bottom,
rgba(0,0,0,0.8), /* 半透明覆蓋 */
transparent
), url('image.jpg'); /* 疊加在圖片上 */
}
radial-gradient().radial-basic {
background: radial-gradient(circle at center, #ff6b6b, #feca57);
}
.radial-position {
background: radial-gradient(ellipse 200px 100px at 20% 80%, #0066ff, transparent);
/* 形狀 尺寸 at 位置 */
}
.radial-repeating {
background: repeating-radial-gradient(
circle at 0 0,
#fff, #fff 10px,
#000 10px, #000 20px
); /* 同心圓環紋理 */
}
background-position 動畫(適用於 background-size > 100% 的條紋/光澗掃過)background-size 動畫(展開/收縮)::before 疊加漸變 + opacity 動畫(性能最佳)@property 註冊自定義屬性(現代瀏覽器,真正動畫漸變參數)背景相關屬性已標準化,無需前綴:
linear-gradient、radial-gradient、repeating-*:全支持無前綴background-clip: text:需 -webkit-background-clip: text(所有主流瀏覽器仍保留 webkit 前綴)conic-gradient():Chrome 69+、Firefox 83+、Safari 14+、Edge 79+,無前綴loading="lazy"。background-blend-mode: multiply, screen, overlay... 疊加多層創造豐富質感,零額外請求。background-attachment: fixed 在移動端:改用 transform: translate3d(0, var(--scroll-y), 0) JS 驅動視差。「背景層是設計師的『無限畫布』:零 DOM 成本、無限疊加、可動畫、可程式化。掌握多背景+漸變+混合模式,能用 1 個 DOM 元素畫出原本需 10 個元素的視覺效果。」