To add a background image, put the content in an HTML element and set that element’s background-image in CSS. For a hero that fills its box without stretching the picture, pair background-size: cover with background-position: center and background-repeat: no-repeat. Give the element visible height or content, and use an HTML <img> instead when the image itself conveys information.
Basic example: add an image behind an element
The HTML creates the section and its foreground content; CSS paints the image behind that content. This example gives the section a visible minimum height, centers the image, and prevents it from tiling:
<section class="hero">
<h1>Welcome</h1>
</section>
.hero {
min-height: 24rem;
background-image: url("images/hero.jpg");
background-color: #333;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
Replace images/hero.jpg with the path or URL for your image. The path is resolved relative to the CSS file when it appears in an external stylesheet. For example, if the stylesheet is at css/site.css and the image is at images/hero.jpg from the site root, the CSS path is commonly ../images/hero.jpg. If CSS is in a <style> element or a style attribute, the relative path is instead resolved from the document URL. An absolute URL can avoid ambiguity when the image is hosted elsewhere.
The element needs a visible box for its background to paint into. A section containing a heading may already have height; otherwise, give it padding, a height, or a minimum height. A background on a small content element paints only within that element’s box.
Do these 3 things before closing this tab:
1Clear out junk files and repair common Windows errors2Scan for outdated or missing drivers - takes under a minute3Repair Windows errors before they cause bigger problems#1 Best Overall
- HTML CSS Design and Build Web Sites
- Comes with secure packaging
- It can be a gift option
Shorter equivalent using the background shorthand
The same main settings can be written in one declaration:
.hero {
min-height: 24rem;
background: url("images/hero.jpg") center / cover no-repeat #333;
}
In this shorthand, the slash separates background-position from background-size; the color is placed after the image settings. Separate longhand properties are often easier to adjust while learning or when you need to set different values for multiple layers.
Choose how the image fits the box
The image’s proportions and the element’s proportions determine whether an image fills the box, leaves empty space, or gets cropped. Choose the behavior according to what matters most in the design:
| Setting | What it does | Best when | Trade-off |
|---|---|---|---|
cover |
Scales the image, preserving its proportions, until the box is completely covered. | The section must be filled, such as a hero or banner. | Parts of the image can be cropped when its aspect ratio differs from the box. |
contain |
Scales the whole image, preserving its proportions, until it fits inside the box. | The complete image must remain visible. | Unfilled space can remain; set background-repeat: no-repeat if tiling is unwanted. |
| Explicit size | Sets the background’s dimensions, for example background-size: 400px 200px. |
A deliberate fixed size is needed. | Independently chosen width and height can distort the source image. |
For most photographic hero backgrounds, cover is a useful starting point because it fills the region without distorting the source. It does not guarantee that every part of the picture remains visible. On a narrow phone-sized box, the sides of a wide image may be cropped; on a short, wide box, the top and bottom may be cropped.
Keep the important subject in view
Change background-position to control which part of the image is favored within the crop. Common values include center, top, and percentage positions. For example:
.hero {
background-image: url("images/hero.jpg");
background-position: 65% center;
background-repeat: no-repeat;
background-size: cover;
}
Here the horizontal position is shifted toward 65% of the image rather than its midpoint. The right value depends on the image and layout, so inspect the crop at both narrow and wide viewport sizes. If the important subject changes position, a single background position may not suit every layout; use a different position at a breakpoint when needed.
Prevent unwanted tiling and choose a fallback
Background images repeat by default. That can be useful for small texture patterns, but often looks wrong with a photograph. Set background-repeat: no-repeat when you want one image. Include a fallback color so the region still has a defined appearance if the image fails to load, and so any space around a contain image is not left to an unexpected color:
.hero {
background-color: #333;
background-image: url("images/hero.jpg");
background-repeat: no-repeat;
background-size: cover;
}
Set a background image for the whole page
For a page-wide background, apply the image to body rather than to a small content element. A typical version is:
Free tools Windows power users keep installed
One-click scans. No signup required.
Rank #3
body {
min-height: 100vh;
background-color: #222;
background-image: url("images/page-background.jpg");
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
The min-height gives the body at least the height of the viewport while still allowing it to grow with the page content. Under normal conditions where the root element’s background is transparent, the body’s background can be propagated to the root element. That behavior is why a body background commonly appears across the viewport. A background on an individual section, by contrast, remains confined to the section’s box.
Place a dark overlay over the image for readable text
If foreground text sits over a photograph, image brightness and detail can make the text hard to read. CSS supports multiple comma-separated background layers. The first layer is painted on top, so put a translucent gradient before the photograph:
.hero {
min-height: 24rem;
background-image:
linear-gradient(rgb(0 0 0 / 45%), rgb(0 0 0 / 45%)),
url("images/hero.jpg");
background-position: center, center;
background-repeat: no-repeat, no-repeat;
background-size: cover, cover;
}
The first position, repeat, and size values apply to the gradient; the second set applies to the photo. When a property has fewer comma-separated values than there are layers, its value list repeats. That can make compact CSS possible, but writing each value explicitly can make a layered background easier to understand. Check the result over the actual image crop and text, rather than assuming the overlay makes every text color readable.
For a stronger fade in one direction, use a directional gradient, for example linear-gradient(90deg, rgb(0 0 0 / 65%), transparent). The gradient remains a visual layer; it does not change the image file or its crop.
Recommended Free Tools
Rank #4
Use a CSS background or an HTML image?
Choose based on the image’s purpose, not just on which CSS rule is shorter. A background is appropriate for decorative imagery behind a section. It is not exposed as image content to assistive technology: MDN notes, “Browsers do not provide any special information about background images to assistive technology.” See the MDN background-image reference.
If the image carries information a reader needs—such as a product photo, diagram, or meaningful illustration—put it in the document as an HTML <img> with an appropriate text alternative. A CSS background alone does not provide an equivalent text alternative. If the image is decorative, keep essential information in the surrounding text and treat the background as visual presentation.
Check contrast over every relevant crop
Text contrast should be checked at the sizes and crop positions where the page will actually be used. MDN’s guidance references WCAG contrast thresholds of 4.5:1 for ordinary text and 3:1 for large text; its cited definition of large text is at least 24px, or bold text at least 18.66px. These are cited thresholds, not measurements of the example above. A fallback color and a suitably tuned gradient can help, but neither removes the need to assess the foreground against the final image and layout.
Common problems and fixes
- The image does not appear: Check the spelling, capitalization, file extension, and path. For a relative path in an external stylesheet, resolve it from the stylesheet’s location, not automatically from the HTML file. Open the image URL directly if it is hosted remotely.
- The background area is missing or tiny: Confirm that the target element exists and has content, padding, a height, or a minimum height. Backgrounds do not create layout height by themselves.
- The image repeats: Set
background-repeat: no-repeat. Repetition is the default, and can be intentional for patterns. - The image looks cropped: This is expected with
coverwhen the image and box have different aspect ratios. Trycontainif all of the image must show, or adjustbackground-positionto bring the subject into view. - There is an empty band or uncovered area: If using
contain, that is expected when the image’s proportions do not match the box. Set a background color for the remaining space, or choosecoverif filling the box matters more than showing the entire image. - The image is stretched: Avoid assigning independent explicit width and height values unless distortion is intentional. Use
coverorcontainto preserve the image’s proportions. - Text is difficult to read: Adjust the crop, add or strengthen a gradient layer, and verify contrast at each relevant viewport. Do not depend on a photograph being uniformly light or dark.
- Important image meaning is missing to screen-reader users: If the image communicates information, use an HTML image with suitable alternative text instead of relying on a CSS background.
Or skip the browser setup
If you want to inspect how the finished page renders, ScreenshotNeo can return a screenshot or PDF from a single GET request. It does not add a CSS background for you; use the HTML and CSS above to build the page, then request a capture of its URL. For example, save this as capture.sh after replacing the URL with the page you want to inspect. See the ScreenshotNeo API docs for request options.
Windows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallOutdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchBest Value
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp
ScreenshotNeo removes cookie banners, newsletter popups, and chat widgets before capture; each of those steps can be turned off. Bot checks or CAPTCHAs, blank pages, timeouts, failed loads, and cache hits are not billed, and the response indicates the page verdict and billing status. Its MCP server offers take_screenshot, get_page_info, and capture_pdf for Claude, Cursor, and other MCP clients. The Free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000 screenshots. Sign up for 1,000 free screenshots a month with no card.
Frequently Asked Questions
Can I put the background image directly in HTML?
The image itself is set with CSS, usually in a stylesheet or a style block. HTML supplies the element that CSS styles.
Will a CSS background image appear when someone prints the page?
Print output can depend on browser and print settings. If the picture is essential to the printed document, include it as document content and check the print layout rather than relying only on a background.
Is there a single background setting supported in every browser?
MDN describes the background-image property as widely available and established, but a specific browser and version should be checked against the compatibility information linked from its reference.
The Tool Desk
Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →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.

