Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

CSS3 background-size property

CSS3 background-size property

Example

Specify the size of the background image:

div

{

background:

url

(

img

_

flwr

.

gif

)

;

background-size:

80

px

60

px

;

background-repeat:

no-repeat

;

}

Try it »

There are more examples at the bottom of this page.


Browser support

The numbers in the table indicate the first browser version that supports this property.

A number immediately before -webkit-, -ms-, or -moz- indicates the first browser version that supports that prefixed property.

property
background-size  4.0
1.0 -webkit- 9.0 4.0
3.6 -moz- 4.1
3.0 -webkit- 10.5
10.0 -o-

Tag Definition and Usage Notes

The background-size property sets the size of the background image.

The image can keep its original size, be stretched to a new size, or be scaled to the size of the available space of the element while maintaining its original proportions.

Default value: auto
Inherited: no
Version: CSS3
JavaScript syntax: object object.style.backgroundSize="60px 80px"

Syntax

background-size:

length

|

percentage

|cover|contain;

Value Description
length Sets the height and width of the background image. The first value sets the width, the second sets the height. If only one value is given, the second is set to auto
percentage Percentages are calculated relative to the background positioning area. The first value sets the width, the second sets the height; values are separated by spaces to specify height and width, and multiple backgrounds are separated by commas. If only one value is given, the second is set to "auto"
cover This keeps the image's aspect ratio and scales the image to the smallest size that will completely cover the background positioning area.
contain This keeps the image's aspect ratio and scales the image to the largest size that will fit within the background positioning area.

Example

/* keywords */

background-size

:

cover

background-size

:

contain

/* one value: this value specifies the width of the image, and the height is implicitly auto */

background-size

:

50%

background-size

:

3em

background-size

:

12px

background-size

:

auto

/* two values */

/* the first value specifies the width of the image, the second specifies the height */

background-size

:

50%

auto

background-size

:

3em

25%

background-size

:

auto

6px

background-size

:

auto

auto

/* multiple comma-separated values: set multiple backgrounds */

background-size

:

auto

,

auto

   

/* different from background-size: auto auto */

background-size

:

50%

,

25%

,

25%

background-size

:

6px

,

auto

,

contain

/* global properties */

background-size

:

inherit

;

background-size

:

initial

;

background-size

:

unset

;


Online example

Stretch the background image
Stretch the background image to completely cover the content area.

Four background images stretched horizontally
Stretch 4 background images horizontally.


Related Articles

CSS3 tutorial: CSS3 Backgrounds