Latest web development tutorials

HTML DOM Style backgroundSize property

Style Object Reference Style Objects

Examples

Specify the background image size:

document.getElementById("myDIV").style.backgroundSize="60px 120px";

try it"

Definition and Usage

backgroundSize property sets or returns the size of the background image.


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

IE9 +, Firefox, Opera, Chrome and Safari support backgroundSize property.


grammar

Back backgroundSize properties:

object .style.backgroundSize

Setting backgroundSize properties:

object .style.backgroundSize="auto|length|cover|contain|intial|inherit"

Property Value

描述
auto 默认值。背景图像包含它的宽度和高度。
length 设置背景图像的宽度和高度。第一个设置宽度,第二个值设置高度。如果只给出一个值,则第二个值被设置为 "auto"。
percentage 以父元素的百分比设置背景图像的宽度和高度。第一个设置宽度,第二个值设置高度。如果只给出一个值,则第二个值被设置为 "auto"。
cover 此时会保持图像的纵横比,并将图像缩放成将完全覆盖背景区域的最小尺寸。背景图像的一些部分可能不会显示在背景定位区域内。
contain 此时会保持图像的纵横比,并将图像缩放成将适合内容区域的最大尺寸。
initial 设置该属性为它的默认值。请参阅 initial
inherit 从父元素继承该属性。请参阅 inherit

technical details

Defaults: auto
return value: String representing the background-size attribute of the element.
CSS version CSS3


related articles

CSS Reference Manual: background-size property


Style Object Reference Style Objects