Skip to content
Tutorials Center
My learning

Topics

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.

CSS
.hero { background-size: cover; }
.logo { background-size: contain; background-repeat: no-repeat; }
.dots { background-size: 20px 20px; }

Try it

auto, length, percentage, contain, cover

Syntax

CSS
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

  • cover for photos, contain for logos. Cover never leaves gaps; contain never crops. Pair either with background-position to choose which part stays visible.
  • contain often reveals tiling. If the image does not fill the box, the default repeat shows extra copies in the gap. Add no-repeat.
  • One value is not a square. background-size: 50px gives 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 auto means 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 of 0 or 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.