background-size sizes background images within their positioning area. It does not resize the element’s own box. Cover and contain preserve an image’s intrinsic proportions when those proportions exist.
Compare cover and contain
The SVG is 300×100, while each frame is 180×120. Cover fills the frame and crops the sides; contain shows the complete image with empty space above and below. The white circles near the image edges make cropping visible.
HTML and CSS
HTML
<p>Cover</p>
<div class="sample cover" aria-hidden="true"></div>
<p>Contain</p>
<div class="sample contain" aria-hidden="true"></div>
CSS
.sample {
width: 180px;
height: 120px;
background-color: #eff6ff;
background-image: url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22300%22%20height%3D%22100%22%20viewBox%3D%220%200%20300%20100%22%3E%3Crect%20width%3D%22150%22%20height%3D%22100%22%20fill%3D%22%232563eb%22%2F%3E%3Crect%20x%3D%22150%22%20width%3D%22150%22%20height%3D%22100%22%20fill%3D%22%230f766e%22%2F%3E%3Ccircle%20cx%3D%2240%22%20cy%3D%2250%22%20r%3D%2224%22%20fill%3D%22white%22%2F%3E%3Ccircle%20cx%3D%22260%22%20cy%3D%2250%22%20r%3D%2224%22%20fill%3D%22white%22%2F%3E%3C%2Fsvg%3E");
background-position: center;
background-repeat: no-repeat;
margin-block: 0.5rem;
}
.cover {
background-size: cover;
}
.contain {
background-size: contain;
}
Edit HTML or CSS to update the preview as you type. Copy code snippet copies the selected tab. Preview spacing and font defaults are not included.
Size a repeating decorative tile
One explicit width and auto height keep the SVG’s ratio. Background-repeat determines how many copies paint; background-size determines each copy’s dimensions.
HTML and CSS
HTML
<div class="sample" aria-hidden="true"></div>
<p>Each decorative tile is 90px wide and 30px tall.</p>
CSS
.sample {
height: 130px;
background-image: url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22300%22%20height%3D%22100%22%20viewBox%3D%220%200%20300%20100%22%3E%3Crect%20width%3D%22150%22%20height%3D%22100%22%20fill%3D%22%232563eb%22%2F%3E%3Crect%20x%3D%22150%22%20width%3D%22150%22%20height%3D%22100%22%20fill%3D%22%230f766e%22%2F%3E%3Ccircle%20cx%3D%2240%22%20cy%3D%2250%22%20r%3D%2224%22%20fill%3D%22white%22%2F%3E%3Ccircle%20cx%3D%22260%22%20cy%3D%2250%22%20r%3D%2224%22%20fill%3D%22white%22%2F%3E%3C%2Fsvg%3E");
background-size: 90px auto;
background-repeat: repeat;
}
Edit HTML or CSS to update the preview as you type. Copy code snippet copies the selected tab. Preview spacing and font defaults are not included.
Values and defaults
The initial value is auto auto and the property is not inherited. One length/percentage sets width with automatic height. Two set width and height; explicitly setting both can change proportions. Percentages use the background positioning area, normally selected by background-origin. Fixed attachment uses the viewport instead.
Auto depends on intrinsic dimensions and ratio. A gradient has neither, so it normally fills its positioning area; it is not a useful substitute for a ratio-bearing image in a cover/contain comparison. Negative sizes are invalid.
Common mistakes
Contain can still tile because the default repeat mode is repeat. Set no-repeat when the intention is one fully visible image. Cover may crop an important subject; use an actual img when the image carries information.
Learn background-position, background-repeat, and object-fit.
Sources and browser support
MDN: background-size describes the syntax and links to the specification and compatibility data. Check individual values in that table when supporting older browsers.