Explain it simply
Explain it simply
Think of an element's background as a wall. First you paint the wall one color (background-color). Then you paste a photo or a blended gradient on top of it (background-image). How big the photo is, where it is aligned and whether it is pasted again and again like wallpaper is decided by background-size, background-position and background-repeat.
The key point is that a background is decoration only. Screen readers do not read background images, and if the image fails to load you only see the paint underneath. So always pick that paint color so your text stays readable on it.
Problem → Why → Solution
The problem: text that vanishes on a photo
The hero below looks great on the designer's laptop, because the photo is dark and the white text pops. But on a slow network the page is plain white for a few seconds before the photo arrives, and the white heading is completely invisible. The day someone swaps in a brighter photo, nobody can read the heading on a white sky either. And on a narrow phone screen, cover trims both sides of the image, so half the faces in the team photo disappear.
.hero {
color: white;
/* no fallback color, no overlay, crop never checked */
background: url("team-photo.jpg") center / cover;
}Why does this happen?
A background image only appears after it downloads, and CSS knows nothing about how light or dark the picture is. Because the shorthand names no color, background-color becomes transparent and the white page shows through. The fix is a fallback color, a dark overlay layer between the photo and the text, and a check of the crop at several screen widths.
Core concept: a background is a stack of layers
The browser paints an element's background from the bottom up. At the very bottom sits a single background-color, which reaches under the border. On top of it come the images listed in background-image, painted from the last one to the first, so the image you write first is the layer closest to the viewer. With background-image: linear-gradient(...), url("photo.jpg"), the gradient sits on top of the photo.
Gradients such as linear-gradient and radial-gradient are images the browser draws itself, which is why they go in background-image, not in background-color. They need no download, so they appear instantly and stay smooth at any size. linear-gradient(135deg, A, B) starts as color A near the top-left corner and blends into B toward the bottom-right.
Each image layer is controlled by three properties. background-repeat defaults to repeat, so a small image tiles across the box. background-position defaults to the top-left corner. background-size: cover scales the image, keeping its proportions, until it covers the whole box, so when the box and image have different shapes the edges get cropped. contain shrinks the image until all of it fits, so nothing is cropped but empty bands can remain. background-position then decides which part survives the crop.
Finally, a background is not content. It has no alt text, screen readers skip it, and most browsers leave it out when printing by default. If an image carries meaning, such as a product photo or a chart, use <img> with alt text instead.
| Property | What it controls | Default |
|---|---|---|
| background-color | Paint under every layer | transparent |
| background-image | List of layers, first = top | none |
| background-size | Layer size (cover, contain, 200px auto) | auto |
| background-position | Where the layer is anchored | 0% 0% (top-left) |
| background-repeat | Whether the layer tiles | repeat |
- Paint background-color at the bottom
- Draw the last listed image layer
- Stack earlier layers on top of it
- Render the text above the first layer
If one image fails, only that layer disappears; the color below and any overlay above still paint.
General pattern
selector {
background-color: fallback-color;
background-image: top-layer, bottom-layer;
background-repeat: repeat | no-repeat;
background-position: x y;
background-size: cover | contain | width height;
}background-color is the fallback and background-image lists the layers separated by commas. repeat, position and size accept comma-separated values per layer too; a single value applies to every layer.
Example: a gradient hero section
This example draws a landing-page hero with nothing but a gradient. Its background-color matches the gradient's first color, so even if the gradient were not painted the section would look almost the same. The repeat, position and size lines are there for the day a photo replaces the gradient.
.hero {
min-height: 420px;
background-color: #101827;
background-image: linear-gradient(135deg, #101827, #123b52);
background-repeat: no-repeat;
background-position: center;
background-size: cover;
}Code breakdown
- Line 2
min-height: 420px;A background only paints inside the element's box. An empty div is 0px tall, so without min-height you would see no background at all.
- Line 3
background-color: #101827;The bottom layer. It matches the gradient's first color, so white text stays readable even if the image layer never paints.
- Line 4
background-image: linear-gradient(135deg, #101827, #123b52);An image the browser draws itself. 135deg makes it run from dark navy at the top-left to blue-teal at the bottom-right.
- Lines 5-7
background-size: cover;A gradient has no size of its own and already fills the box, so these three lines change nothing visible yet. Once a url() photo is added, they stop it tiling, center it and make it cover the box.
A 420px-tall hero filled with a diagonal gradient that shifts from dark navy (#101827) at the top-left to blue-teal (#123b52) at the bottom-right, with no tiling seams.Overlay layers and the shorthand
The most common way to make text on a photo readable is to add a see-through dark layer on top of the photo. Giving linear-gradient the same color twice produces a flat single-color layer, and the text keeps its contrast however bright the photo gets underneath.
.hero {
background-color: #1e293b; /* fallback */
background-image:
linear-gradient(rgb(0 0 0 / 0.5), rgb(0 0 0 / 0.5)), /* overlay, on top */
url("hero.webp"); /* photo, below */
background-size: cover;
background-position: center top;
}
/* The same thing as one shorthand: the color goes in the LAST layer only */
.hero-short {
background:
linear-gradient(rgb(0 0 0 / 0.5), rgb(0 0 0 / 0.5)),
url("hero.webp") center top / cover no-repeat,
#1e293b;
}Two things to watch with the background shorthand. Position and size are separated by a slash, as in center top / cover, and a color is only allowed in the last layer. More importantly, the shorthand resets every sub-property you leave out back to its default. So write the shorthand first and override single properties with longhands after it.
In real projects
Where this shows up in real projects
Marketing-site hero banners, blog-post covers and login-page backdrops all use the photo plus overlay plus fallback color pattern. Dashboards and card UIs separate sections with pale background colors far more often than with photos, and save gradients for small buttons and badges. Design systems usually name these colors as CSS variables such as --surface and --surface-muted.
Performance matters too. The browser only starts downloading a CSS background image after it has parsed the stylesheet and matched the element, so it usually appears later than an <img> in the HTML. For a hero image, often the largest image on the page, use a compressed format such as WebP and keep the file small; when it is critical, consider an <img> or a preload hint. Before release, blocking images in DevTools and checking that the text is readable on the fallback color is a five-minute test.
Common mistakes
Common mistakes
1. The shorthand wiping out the fallback color
Write .card { background-color: #fef3c7; } and later .card.promo { background: url(stars.svg) no-repeat; } and the promo card's background-color becomes transparent, because the shorthand resets the color it did not mention. Use the background-image longhand, or repeat the color inside the shorthand.
2. Putting content inside a background image
Baking text like "50% off" into a photo and using it as a background means screen readers skip it, it cannot be translated, it blurs when zoomed and nobody can copy it. Keep text as HTML text, and use <img alt="..."> for images that carry meaning.
3. Never checking what cover crops
A banner that is wide on desktop becomes a tall box on a phone, so cover crops both sides of the picture. Find where the important part (a face, the product) is, anchor it with a value such as background-position: center top, then check the page at 360px wide.
Best practices
Best practices
- Give every background image a fallback background-color that contrasts with the text.
- When text sits on a photo, add an overlay layer and check contrast over the brightest part of the image.
- Use CSS backgrounds for decoration only; use <img alt> for images that carry meaning.
Mini challenge
Mini challenge: a banner with an overlay
Build a photo banner for a travel website. The white heading must be clearly readable now, while the photo file does not exist yet, and later when it does.
- A dark fallback background-color (#1e293b) that white text is readable on
- Two layers in background-image: a 55% black overlay on top, url("mountains.webp") below
- The photo must not tile, must cover the banner and stay centered
- Use longhand properties only, no background shorthand
<!doctype html>
<html lang="en">
<style>
.banner {
/* TODO: generous padding and centered white text */
/* TODO: a dark fallback color for when the photo is missing */
/* TODO: two layers - a translucent black overlay FIRST,
then url("mountains.webp") */
/* TODO: no tiling, centered, covering the whole banner */
}
</style>
<section class="banner">
<h1>Trekking in Shan State</h1>
<p>Three-day routes from Kalaw to Inle Lake</p>
</section>
</html>Expected: With mountains.webp missing, you see a centered white heading and tagline on dark slate (#1e293b) made even darker by the overlay. Once the photo exists it covers the banner, dimmed by the 55% overlay, and the text stays readable.
Show hint
For a flat one-color layer, give linear-gradient the same color twice: linear-gradient(rgb(0 0 0 / 0.55), rgb(0 0 0 / 0.55)). The layer you list first is painted on top.
Show solution
<!doctype html>
<html lang="en">
<style>
.banner {
padding: 80px 24px;
text-align: center;
color: #ffffff;
background-color: #1e293b;
background-image:
linear-gradient(rgb(0 0 0 / 0.55), rgb(0 0 0 / 0.55)),
url("mountains.webp");
background-repeat: no-repeat;
background-position: center;
background-size: cover;
}
</style>
<section class="banner">
<h1>Trekking in Shan State</h1>
<p>Three-day routes from Kalaw to Inle Lake</p>
</section>
</html>Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- Backgrounds paint bottom-up: the color is lowest, and the first layer in background-image is on top.
- cover fills the box and crops the edges, contain shows the whole image and may leave gaps, and background-position steers the crop.
- Text on a photo always needs a fallback color and an overlay layer.
- The background shorthand resets sub-properties you leave out, so override with longhands.
CSS reference — MDN Web Docs