CSS background-size Property Explained

What the CSS background-size property does, with an example.

Published November 6, 2026

background-size controls the size of a background image: auto, a length, a percentage, cover (fill and crop) or contain (fit inside).

How it works

cover scales the image to cover the whole box, cropping if needed; contain shows the entire image with possible gaps. Two values set width and height separately.

Combine with background-position and background-repeat.

Values and effects

background-size: cover; → fill and crop
background-size: contain; → fit inside
background-size: 50% auto; → half width, auto height
background-size: 100% 100%; → stretch

Practical examples

Hero image

.hero { background: url(hero.jpg) center / cover no-repeat; }

Logo background

.logo { background: url(logo.svg) left center / contain no-repeat; }

Retina sprite

.icon { background-image: url(icons.png); background-size: 200px 100px; }

Common mistakes

  • Using cover without a defined box height.
  • Forgetting no-repeat.
  • Using stretch (100% 100%) and distorting images.

Key takeaways

  • background-size in one line: background-size controls the size of a background image: auto, a length, a percentage, cover (fill and crop) or contain (fit inside).
  • Inspect it live in DevTools → Styles.
  • Test across browsers and screen sizes.

FAQ

cover vs contain?

cover fills the box and crops; contain fits everything and may leave empty space.

Is background-size supported in all browsers?

Yes in all modern browsers; check caniuse.com for edge cases.

More CSS articles