CSS background-size Property
The background-size property controls the rendered width and height of an element's background images.
It can preserve an image's natural dimensions, assign specific lengths or percentages, scale an image to fit entirely within the background positioning area, or enlarge it to cover that area. The property works with image files, CSS gradients, and multiple background layers.
background-size Syntax
selector {
background-size: cover;
}
The property accepts the keywords cover and contain, one or two sizing values, or a comma-separated list for multiple background images.
.natural-size {
background-size: auto;
}
.cover-image {
background-size: cover;
}
.contained-image {
background-size: contain;
}
.fixed-width {
background-size: 200px;
}
.fixed-dimensions {
background-size: 200px 120px;
}
.percentage-width {
background-size: 50%;
}
.percentage-dimensions {
background-size: 50% 75%;
}
.preserve-proportions {
background-size: 200px auto;
}
.multiple-layers {
background-size: contain, 40px 40px;
}
CSS-wide values such as inherit, initial, revert, revert-layer, and unset are also supported.
When only one length or percentage is supplied, the second dimension defaults to auto.
Property Values
| Value | Description |
|---|---|
auto |
Uses intrinsic image dimensions when available, or calculates dimensions according to the image's intrinsic proportions and positioning area. |
cover |
Scales the image proportionally until the entire background positioning area is covered. Some image content may extend outside the area. |
contain |
Scales the image proportionally so the entire image fits inside the background positioning area. |
<length> |
Specifies a dimension using a CSS length, such as px, rem, or vw. |
<percentage> |
Specifies a dimension relative to the corresponding dimension of the background positioning area. |
auto <length-percentage> |
Calculates the width automatically while explicitly setting the height. |
<length-percentage> auto |
Explicitly sets the width while calculating the height automatically. |
<length-percentage> <length-percentage> |
Sets the width and height independently. The image may be stretched if its original proportions differ. |
comma-separated list |
Specifies sizes for multiple background image layers. |
The initial value is auto auto. The property applies to all elements, is not inherited, and is animatable when corresponding sizing values are interpolable. Other changes, such as switching between cover and contain, are discrete.
Negative lengths and percentages are invalid for background-size. A value of 0 is permitted but can make the background image invisible.
How background-size Works
The browser uses background-size to determine the rendered dimensions of each background image before positioning and repetition are applied.
.image-panel {
width: 320px;
height: 200px;
background-image: url("landscape.jpg");
background-size: 160px 100px;
background-repeat: no-repeat;
}
The image is rendered at 160 pixels wide and 100 pixels tall, regardless of the original file dimensions.
Rendered Size vs. Original Size
An image file may have natural dimensions of 800 by 500 pixels, but background-size can render it at 160 by 100 pixels or another size.
The original file is not modified. Only its appearance within the element changes.
Background Images Do Not Affect Layout
Unlike an HTML <img> element, a CSS background image does not contribute to an element's intrinsic width or height.
Setting background-size: cover does not automatically give the element a height. The element must have dimensions established by its content, padding, layout, or other CSS properties.
Image Size Is Calculated Per Layer
When an element has multiple background images, each image can have its own rendered size.
Once sized, the images are positioned and repeated according to their corresponding background properties.
The cover Value
The cover value scales a background image proportionally until the entire background positioning area is covered.
.hero-banner {
min-height: 350px;
background-image: url("landscape.jpg");
background-size: cover;
background-position: center;
background-repeat: no-repeat;
}
The image maintains its aspect ratio while expanding or shrinking as needed. If the image and positioning area have different proportions, portions of the image extend beyond the area and may be clipped.
How cover Calculates Image Size
Suppose an image is 800 by 400 pixels and its background positioning area is 300 by 200 pixels.
Scaling the image to 300 pixels wide would produce a height of 150 pixels, leaving part of the area uncovered. Instead, cover scales the image to 400 by 200 pixels.
The full height is covered, but 100 pixels of image width extend beyond the positioning area.
Controlling Which Part Remains Visible
.centered-cover {
background-size: cover;
background-position: center;
}
.left-aligned-cover {
background-size: cover;
background-position: left center;
}
.right-aligned-cover {
background-size: cover;
background-position: right center;
}
The background-position property determines which portion of an oversized background image remains visible.
Common Uses
The cover value is commonly used for hero banners, full-width sections, cards, and image-backed page headers where filling the available area is more important than displaying every part of the image.
The contain Value
The contain value scales a background image proportionally so the entire image fits inside the background positioning area.
.logo-panel {
width: 320px;
height: 200px;
background-image: url("logo.png");
background-size: contain;
background-position: center;
background-repeat: no-repeat;
}
The image preserves its aspect ratio and remains completely visible. If its proportions differ from those of the positioning area, unused space may remain horizontally or vertically.
How contain Calculates Image Size
Consider an 800 by 400 pixel image placed in a 300 by 200 pixel positioning area.
The image scales to 300 by 150 pixels, fitting entirely within the available width while leaving 50 pixels of unused vertical space.
Centering the Image
.contained-image {
background-image: url("illustration.png");
background-size: contain;
background-position: center;
background-repeat: no-repeat;
}
The image is centered within the available area, and repetition is disabled so extra copies do not appear in the unused space.
Common Uses
The contain value is useful for logos, diagrams, illustrations, and other graphics that must remain fully visible.
cover vs. contain
| Behavior | cover |
contain |
|---|---|---|
| Preserves aspect ratio | Yes | Yes |
| Fills positioning area | Yes | Not necessarily |
| Entire image fits inside area | Not necessarily | Yes |
| May leave unused space | No | Yes |
| Typical use | Photographic backgrounds | Logos and illustrations |
The auto Value
The auto value allows the browser to determine a background image dimension using its intrinsic size, aspect ratio, and the background positioning area.
.natural-background {
background-image: url("pattern.png");
background-size: auto;
}
For a raster image with intrinsic width and height, auto auto generally displays the image at its natural dimensions.
Setting One Dimension Automatically
.fixed-width {
background-size: 200px auto;
}
.fixed-height {
background-size: auto 120px;
}
When the image has an intrinsic aspect ratio, the automatic dimension is calculated to preserve that ratio.
Example with an Intrinsic Aspect Ratio
Suppose an image has natural dimensions of 800 by 400 pixels, giving it a 2:1 aspect ratio.
.proportional-image {
background-size: 200px auto;
}
The rendered image is 200 pixels wide and 100 pixels tall.
Images Without Intrinsic Dimensions
Not all background images have natural dimensions. CSS gradients, for example, do not have intrinsic dimensions or an intrinsic aspect ratio.
With auto auto, a gradient normally fills the background positioning area. Other image types, such as certain SVG images, may have an intrinsic ratio but only one or neither intrinsic dimension, requiring additional sizing calculations.
Length Values
Length values set background image dimensions using CSS units such as px, rem, em, or vw.
Setting a Fixed Width
.fixed-width-background {
background-image: url("illustration.png");
background-size: 200px;
background-repeat: no-repeat;
}
The image is rendered 200 pixels wide. Its height is calculated automatically when an intrinsic aspect ratio is available.
Setting Width and Height
.fixed-size-background {
background-image: url("pattern.png");
background-size: 80px 50px;
}
The background image is rendered at 80 by 50 pixels. Because both dimensions are specified, the browser does not need to preserve the image's original aspect ratio.
Using Relative Units
.relative-size-background {
background-image: url("icon.png");
background-size: 5rem auto;
background-repeat: no-repeat;
}
The image width is based on the root element's font size. Its height is calculated automatically when possible.
Using calc()
.calculated-background {
background-image: url("illustration.png");
background-size: calc(100% - 40px) auto;
background-repeat: no-repeat;
}
The image width is calculated from the positioning area's width minus 40 pixels.
Length values are useful when the design requires predictable tile dimensions or when image sizing should follow typography and viewport units.
Percentage Values
Percentage values size a background image relative to the background positioning area, not the image's original dimensions.
.half-width-background {
background-image: url("illustration.png");
background-size: 50%;
background-repeat: no-repeat;
}
The image width is 50% of the positioning area's width. Its height is calculated automatically when an intrinsic aspect ratio is available.
Two Percentage Values
.percentage-background {
background-image: url("pattern.png");
background-size: 50% 75%;
}
The image is rendered at 50% of the positioning area's width and 75% of its height.
Percentage Calculation Example
Suppose the positioning area is 400 pixels wide and 200 pixels tall.
.percentage-example {
background-size: 50% 75%;
}
The rendered image becomes 200 pixels wide and 150 pixels tall.
Using 100% 100%
.stretched-background {
background-image: url("illustration.png");
background-size: 100% 100%;
background-repeat: no-repeat;
}
The image stretches to match the full width and height of the positioning area. Its original proportions are not necessarily preserved.
Percentage Sizing vs. cover
background-size: 100% 100% stretches an image independently along both axes, while cover preserves its aspect ratio and may crop portions of the image.
Use percentages when background dimensions should scale with the element, and use cover when maintaining the image's proportions is important.
One-Value and Two-Value Syntax
The number of values determines whether the browser calculates one dimension automatically or uses explicitly supplied dimensions.
One-Value Syntax
.one-value {
background-size: 200px;
}
This is equivalent to:
.one-value {
background-size: 200px auto;
}
The first value sets the width. The height is automatic.
Two-Value Syntax
.two-values {
background-size: 200px 120px;
}
The first value controls width, and the second controls height.
Mixing auto with Explicit Dimensions
.width-controlled {
background-size: 200px auto;
}
.height-controlled {
background-size: auto 120px;
}
These declarations preserve an image's intrinsic proportions when the image provides an intrinsic aspect ratio.
Keyword Syntax
.cover-background {
background-size: cover;
}
.contain-background {
background-size: contain;
}
The keywords cover and contain must be used individually for a background layer. They cannot be combined with a second sizing value.
/* Invalid */
.invalid-background {
background-size: cover 200px;
}
Intrinsic Dimensions and Aspect Ratios
Intrinsic dimensions are the natural width and height supplied by an image. An intrinsic aspect ratio describes the proportional relationship between those dimensions.
Raster Images
Raster formats such as JPEG and PNG typically provide intrinsic width and height. When one dimension is set to auto, the browser can use the image's intrinsic aspect ratio to calculate that dimension.
SVG Images
SVG images may provide explicit dimensions, an intrinsic aspect ratio, both, or neither. Their automatic background sizing depends on which intrinsic properties are available.
For example, an SVG with a viewBox but no explicit width or height may have an intrinsic aspect ratio without intrinsic dimensions.
CSS Gradients
CSS gradients have no intrinsic dimensions or intrinsic aspect ratio. With background-size: auto auto, they use the dimensions of the background positioning area.
Preserving vs. Changing Proportions
.preserved-ratio {
background-size: 200px auto;
}
.changed-ratio {
background-size: 200px 80px;
}
The first declaration preserves an image's intrinsic aspect ratio when available. The second sets both dimensions explicitly and may distort the image.
Use cover, contain, or an automatic dimension when preserving proportions is important.
Working with background-position
The background-position property controls where the sized background image appears within the background positioning area.
Centering a Covered Image
.centered-cover {
background-image: url("landscape.jpg");
background-size: cover;
background-position: center;
background-repeat: no-repeat;
}
When the image extends beyond the positioning area, centering distributes the excess equally on both sides of the affected axis.
Keeping the Top Visible
.top-aligned-cover {
background-image: url("landscape.jpg");
background-size: cover;
background-position: center top;
background-repeat: no-repeat;
}
The image remains aligned with the top of the positioning area. Any excess height extends toward the bottom.
Positioning a Smaller Image
.corner-icon {
background-image: url("icon.png");
background-size: 48px 48px;
background-position: right 15px bottom 15px;
background-repeat: no-repeat;
}
The image is rendered at 48 by 48 pixels and positioned 15 pixels from the right and bottom edges of the positioning area.
Responsive Focal Points
.photo-banner {
background-image: url("landscape.jpg");
background-size: cover;
background-position: 65% center;
background-repeat: no-repeat;
}
Percentage positioning can help keep an important portion of an image visible when the container's dimensions change.
Working with background-repeat
The background-repeat property determines whether the sized background image is repeated across the element.
Creating Repeating Tiles
.tile-pattern {
background-image: url("pattern.png");
background-size: 40px 40px;
background-repeat: repeat;
}
The image is rendered at 40 by 40 pixels and repeated horizontally and vertically.
Preventing Repetition
.single-photo {
background-image: url("photo.jpg");
background-size: contain;
background-position: center;
background-repeat: no-repeat;
}
The image appears once, with no additional copies in any unused space.
Repeating Along One Axis
.horizontal-strip {
background-image: url("stripe.png");
background-size: 40px 12px;
background-repeat: repeat-x;
}
.vertical-strip {
background-image: url("stripe.png");
background-size: 12px 40px;
background-repeat: repeat-y;
}
These declarations create horizontal and vertical repeating strips.
The round Repetition Value
.fitted-tiles {
background-image: url("pattern.png");
background-size: 45px 45px;
background-repeat: round;
}
The round value can adjust tile dimensions so a whole number fits within the positioning area.
Use no-repeat with cover and contain when the intention is to display only one background image.
Background Positioning Area
The background positioning area provides the dimensions used to calculate percentages, cover, and contain.
The background-origin Property
For ordinary elements, the positioning area defaults to the padding box. The background-origin property can change it to the border box or content box.
.content-sized-background {
padding: 20px;
background-image: url("illustration.png");
background-origin: content-box;
background-size: 100% 100%;
background-repeat: no-repeat;
}
The image dimensions are calculated using the content box rather than the padding box.
The background-clip Property
The background-clip property controls the area where the background is painted. It does not normally change the dimensions used by background-size.
.clipped-background {
padding: 20px;
border: 5px solid #1769aa;
background-image: url("pattern.png");
background-origin: padding-box;
background-clip: content-box;
background-size: 50% 50%;
}
The image is sized relative to the padding box but painted only within the content box.
Special Behavior with fixed Backgrounds
When background-attachment: fixed is used, the background positioning area is generally based on the viewport rather than the element's box.
.fixed-background {
background-image: url("landscape.jpg");
background-attachment: fixed;
background-size: cover;
background-position: center;
background-repeat: no-repeat;
}
In this example, cover generally scales the image relative to the viewport. Fixed backgrounds can behave differently on some mobile browsers.
Multiple Background Layers
The background-size property accepts comma-separated values for sizing individual background image layers.
.layered-background {
background-image: url("logo.png"), url("texture.png");
background-size: 120px auto, 40px 40px;
background-position: center, left top;
background-repeat: no-repeat, repeat;
}
The first image is rendered 120 pixels wide with an automatically calculated height. The second image is rendered at 40 by 40 pixels and repeated behind it.
Combining cover with Other Sizes
.decorated-photo {
background-image: url("overlay.png"), url("landscape.jpg");
background-size: 80px 80px, cover;
background-position: right 20px top 20px, center;
background-repeat: no-repeat, no-repeat;
}
The overlay is displayed at a fixed size above a photograph that covers the positioning area.
Matching Sizes to Layers
Each comma-separated size corresponds to the background image at the same position in the background-image list.
If fewer sizes are specified than image layers, the browser repeats the list as necessary. Extra sizes are not used when there are fewer image layers.
Background Layer Order
The first background image is painted above subsequent image layers. The background color is painted beneath all background images.
Multiple background sizes are useful for combining photographs, logos, textures, and decorative graphics within one element.
Sizing CSS Gradients
CSS gradients are background images and can be sized with background-size.
Creating a Repeating Grid
.grid-background {
background-color: #f4f8fc;
background-image: linear-gradient(to right, #b7d5eb 1px, transparent 1px), linear-gradient(to bottom, #b7d5eb 1px, transparent 1px);
background-size: 25px 25px;
}
Each gradient layer is rendered in 25 by 25 pixel tiles, producing horizontal and vertical grid lines.
Creating a Dotted Pattern
.dotted-background {
background-color: #e8f2fb;
background-image: radial-gradient(circle, #1769aa 3px, transparent 4px);
background-size: 24px 24px;
background-repeat: repeat;
}
The radial gradient creates a dot inside each repeated 24 by 24 pixel tile.
Sizing a Non-Repeating Gradient
.gradient-decoration {
background-image: linear-gradient(90deg, #1769aa, #69b7e8);
background-size: 80% 40px;
background-position: center bottom;
background-repeat: no-repeat;
}
The gradient occupies 80% of the positioning area's width and is 40 pixels tall.
Gradients have no intrinsic dimensions. When both background size dimensions are auto, they normally fill the background positioning area.
Using the background Shorthand
The background-size property can be included in the background shorthand immediately after the background position, separated by a slash.
.hero {
background: url("landscape.jpg") center / cover no-repeat;
}
The image is centered, scaled to cover the positioning area, and displayed without repetition.
Including a Background Color
.banner {
background: #173b5e url("landscape.jpg") center / cover no-repeat;
}
The background color is painted beneath the image and can remain visible if the image does not load or has transparent regions.
Setting Explicit Dimensions
.icon-panel {
background: url("icon.png") right 15px center / 48px 48px no-repeat;
}
The image is rendered at 48 by 48 pixels and positioned 15 pixels from the right edge, centered vertically.
Shorthand Reset Behavior
.panel {
background-size: cover;
background: url("landscape.jpg") center no-repeat;
}
The later background shorthand resets background-size to its initial value of auto auto, because no size is included.
To preserve the desired size, include it in the shorthand or place the longhand declaration afterward.
.panel {
background: url("landscape.jpg") center no-repeat;
background-size: cover;
}
Remember that the slash is required when specifying background size in the shorthand. A background size cannot be included without a preceding background position.
background-size Example
The following example compares six background sizing methods using the same CSS-generated graphic. Each panel has the same dimensions, making it easier to see how the background changes.
<div class="background-size-demo">
<div class="size-box auto">auto</div>
<div class="size-box cover">cover</div>
<div class="size-box contain">contain</div>
<div class="size-box fixed">120px 80px</div>
<div class="size-box percentage">50% 50%</div>
<div class="size-box stretch">100% 100%</div>
</div>
.background-size-demo {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
gap: 15px;
}
.background-size-demo .size-box {
display: flex;
align-items: flex-end;
justify-content: center;
min-height: 170px;
padding: 10px;
box-sizing: border-box;
border: 2px solid #1769aa;
border-radius: 6px;
background-color: #e8f2fb;
background-image: radial-gradient(ellipse at 30% 35%, #ffd166 0 14%, transparent 15%), linear-gradient(145deg, transparent 0 45%, #1769aa 46% 65%, #69b7e8 66% 80%, transparent 81%), linear-gradient(#d8f0ff, #a9d8f2);
background-position: center;
background-repeat: no-repeat;
color: #173b5e;
font-weight: bold;
text-align: center;
}
.background-size-demo .auto {
background-size: auto;
}
.background-size-demo .cover {
background-size: cover;
}
.background-size-demo .contain {
background-size: contain;
}
.background-size-demo .fixed {
background-size: 120px 80px;
}
.background-size-demo .percentage {
background-size: 50% 50%;
}
.background-size-demo .stretch {
background-size: 100% 100%;
}
The browser result below uses a single layered CSS gradient as its background graphic. Because gradients have no intrinsic dimensions, auto, cover, and contain may appear similar in this particular example. Raster images with intrinsic dimensions show more pronounced differences between those values.
The fixed-size panel displays each gradient layer at 120 by 80 pixels, while the percentage panel renders each layer at half the positioning area's width and height.
The 100% 100% panel stretches each layer to fill the positioning area. The auto, cover, and contain panels demonstrate how images without intrinsic dimensions are sized.
Important Considerations
Keep the following points in mind when using background-size:
- Initial value: The default is
auto auto. - cover: Preserves the image's aspect ratio while filling the background positioning area. Some image content may be cropped.
- contain: Preserves the aspect ratio and keeps the entire image visible, potentially leaving unused space.
- auto: Uses intrinsic image information when available to determine dimensions.
- One-value syntax: A single length or percentage sets the width and leaves the height automatic.
- Two-value syntax: The first value controls width, and the second controls height.
- Image distortion: Explicitly setting both dimensions may change the original image proportions.
- Percentages: Percentage sizes are relative to the background positioning area.
- Negative values: Negative dimensions are invalid.
- Background position: Use
background-positionto control which part of an oversized image remains visible. - Background repetition: Use
background-repeat: no-repeatwhen an image should appear only once. - Positioning area:
background-originaffects the dimensions used for sizing on ordinary non-fixed backgrounds. - Clipping:
background-clipdetermines where the sized background is painted. - Fixed backgrounds: A fixed background generally uses the viewport as its positioning area.
- Multiple layers: Each background image can have its own size using comma-separated values.
- Gradients: CSS gradients have no intrinsic dimensions and can be resized or repeated to create patterns.
- Shorthand resets: A later
backgroundshorthand declaration resets the size unless a size is explicitly included. - Responsive design:
cover,contain, and percentages adapt to changes in the positioning area's dimensions. - Accessibility: Avoid placing important text inside decorative background images. Use HTML content for information that must remain accessible.
Choose cover when an image must fill its background area, contain when the entire image must remain visible, and explicit dimensions when precise sizing or repeating patterns are required.
Browser Support
The background-size property has Baseline Widely available status and is supported in modern versions of Chrome, Edge, Firefox, Safari, and other major browsers.
The cover, contain, auto, length, percentage, and multiple-background sizing features have broad support in current browsers.
Providing a Fallback
.hero {
background-color: #173b5e;
background-image: url("landscape.jpg");
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
The background color provides a fallback if the image fails to load. Browsers without support for background-size can still display the background image using its default sizing behavior.
Visit Can I Use? to review browser compatibility for background image sizing features.
Specifications
The background-size property is defined in the CSS Backgrounds and Borders Module Level 3 specification.
