CSS Reference ยท Colors and Backgrounds
The background-size property
Reference for CSS background-size: auto, cover, contain, lengths and percentages, how one and two values work, and live examples with cropping and tiling.
The background-size property sets how large each background image is drawn. Without it, an image appears at its natural size (a gradient fills the whole area). With it, you can scale the image to cover or fit the box, or give it exact dimensions.
The two keywords you will use most are cover, which fills the box and crops what overflows, and contain, which shows the whole image and may leave empty bands.
.hero { background-size: cover; } .logo { background-size: contain; background-repeat: no-repeat; } .dots { background-size: 20px 20px; }
Try it
Syntax
background-size: <bg-size>#; <bg-size> = [ <length-percentage [0,โ]> | auto ]{1,2} | cover | contain background-size: 120px; /* width 120px, height auto */ background-size: 100% auto; background-size: cover, 24px 24px; /* two layers */
- Initial value:
auto - Applies to: all elements
- Inherited: no
- Animatable: yes (lengths and percentages)
- Computed value: for each layer, the keyword, or a pair of lengths, percentages or
auto
Values
| Value | Description |
|---|---|
auto |
Uses the image's natural size. For an image with a ratio but no size, it keeps the ratio; for a gradient it fills the area. |
<length> |
An exact width (and height, if two values are given). One value sets the width and the height is auto. |
<percentage> |
A share of the background positioning area (the padding box by default). |
cover |
Scales the image, keeping its ratio, to the smallest size that covers the area. Parts may be cropped. |
contain |
Scales the image, keeping its ratio, to the largest size that fits inside the area. Empty space may appear. |
| Two values | Width first, height second; either can be auto. |
Usage notes
coverfor photos,containfor logos. Cover never leaves gaps; contain never crops. Pair either withbackground-positionto choose which part stays visible.containoften reveals tiling. If the image does not fill the box, the defaultrepeatshows extra copies in the gap. Addno-repeat.- One value is not a square.
background-size: 50pxgives width 50px and the height follows the image ratio. Use two values for a fixed box. - Gradients are sized too. A gradient has no natural size, so
automeans the whole area. Set a small size and let it repeat for stripes, grids and checkerboards. - Percentages refer to the box, not the image.
100%means as wide as the positioning area. A percentage of0or a negative number is invalid. - Shorthand rule. Inside
background, the size must come right after the position and a slash:center / cover. - Sharpness. Scaling a raster image up blurs it. Provide larger files for high-density displays, or use SVG.
Browser support
Supported in every current browser, including cover and contain.
Related
background-positionโ choose the visible partbackground-repeatโ avoid unwanted tilingbackground,background-imageobject-fitโ the same idea for<img>- Lesson: Backgrounds