CSS / Reference / background-size: Cover, Contain, and Background Sizing
CSS background-size sets the rendered size of an element’s background image. cover keeps the image’s aspect ratio and fills the area, which may crop the image. contain keeps the whole image visible and may leave some space uncovered.
Background-size values and when to use them
Use cover for a decorative hero photo that can be cropped, and contain for a logo whose edges must remain visible. Add background-repeat: no-repeat if you want only one copy. With two sizes, width comes first and height second. With one size, height is auto. The initial value is auto auto, and the property is not inherited.
| Value | Result |
|---|---|
cover |
Uses the smallest size that fills the area while preserving the aspect ratio. |
contain |
Uses the largest size that fits the entire image inside the area while preserving its aspect ratio. |
auto (auto auto) |
Uses intrinsic dimensions when available. See below for images that lack dimensions or an aspect ratio. |
120px (120px auto) |
Sets the width to 120px. The height follows the intrinsic aspect ratio when one exists. |
50% auto |
Uses half the background positioning area’s width. Height scales proportionally when the image has an aspect ratio. |
100% 100% |
Sizes the two axes independently to the area. This can distort the image. |
CSS Backgrounds and Borders definition of background-size
Compare cover and contain with the same image
Save this as background-size-example.html in UTF-8 and open it in a browser. The file includes its own 160×80 SVG. On a wide screen, the two boxes sit side by side and differ only in background size.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Compare CSS background image sizes</title>
<style>
body { margin: 16px; font: 1rem/1.5 system-ui, sans-serif; overflow-wrap: anywhere; }
.samples { display: flex; flex-wrap: wrap; gap: 16px; }
figure { margin: 0; }
figcaption { margin-bottom: 0.25rem; }
.sample {
width: min(240px, calc(100vw - 96px));
height: 150px; padding: 20px; border: 12px dashed #526a7e;
background-color: #eaf2f8;
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='160' height='80' viewBox='0 0 160 80'%3E%3Crect width='160' height='80' fill='%23006c9c'/%3E%3Ccircle cx='26' cy='40' r='14' fill='%23ffd166'/%3E%3Cpath d='M134 14v52' stroke='%23ff9d93' stroke-width='6'/%3E%3C/svg%3E");
background-origin: padding-box; background-clip: padding-box;
background-repeat: no-repeat; background-position: left top;
}
.cover { background-size: cover; }
.contain { background-size: contain; }
</style>
</head>
<body>
<main>
<h1>Compare CSS background image sizes</h1>
<div class="samples">
<figure><figcaption>cover</figcaption><div class="sample cover"></div></figure>
<figure><figcaption>contain</figcaption><div class="sample contain"></div></figure>
</div>
</main>
</body>
</html>
The blue image fills the cover box at left, but its pink bar is cropped. In the contain box at right, both the yellow circle and pink bar are visible, and the pale background shows below them. A narrower screen changes the box width and can change which parts are cropped.
Calculate percentages and control cropping
Percentages, cover and contain all use the background positioning area selected by background-origin. The example uses padding-box, so the area includes content and padding and excludes the border. On a wide screen, it measures 240 + 40 = 280px by 150 + 40 = 190px.
- For the 2:1 image,
coveryields 380×190px, enough to fill both axes.containyields 280×140px, leaving 50px below the image. - Add
background-position: centerto.cover. The size stays the same, but shifting the crop brings the pink bar into view. Remove the declaration when done. - Change only the size on
.containto50% auto. The image becomes 140×70px on a wide screen. Restorecontainafter comparing.
background-origin chooses the box for positioning and sizing calculations. background-clip controls where paint can appear. Both are set to padding-box in this example so the image cannot show through the dashed border.
Intrinsic sizes and multiple background layers
For a photo with intrinsic dimensions and an aspect ratio, auto auto displays its intrinsic size. A CSS gradient has neither, so with auto auto it fills the positioning area. An SVG’s intrinsic dimensions and ratio depend on such details as dimensions and viewBox.
CSS Images rules for cover and contain sizing
For multiple images, list the sizes in the same comma-separated order as background-image. The CSS below crops photo.jpg from the same folder to fit a banner and places a translucent gradient over it.
.banner {
background-image: linear-gradient(#0008, #0008), url('photo.jpg');
background-size: 100% 100%, cover;
background-position: center;
background-repeat: no-repeat;
}
Give the banner height through its contents or min-height. Swap the two sizes to check how the first applies to the gradient and the second to the photo.
Troubleshoot unexpected background sizing
- If the background is missing, check the box dimensions and the image request in the browser’s Network panel. A CSS background image does not give the element height.
- If more copies of the image fill the space left by
contain, inspect the computedbackground-repeat.roundcan also resize an image to fit whole tiles. - If the computed size is unexpectedly
auto auto, check whether a laterbackgroundshorthand reset it. In the shorthand, put size after position and a slash, as incenter / cover.
Related examples and property references. Setting CSS background colors and images · Controlling tiling with CSS background-repeat · background-position · background-origin





