CSS3屬性之四:Multiple backgrounds

語法:

background[background-image] | [background-origin] | [background-clip] | [background-repeat] | [background-size] | [background-position]

相關屬性background-image | background-origin | background-clip | background-repeat | background-size | background-position

取值:

<background-image>
指定或檢索對象的背景圖像。
<background-origin>
指定背景的顯示區域。
<background-clip>
指定背景的裁剪區域。
<background-repeat>
設置或檢索對象的背景圖像是否及如何重複鋪排。
<background-size>
指定背景圖片的大小。
<background-position>
設置或檢索對象的背景圖像位置。

說明:

多重背景圖象,可以把不同背景圖象只放到一個塊元素裏。

多個圖片url之間使用逗號隔開即可;如果有多個背景圖片,而其他屬性只有一個(例如background-repeat只有一個),表明所有背景圖片應用該屬性值。

CSS3中在容器的多層背景,各子屬性與逗號來分隔值,如果指定的值,如下:

background-image: w1, w2, w3,…, wM background-repeat: x1, x2, x3,…, xR background-size: y1, y2, y3,…, yS background-position: s1, s2, s3,…, sP

當背景層的數值是N = max(M,R, S, P)

每個屬性可以假如它的值爲n,通過重複指定的值如下所示:

background-image: w1,…wM, w1,…wM, w1,… /* N values */ background-repeat: x1,…xR, x1,…xR, x1,… /* N values */ background-size: y1,…yS, y1,…yS, y1,… /* N values */ background-position: s1,…rP, s1,…rP, s1,… /* N values */

範例書寫:

background-image: url(img/multiple-backgrounds.png), 
           url(img/multiple-backgrounds.png),
            url(img/multiple-backgrounds.png);
background-position: left top, -320px bottom, -640px top; background-repeat: no-repeat, no-repeat, repeat-y;

也可以簡寫:

background: url(img/multiple-backgrounds.png) left top no-repeat,
        url(img/multiple-backgrounds.png) -320px bottom no-repeat,
        url(img/multiple-backgrounds.png) -640px top repeat-y;
  
  Background屬性在CSS3樣式中已經徹底改革,開始支持多背景圖片。
  
  舉個簡單的例子:
複製代碼
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>Multiple Backgrounds</title> <style> .box { background: url(Tulips.jpg) 0 0 no-repeat, /**/ url(Penguins.jpg) 100% 0 no-repeat; /* 企鵝 */ width:500px; height:250px; } </style> </head> <body> <div class="box"></div> </body> </html>
複製代碼
  運行效果(Chrome):
  以上,通過逗號分隔符,插入了兩張背景圖像,第一個的定位是(0,0),第二個的定位是(100%,0)。要確定你對不支持的瀏覽器使用了備用圖片,否則,它將跳此屬性,使背景圖像留空。對舊瀏覽器的補償,要添加一張單獨的圖像給老瀏覽器用,像IE7。要定義兩遍background,一遍是爲老瀏覽器,另一遍是重寫。

發佈了18 篇原創文章 · 獲贊 10 · 訪問量 16萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章