CSS3 background-size
Property
Example
Specify the size of the background image:
div {
background: url(img_flwr.gif);
background-size: 80px 60px;
background-repeat: no-repeat;
}
Browser Support
The numbers in the table specify the first browser version that fully supports the property.
Numbers followed by -webkit-, -ms-, or -moz- specify the first version that worked with a prefix.
Property | Chrome | IE | Firefox | Safari | Opera |
---|---|---|---|---|---|
background-size | 4.0 <br>1.0 -webkit- | 9.0 | 4.0 <br>3.6 -moz- | 4.1 <br>3.0 -webkit- | 10.5 <br>10.0 -o- |
Definition and Usage
The background-size
property sets the size of the background images.
The image can be left to its natural size, stretched, or constrained to fit the available space while maintaining its original aspect ratio.
Default value: | auto |
---|---|
Inherited: | no |
--- | --- |
Version: | CSS3 |
--- | --- |
JavaScript syntax: | object.style.backgroundSize="60px 80px" |
--- | --- |
Syntax
Value | Description |
---|---|
length | Sets the width and height of the background image. The first value sets the width, the second value sets the height. If only one value is given, the second is set to auto . |
percentage | Sets the width and height of the background image in percent of the parent element. The first value sets the width, the second value sets the height. If only one value is given, the second is set to auto . |
cover | Scales the image while maintaining its aspect ratio to cover the entire background positioning area. |
contain | Scales the image while maintaining its aspect ratio to fit within the background positioning area. |
Example
/* Keywords */
background-size: cover;
background-size: contain;
/* One value: this value specifies the width, the height becomes auto */
background-size: 50%;
background-size: 3em;
background-size: 12px;
background-size: auto;
/* Two values */
/* The first value specifies the width, the second value specifies the height */
background-size: 50% auto;
background-size: 3em 25%;
background-size: auto 6px;
background-size: auto auto;
/* Comma-separated multiple values: sets multiple backgrounds */
background-size: auto, auto; /* Different from background-size: auto auto */
background-size: 50%, 25%, 25%;
background-size: 6px, auto, contain;
/* Global values */
background-size: inherit;
background-size: initial;
background-size: unset;
Online Examples
Four Background Images Stretched Horizontally
Related Articles
CSS3 Tutorial: CSS3 Backgrounds