每個元素盒子包含兩層:
background-color)+ 多張背景圖片(background-image),圖片疊加在顏色之上CSS3 前僅支援單一背景圖,現可堆疊多圖與漸變。
background-colorbackground-imagebackground-repeatbackground-positionbackground-size (CSS3)background-attachmentbackground 簡寫background-clip、background-origin、background-break(支援度有限)background-color: #caebff;
background-image: url(images/blue_circle.png);
預設從左上角起點,水平垂直重複鋪滿。
background-repeat: repeat | repeat-x | repeat-y | no-repeat;
最複雜屬性,五關鍵字 top left bottom right center 任意組合。
center center 或簡寫 center 置中。50% 50% 配合 no-repeat 置中。單值時第二值預設 center。px 等絕對/相對單位,可用負值將圖片部分移出可視區。background-size: 50%; /* 填滿一半 */
background-size: 100px 50px; /* 指定寬高 */
background-size: cover; /* 填滿並保持比例(可能裁切) */
background-size: contain; /* 完整顯示並保持比例(可能留白) */
⚠️ 小圖放大會失真。
background-attachment: scroll | fixed;
fixed 常用於 body 中心水印,不隨頁面滾動。
body {
background: url(images/watermark.png) center #fff no-repeat contain fixed;
}
省略值會套用預設值。
p {
background:
url(images/turq_spiral.png) 30px -10px no-repeat,
url(images/pink_spiral.png) 145px 0px no-repeat,
url(images/gray_spiral.png) 140px -30px no-repeat,
#ffbd75; /* 最後的顏色為背景色 */
}
先寫的圖層在上層。
漸變本質是 CSS 生成的背景圖片,可用 background-image 或 background 簡寫。
CSS3 屬性標準化前需加前綴:
-moz-transform: skewX(-45deg); /* Firefox */
-webkit-transform: skewX(-45deg); /* Chrome/Safari */
-ms-transform: skewX(-45deg); /* IE */
-o-transform: skewX(-45deg); /* Opera */
transform: skewX(-45deg); /* W3C 標準最後寫 */
必須加 VSP 的屬性(含背景圖片/漸變相關):
border-image, translate,
linear-gradient, transition,
radial-gradient, background*,
transform, background-image*,
transform-origin