使用CSS3来设置网页背景一般使用的是background属性。background属性有如下几个属性值:background-color、background-image、background-repeat、background-position、background-attachment。
background-color:该属性用于为元素设置背景颜色。
background-image:该属性用于为元素设置背景图片,常用于制作网站效果图、背景图等。
background-repeat:该属性用于定义是否重复背景图片。如果设置为no-repeat,则表示不重复;如果设置为repeat-x,则表示横向重复;如果设置为repeat-y,则表示纵向重复。
background-position:该属性用于定义背景图片的位置。可以使用“top left”、“top right”、“bottom left”、“bottom right”、“center center”等关键字,也可以使用百分比值或者像素值。
background-attachment:该属性用于定义背景图片的固定方式。如果设置为fixed,则表示背景图片会随着页面滚动而固定在元素的位置不动;如果设置为scroll,则表示背景图片会随着页面滚动而滚动。
下面是一个使用CSS3设置网页背景的例子:
p {
background-color: #f5f5f5;
background-image: url(bg.png);
background-repeat: no-repeat;
background-position: center center;
background-attachment: fixed;
}