Full answer
A CSS background paints behind the content of a box. The image comes from background-image, which accepts a url() or a gradient such as linear-gradient().
The main controls are:
background-size:auto(natural size), a length or percentage,cover(scale until the box is covered, cropping if needed) orcontain(scale until the whole image fits, leaving gaps).background-position: where the image sits, such ascenter,top leftor20% 80%.background-repeat:repeatis the default. Useno-repeatfor a single image.background-attachment:scrollby default.fixedkeeps the image still while the page scrolls, but it can behave differently on touch devices, so test it.
You can give several backgrounds at once, separated by commas. The first one is on top. A common use is a dark gradient over a photo, so white text stays readable. The gradient goes first, because it is the top layer.
Use a background for decoration. If the picture carries meaning, use an img with alt, because backgrounds are invisible to screen readers.
The hero has two background layers: a 55 percent black gradient on top and the picture below, so the heading stays readable whatever the picture looks like. cover scales the picture to fill the box. The second box is a pattern made only from a gradient, with no image file at all.
For photos that carry content, such as a product picture, use img and object-fit so the image stays in the accessibility tree.
<style>
.hero {
min-height: 160px;
padding: 24px;
color: #fff;
background:
linear-gradient(rgba(0, 0, 0, 0.55), rgba(0, 0, 0, 0.55)),
url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='600' height='300'%3E%3Crect width='600' height='300' fill='%2399ccff'/%3E%3C/svg%3E");
background-size: cover;
background-position: center;
background-repeat: no-repeat;
}
.stripes {
height: 40px;
background: repeating-linear-gradient(45deg, #0a6cff 0 10px, #ffffff 10px 20px);
}
</style>
<div class="hero" id="hero"><h1>Admissions open</h1></div>
<div class="stripes" id="stripes"></div>Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- background-image accepts url() and gradients.
- cover fills the box and crops; contain fits the whole image.
- Multiple backgrounds are comma-separated; the first is on top.
- A dark gradient over a photo keeps text readable.
- Backgrounds are decorative; use img with alt for meaningful pictures.
Common mistakes
- Using a background image for content that needs alt text, so screen reader users miss it.
- Putting the gradient after the image in the list, so the image covers it and the overlay never shows.
- Relying on background-attachment: fixed for a parallax effect without testing on phones, where it can behave differently.
Follow-ups
- What is the difference between background-size: cover and contain?
- How would you add a second background image behind the first?
Tips
- State which layer is on top: the first one in the list.
- Mention the gradient overlay trick for text on photos. It is a very common design need.