Use background-size: cover to fill an element with an image while preserving its proportions; some of the image may be cropped. Use contain to keep the whole image visible, accepting empty space if the image and element have different proportions. For a specific rendered width, use one length or percentage followed by auto, such as 100% auto. Set background-position to control the visible area and background-repeat: no-repeat when you do not want tiling.
Choose a size based on what must remain visible
The background-size property controls how large a background image is rendered within its background positioning area. The right value depends on whether the priority is filling the element, preserving every part of the image, or controlling its dimensions. A background image is decorative or layered behind an element’s content; this property does not set the element’s width or height.
| Goal | Value | What to expect |
|---|---|---|
| Fill the area edge to edge | cover |
Preserves image proportions but crops the dimension that overflows. |
| Show the whole image | contain |
Preserves proportions without cropping; unused space may remain. |
| Control rendered width while preserving proportions | 480px auto or 100% auto |
Sets the width and derives height from the image’s intrinsic ratio when available. |
| Set exact rendered width and height | 480px 180px |
Sets both dimensions; if their ratio differs from the source, the image can look distorted. |
| Use the image’s natural dimensions | auto or auto auto |
Uses intrinsic dimensions when available, which may not fit the element. |
Fill a hero, banner, or card with cover
For a photographic hero or banner where empty edges would be undesirable, set the size to cover. The browser scales the image, keeping its ratio, until it covers the background positioning area. If the element and image have different aspect ratios, part of the image must extend beyond the area and will not be visible.
.hero {
min-height: 24rem;
background-image: url("hero.jpg");
background-size: cover;
background-position: center;
background-repeat: no-repeat;
}
background-position: center makes the crop balanced around the center by default. If the important subject is not centered, shift the crop with a position such as top or 70% 40%. Positioning changes which part of the rendered image is visible; it does not prevent cropping caused by cover.
Recommended Free Tools
When cover is the right choice
- Use it for a large background photo where the element should have no uncovered bands.
- Check the result at different element shapes: a wide desktop banner and a narrow mobile panel will crop different portions of the same source.
- Choose a source image with enough space around its focal subject, or adjust the positioning to keep that subject in frame.
Keep the complete image visible with contain
Use contain when no part of the image may be cropped, for example a diagram or other graphic that needs to remain wholly visible. The browser scales the image so the entire image fits inside the background positioning area without distortion. If the aspect ratios differ, the image cannot fill both dimensions, so some of the area remains uncovered.
#1 Best Overall
.diagram-panel {
background-color: #f2f4f7;
background-image: url("diagram.png");
background-size: contain;
background-position: center;
background-repeat: no-repeat;
}
The background color shows through areas not covered by the image and transparent parts of the image. Keep background-repeat: no-repeat when you want a single complete image: if the image is smaller than the area and repetition is not disabled, it can tile.
Set a width, percentage, or exact dimensions
Use explicit dimensions when you know how wide the image should render, rather than asking it to fill the area. One length or percentage sets the width; the other dimension defaults to auto. When the image has intrinsic proportions, that lets its height follow its ratio instead of forcing it into a different shape.
.banner {
background-image: url("banner.jpg");
background-size: 480px auto;
background-position: center top;
background-repeat: no-repeat;
}
To scale to the positioning area’s width while preserving the image ratio, use 100% auto:
Free tools Windows power users keep installed
One-click scans. No signup required.
Rank #2
.banner {
background-image: url("banner.jpg");
background-size: 100% auto;
background-position: center top;
background-repeat: no-repeat;
}
This makes the rendered background image as wide as the area, but it does not make the image tall enough to fill the element. A tall element may show space below the image. Unlike cover, width-based sizing does not force coverage of both dimensions.
When to specify both dimensions
Two values set width and height, in that order. For example, background-size: 480px 180px requests a rendered width of 480 pixels and height of 180 pixels. Use both when those dimensions are intentional; if the requested ratio differs from the source image’s ratio, the image can appear stretched or compressed. For responsive designs, fixed pixel dimensions may not be suitable at every element width.
Understand percentages and the positioning area
Percentage values are calculated relative to the background positioning area, not automatically relative to the image’s original pixel dimensions. That area is based on background-origin; its default is the padding box. As a result, changing the element’s padding or background origin can affect the area used to calculate percentage sizing.
With background-size: 100% auto, the width is matched to that area and the height follows the image’s intrinsic ratio when available. With background-size: 50% 50%, both dimensions are specified as percentages of the area, so the image may not preserve the source ratio. If the goal is proportional scaling, use one dimension with auto rather than assigning two independent percentages.
Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minutePC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Use positioning and shorthand correctly
background-position determines where the image sits in the element. With cover, it is especially useful for keeping a face, product, or other focal point visible inside the crop. Try center, top, or a two-value position such as 70% 40%, then inspect the actual crop at the relevant element sizes. Percentage positioning depends on the difference between the positioning area and the rendered image size.
You can combine the image, color, position, size, and repeat behavior with the background shorthand. When size appears in the shorthand, put it after the position and separate the two with a slash:
Rank #4
.hero {
background: #243044 url("hero.jpg") center / cover no-repeat;
}
The longhand declarations are often easier to debug when a rule has several background settings. If you use the shorthand, remember that it can also reset other background properties; inspect the complete rule if an existing color, image, or repeat setting seems to disappear.
Check the result in the browser
- Give the element a visible size. A background cannot be judged on an element with no visible area.
- Set
background-imageand choose the size value based on whether fill, full visibility, or exact dimensions matter most. - Set
background-positionto place the focal area, and setbackground-repeat: no-repeatif a single image is intended. - Inspect the element in the shapes and sizes where it will actually appear. In particular, compare wide and narrow layouts when using
cover. - If the result is wrong, check the loaded image, the element’s dimensions, the positioning area, and any other background declarations before changing the size value.
Or skip the browser setup
If you want a screenshot of the page after changing its CSS, ScreenshotNeo can capture a URL with one GET request. For example, save this as shot.sh and run it in a shell, replacing the URL with a page you can access and using your API key:
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp
See the ScreenshotNeo API documentation for request options. ScreenshotNeo accepts cookie or consent banners like a visitor and removes more than 60 known consent platforms, newsletter popups, and chat widgets before capture; each cleanup step can be turned off. Bot checks or CAPTCHAs, blank pages, timeouts, failed loads, and cache hits cost nothing, and the response identifies the page verdict and billing status in headers. It also provides an MCP server with screenshot, page-info, and PDF-capture tools for AI agents. The free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000 shots. See ScreenshotNeo and sign up for 1,000 free screenshots a month with no card.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Troubleshoot common background-size problems
The subject is cut off
This is expected with cover when the image and element have different aspect ratios. Adjust background-position to move the visible area toward the subject, change the element’s ratio if the design allows, or use a source image composed for that crop. Switching to contain keeps the whole image but may leave unused space.
Best Value
There are blank bands around the image
This is expected with contain when the element and image ratios do not match. Decide whether full visibility or edge-to-edge coverage matters more: keep contain if all image content must show, or try cover if cropping is acceptable. A background color can make uncovered space deliberate rather than transparent.
The image appears more than once
Check background-repeat. A background may tile, including when a contain-sized image is smaller than the area. Set background-repeat: no-repeat for a single image.
The Tool Desk
Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →The background is invisible
Confirm that the element has a visible area and that the image URL loads. Then check its position and any overlapping or overriding background declarations. A background color can help reveal the element’s area or show through transparent image pixels.
The image looks stretched or compressed
Check whether both width and height have been set. If they force a ratio unlike the source image’s ratio, the image can appear distorted. Use cover, contain, or one explicit dimension plus auto when preserving proportions is more important than matching two exact dimensions.
Browser support
background-size is widely available in contemporary browsers. MDN lists its availability across browsers since July 2015. If a project must support unusually old browsers, check compatibility for the specific target browsers rather than assuming current support applies to them.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

