CSS background-size Property

Beginner
⏱️ 5 min read
📚 Updated: Jun 2026
🎯 4 Examples
Backgrounds & Layout

What You’ll Learn

The background-size property controls how large a background image appears inside an element. It is essential for responsive hero banners, logos, and repeating patterns.

01

cover

Fill the container.

02

contain

Show the full image.

03

auto

Natural image size.

04

Pixels

Exact width and height.

05

Percentages

Relative to the element.

06

Responsive

Pair with no-repeat.

Definition and Usage

The background-size CSS property sets the dimensions of a background image. You can scale backgrounds to fill a container, fit entirely inside it, or set exact widths and heights.

This property is widely used in responsive design so backgrounds look good on phones, tablets, and desktops without stretching awkwardly or leaving unwanted empty space.

💡
Beginner Tip

For hero sections, the most common combination is background-size: cover; with background-repeat: no-repeat; and background-position: center;.

📝 Syntax

Apply background-size after setting a background-image:

syntax.css
selector {
  background-size: <bg-size>#;
}

Basic Example

background-size.css
.hero {
  background-image: url("photo.jpg");
  background-repeat: no-repeat;
  background-size: cover;
  background-position: center;
}

Syntax Rules

  • The initial value is auto, which keeps the image’s natural size.
  • One value sets both width and height; two values set width then height.
  • Keywords cover and contain preserve aspect ratio while scaling.
  • Percentages are relative to the background positioning area.
  • Use with background-repeat: no-repeat for single-image layouts.

⚡ Quick Reference

QuestionAnswer
Initial valueauto
Applies toBackground image dimensions
InheritedNo
AnimatableYes
Common useHero banners, logos, responsive photos, pattern tiles

Default Value

The initial value of background-size is auto. That keeps each dimension at the background image’s natural size unless another value overrides it.

💎 Property Values

These are the most common ways to size background images with background-size.

ValueExampleMeaning
autobackground-size: auto;Keeps the image at its natural size
coverbackground-size: cover;Scales to fill the container; may crop edges
containbackground-size: contain;Scales to fit entirely inside the container
Lengthbackground-size: 100px 200px;Sets exact width and height
Percentagebackground-size: 50% 50%;Sizes relative to the element dimensions
cover

Fills the container completely while preserving aspect ratio.

Common for hero banners.

contain

Shows the entire background without cropping.

Useful for logos.

auto

Keeps the image at its intrinsic dimensions.

Default sizing behavior.

40px 40px

Sets an exact tile size in pixels or other length units.

Great for patterns.

50% 50%

Scales relative to the element width and height.

Responsive sizing.

cover vs contain

ValueBehaviorBest for
coverFills the entire container; may crop parts of the imageFull-width hero sections and banner backgrounds
containFits the whole image inside the container; may leave empty spaceLogos, product images, and icons that must stay fully visible
autoUses the image’s natural dimensionsSmall decorative assets at their original size
Length / %Sets explicit width and height valuesPattern tiles and precise layout control

👀 Live Preview

The same gradient background with four different size values:

cover
contain
48px
50%

All panels use background-repeat: no-repeat; and background-position: center;.

Examples Gallery

Try background-size with cover, contain, pixel dimensions, and a three-value comparison.

📚 Keyword Sizing

Use cover and contain for responsive layouts that preserve aspect ratio.

Example 1 — Fill a Hero with cover

Scale a background to completely fill a container, even if parts are cropped.

cover-size.css
.hero {
  background-image: url("photo.jpg");
  background-repeat: no-repeat;
  background-size: cover;
  background-position: center;
}
Try It Yourself

How It Works

cover scales the image until the container is filled. Excess parts may be cropped to maintain aspect ratio.

Example 2 — Show the Full Image with contain

Keep the entire background visible inside the element without cropping.

contain-size.css
.logo-panel {
  background-image: url("logo.png");
  background-repeat: no-repeat;
  background-size: contain;
  background-position: center;
}
Try It Yourself

How It Works

contain scales the image until it fits entirely inside the box. Empty space may appear on the sides or top and bottom.

🎨 Exact Dimensions

Use pixel and percentage values when you need precise control over background dimensions.

Example 3 — Fixed Pixel Size

Set an exact background width and height for icons or pattern tiles.

pixel-size.css
.tile {
  background-image: url("dot.svg");
  background-repeat: no-repeat;
  background-size: 80px 80px;
  background-position: center;
}
Try It Yourself

How It Works

Two length values set width and height independently. This is useful for pattern tiles and fixed-size decorative assets.

Example 4 — Compare auto, cover, and contain

See how the three most common sizing modes behave on the same background.

size-compare.css
.auto { background-size: auto; }
.cover { background-size: cover; }
.contain { background-size: contain; }
Try It Yourself

How It Works

auto keeps natural dimensions, cover fills the box, and contain shows the full background without cropping.

🧠 How background-size Works

1

You add a background image

Set a photo, logo, or pattern with background-image.

Background
2

You choose a size mode

Use cover, contain, auto, or explicit dimensions.

Sizing rule
3

You position and repeat

Pair with background-repeat: no-repeat and background-position for polished layouts.

Layout polish
=

Responsive background control

Backgrounds scale correctly across screen sizes without awkward stretching.

Universal Browser Support

background-size is supported in all modern browsers. Basic keywords and length values work back to IE9.

Baseline · Modern browsers

Scale backgrounds confidently everywhere

Chrome, Firefox, Safari, Edge, and Opera all support cover, contain, and custom dimensions.

99% Modern browser support
Google Chrome1+ · Desktop & Mobile
Full support
Mozilla Firefox3.6+ · Desktop & Mobile
Full support
Apple Safari4.1+ · macOS & iOS
Full support
Microsoft Edge12+ · Modern versions
Full support
Opera10.5+ · Modern versions
Full support

Fallback behavior

For very old browsers, provide a fixed height container and test that cover and contain behave as expected.

💻
IE9+ Supports background-size with vendor prefixes in legacy codebases
Legacy
background-size property 99% supported

Bottom line: Use background-size freely in modern projects for responsive background images.

Conclusion

The background-size property gives you precise control over how large background images appear. Whether you need a full-bleed hero with cover or a fully visible logo with contain, this property is a core tool for responsive web design.

Combine it with background-repeat, background-position, and background-image to build polished background layouts on any screen size.

💡 Best Practices

✅ Do

  • Use cover for hero banners that should fill the container
  • Use contain for logos and images that must stay fully visible
  • Pair with background-repeat: no-repeat for single images
  • Set explicit pixel sizes for repeating pattern tiles
  • Test backgrounds at mobile and desktop widths

❌ Don’t

  • Stretch images with unequal width and height unless intentional
  • Use cover when every part of the image must remain visible
  • Forget to set a min-height on containers using background images
  • Ignore file size and performance for large background photos
  • Mix up cover and contain when choosing a layout

Key Takeaways

Knowledge Unlocked

Five things to remember about background-size

Use these points when sizing background images.

5
Core concepts
🖼️02

contain Fits

Shows full image.

Logo
03

auto Default

Natural size.

Default
📝04

Two Values

Width and height.

Syntax
🛠05

With no-repeat

Single-image layouts.

Pair

❓ Frequently Asked Questions

The background-size property sets the width and height of a background image. It controls how large the background appears inside an element.
The initial value is auto, which keeps the background image at its natural size in each dimension.
cover scales the image to fill the entire container and may crop edges. contain scales the image to fit entirely inside the container without cropping, which may leave empty space.
Yes. You can provide one value for both axes or two values such as background-size: 100px 200px or 50% 75%.
Often yes. Pair background-size: cover or contain with background-repeat: no-repeat for hero images and logos.

Practice in the Live Editor

Open the HTML editor, try cover and contain, and experiment with background-size instantly.

HTML Editor →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful