1 out of 5 rated this helpful - Rate this topic

background-size property

Specifies the size of the background images.

CSS Backgrounds and Borders Module Level 3, Section 3.9Internet Explorer 9

Syntax

background-size: [ <length> | <percentage> | auto ] {1,2} | cover | contain [ , [ <length> | <percentage> | auto ] {1,2} | cover | contain ]*

Property values

One or more of the following values. If layering multiple images, each image's value(s) are separated from each other using commas.

length

A floating-point number, followed by a supported length unit.

percentage

An integer, followed by a percent (%). A percentage value is relative to the background positioning area.

auto

Initial value. See Remarks.

contain

Scale the image, while preserving its intrinsic aspect ratio (if any), to the largest size such that both its width and its height can fit inside the background positioning area.

cover

Scale the image, while preserving its intrinsic aspect ratio (if any), to the smallest size such that both its width and its height can completely cover the background positioning area.

CSS information

Applies ToAll elements
Mediavisual
Inheritedno
Initial Valueauto

Standards information

Remarks

The length, percentage, and auto values can be used twice per image layer. The first value is the width of the corresponding image, and the second value is the image's height. If only one value is given, the second is assumed to be auto.

An auto value for one dimension is resolved by using the image's intrinsic ratio and the size of the other dimension. If either of these values is not available, the image's intrinsic size is used. If the image's intrinsic size is not available, it is assigned the value of 100%. If both values are auto, use the intrinsic width, height, or both, of the image. If the image has neither an intrinsic width nor an intrinsic height, its size is determined as for contain.

Negative values are not allowed.

In Windows Internet Explorer 9, the background of a box can have multiple layers. The number of layers is determined by the number of comma-separated values in the background-image property. Each of the images is sized, positioned, and tiled according to the corresponding value in the other background properties (background-attachment, background-clip, background-origin, background-position, background-repeat, and background-size). The first image in the list is the layer closest to the user, the next one is painted behind the first, and so on.

Examples

In this example, the background image is stretched in both directions to completely cover the content area.


div {
  background-image: url(puppy.png);
  background-repeat: no-repeat;
  background-size: 100% 100%;
  background-origin: content-box 
}


In this example, the image is stretched so that it is duplicated twice horizontally and the aspect ratio is preserved.


p {
  background-image: url(poster.jpg);
  background-size: 50% auto;
  background-origin: border-box 
}

In this example, the image is forced to a size of 20 pixels by 20 pixels.


p {
  background-size: 20px 20px;
  background-image: url(tiling.png)
}

In this example, the image's intrinsic size is used. This is the default sizing behavior.


body {
  background-size: auto;
  background-image: url(apples.png) 
}

See also

CSSStyleDeclaration
currentStyle
defaults
LayoutRect
runtimeStyle
style
style
Reference
background-color
background-image
background-repeat
background-attachment
background-position
background-clip
background-origin
background

 

 

Send comments about this topic to Microsoft

Build date: 11/29/2012

Did you find this helpful?
(1500 characters remaining)

Community Additions

ADD
© 2013 Microsoft. All rights reserved.