The CSS background property: how it works, with examples
background paints everything behind an element's content: a color, one or more images, and how those images are sized, placed and repeated. This guide covers the shorthand, every longhand property, layering, and the three ways to fill a background (photos, SVG and pure CSS gradients) so you can pick the right one.
The background shorthand
background is a shorthand: one declaration sets up to eight separate properties. The two rules below do exactly the same thing.
.hero {
background: #f8eff8 url("pattern.svg") center / 40px 40px repeat;
}
/* is the same as */
.hero {
background-color: #f8eff8;
background-image: url("pattern.svg");
background-position: center;
background-size: 40px 40px;
background-repeat: repeat;
}Three things trip people up:
- Size needs a slash.
background-sizecan only appear right after the position, separated by/:center / cover, never justcoveron its own. - The shorthand resets everything it doesn't mention. Writing
background: url(a.png)afterbackground-color: navysets the color back to transparent. Put the shorthand first and longhands after it. - The color goes on the last layer only. With several layers, only the final one can include a color, because the color always sits underneath every image.
The background properties
| Property | What it does | Common values |
|---|---|---|
background-color | Solid color under all images | #fff, transparent, var(--bg) |
background-image | One or more images or gradients, as layers | url(), linear-gradient(), radial-gradient(), conic-gradient() |
background-size | How big each image is drawn | cover, contain, 40px 40px, auto |
background-position | Where each image starts | center, top right, 50% 20% |
background-repeat | Whether images tile | repeat, no-repeat, repeat-x, space, round |
background-attachment | Whether it scrolls with the page | scroll, fixed, local |
background-origin | Which box positioning starts from | padding-box, border-box, content-box |
background-clip | Which box the background is cut to | border-box, padding-box, text |
background-blend-mode | How layers mix with each other | multiply, screen, overlay |
background-blend-mode isn't part of the shorthand, so set it separately. Each property takes a comma-separated list, one value per image layer.
Layering multiple backgrounds
background-image accepts a list. The first layer is drawn on top, and the color sits under all of them. Here a translucent gradient tints a dot pattern so text stays readable:
Readable text on a busy pattern
.card {
background:
linear-gradient(rgb(34 0 34 / 0.55), rgb(34 0 34 / 0.55)), /* top: tint */
radial-gradient(#f09f33 20%, transparent 21%) 0 0 / 24px 24px, /* middle: dots */
#880088; /* bottom: color */
}A one-color linear-gradient() like the top layer here is the standard way to put a tint over any background, including photos.
Photos, SVG or pure CSS?
Every background image comes from one of three places. Each one is good at different things, and many sites use all three.
1. Photos (JPG, PNG, WebP, AVIF)
Use a photo when the background should show something real: a place, a product, people. Free stock sites such as Pexels are a good source. Photos are the heaviest option, so a few habits matter:
- Export AVIF or WebP around 1600–2000px wide, and serve them with
image-set()plus a JPG fallback. - Use
center / cover no-repeatso the photo fills the element without stretching. - Add a dark or light gradient layer on top so text has enough contrast.
- For the biggest image above the fold, consider an
<img>withobject-fit: coverinstead. Browsers find it sooner than a CSS background, which helps load time.
.hero {
background:
linear-gradient(rgb(0 0 0 / 0.45), rgb(0 0 0 / 0.45)),
image-set(url("hero.avif") type("image/avif"), url("hero.jpg") type("image/jpeg"))
center / cover no-repeat;
}2. SVG
SVG suits patterns, illustrations, waves and shapes: anything drawn rather than photographed. Files are small, stay sharp at any size, and can be recolored by editing the file. They can also do what gradients can't, such as organic curves, textures and hand-drawn shapes.
.section {
background-color: #f8eff8;
background-image: url("wave.svg");
background-size: 40px 20px;
}If you'd rather not draw your own, SVGBackgrounds.com has a library of customizable SVG backgrounds, patterns and shape dividers that you can recolor and export as CSS. It's our sister site.
3. Pure CSS gradients
Gradients are images that the browser draws itself. Stacked and repeated, they make dots, stripes, grids, checks and many geometric patterns with no file at all. That means no extra download, nothing to host, and colors you can change with a custom property. Every pattern onCSSBackgrounds works this way.
.section {
--bg: #ffffff;
--fg: #880088;
--size: 24px;
background-color: var(--bg);
background-image: radial-gradient(circle closest-side, var(--fg) 40%, transparent calc(40% + 1px));
background-size: var(--size) var(--size);
}Side by side
| Photo | SVG | Pure CSS | |
|---|---|---|---|
| Typical weight | 50–500 KB | 1–20 KB | Under 1 KB |
| Extra request | Yes | Yes (none if inlined) | No |
| Sharp at any size | No | Yes | Yes |
| Recolor | Edit the image | Edit the file | Change a variable |
| Best for | Real scenes, products, people | Illustrations, organic shapes, textures | Geometric patterns, gradients, UI surfaces |
Design tips
- Keep it quiet behind text. Lower the contrast between pattern colors, or put a solid panel behind the text. Check contrast on the busiest part of the background.
- Scale the pattern to the element. A 24px dot works on a card. A full-width hero usually needs larger, sparser shapes.
- Use custom properties for color. Patterns built on
--bgand--fgswitch to dark mode by redefining two variables. - Be careful with
background-attachment: fixed. It's ignored on iOS Safari and can make scrolling janky. Test it on a phone. - Respect reduced motion. If a background animates, stop it inside
@media (prefers-reduced-motion: reduce).
Common problems
The background doesn't show up
The element probably has no height. Backgrounds only paint the element's box, so an empty div shows nothing.
/* An empty element has no height, so its background never shows. */
.hero {
min-height: 60vh;
}The image path is wrong
A url() inside a CSS file is relative to the CSS file, not the HTML page. Check the path from the stylesheet's folder.
Lines or gaps where a pattern repeats
Tile sizes that land on half pixels can leave thin seams. Use whole-pixel sizes (even numbers when a layer is offset by half a tile).
cover crops the important part
cover fills the box and crops the overflow. Move the focus point with background-position, for example 50% 30%.