CSS3 包含几个新的背景属性,它们提供了对背景更强大的控制。segmentfault
background-size
: 规定背景图片的尺寸。浏览器
background-origin
: 规定背景图片的定位区域。url
background-clip
: 规定背景的绘制区域。spa
Internet Explorer 9+、Firefox、Chrome、Safari 以及 Opera 支持新的背景属性。code
background-size
属性规定背景图像的尺寸。图片
body{ background: url('bg_flower.gif'); background-size: 63px 100px; background-repeat: no-repeat; padding-top: 80px; }
设置背景尺寸的方式有如四种ip
设置背景图像的高度和宽度。rem
第一个值设置宽度,第二个值设置高度。get
若是只设置一个值,则第二个值会被设置为 auto
。it
以父元素的百分比来设置背景图像的宽度和高度。
第一个值设置宽度,第二个值设置高度。
若是只设置一个值,则第二个值会被设置为 auto
。
使背景图像扩充到足够大,以使背景图像彻底覆盖背景区域。
你能够将其等同于 background-size: 100% auto;
背景图像的某些部分也许没法显示在背景定位区域中。
把背景图像扩展至最大尺寸,以使其宽度和高度彻底适应内容区域。
使背景图像最大,但不会超出背景区域,等效下面的状况:
若是背景区域宽大于高的尺寸,至关于 background-size: auto 100%;
若是背景区域高大于宽的尺寸,至关于 background-size: 100% auto;
下图展现了不一样属性值的效果。
background-origin
属性规定 background-position
属性相对于什么位置来定位。
背景图片能够放置于 content-box
、padding-box
或 border-box
区域。
div { background: url('bg_flower.gif'); background-repeat: no-repeat; background-size:100% 100%; background-origin:content-box; }
定位区域默认是在 padding-box
.
CSS3 容许您为元素使用多个背景图像。
body { background-image:url('bg_flower.gif'),url('bg_flower_2.gif'); }
每一个图像还能够使用不一样的属性。
p { background: url('turq_spiral.png') 30px -10px no-repeat, url('pink_spiral.png') 145px 0px no-repeat, url('gray_spiral.png') 140px -30px no-repeat, #ffbd75; }
background-clip
属性规定背景颜色的绘制区域。同background-origin
的属性值。
background-clip: border-box|padding-box|content-box;
其余背景属性请参照 CSS 背景