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,一遍是为老浏览器,另一遍是重写。
posted @ 2011-03-21 18:10  疯狂的代码  阅读(1059)  评论(0编辑  收藏  举报