The short version
- The CSS background property can hold several comma separated layers, and the first layer listed is painted on top.
- A background colour can only be set once, in the last layer, and it shows beneath every image and gradient.
- In the background shorthand, background-size must follow background-position with a slash, as in center / cover.
- Placing a gradient layer above an image layer in one declaration is the simplest way to tint a photo for legible text.
- Images set in CSS are discovered later than img elements, so an important hero image may need a preload hint.
On this page
A CSS background is a stack of layers on one element. The background property accepts a comma separated list of images and gradients, painted with the first one on top, plus a single colour at the very bottom. That is all you need for a tinted hero photo, a soft light over a gradient or a texture over a brand colour, without extra elements or wrappers. The rest is knowing how the shorthand reads its values.
The background shorthand, value by value
The shorthand sets up to eight longhand properties at once. Any you leave out are reset to their defaults, which is the most common source of "why did my background size disappear" bugs.
background-colorWhat it controls
Common values
#0f0b1e, oklch(20% 0.05 290)background-imageWhat it controls
Common values
url(photo.webp), linear-gradient(…)background-positionWhat it controls
Common values
center, top right, 50% 30%background-sizeWhat it controls
Common values
cover, contain, 200px autobackground-repeatWhat it controls
Common values
no-repeat, repeat, repeat-xbackground-attachmentWhat it controls
Common values
scroll, fixed, localbackground-origin and background-clipWhat it controls
Common values
padding-box, border-box, text.hero {
/* image | position / size | repeat | colour */
background: url("harbour.webp") center 30% / cover no-repeat #0f172a;
}How background layers stack
List layers from top to bottom, separated by commas. Each layer can have its own position, size and repeat, given in the same order in each longhand. Only the final layer may include a colour. Think of it as reading down through a pile of tracing paper.
.section {
background:
radial-gradient(circle at 80% 20%, #f472b6 0%, transparent 45%),
radial-gradient(circle at 15% 85%, #38bdf8 0%, transparent 50%),
linear-gradient(160deg, #1e1b4b 0%, #0f0b1e 100%)
#0f0b1e;
}Use transparent in the outer stops so lights blend into whatever is below. For the shapes themselves, see CSS radial gradient and CSS mesh gradient.
A gradient over a background image
The most useful layered background tints a photo so words on top can be read. Put a gradient layer first and the image second. A gradient that is dark where the text sits and clear elsewhere keeps far more of the photo than a flat dark overlay.
.hero {
background:
linear-gradient(90deg, rgb(10 12 24 / 0.85) 0%, rgb(10 12 24 / 0.4) 45%, transparent 70%),
url("terrace.webp") center / cover no-repeat
#0a0c18;
color: #f8fafc;
}A flat overlay makes text readable by dimming everything equally.
A directional layer darkens only where the words are and leaves the rest of the scene bright.
Gradient overlay has more overlay recipes, and text over image covers when to move the words instead of darkening the picture.
background-size, position and repeat
coverfills the box and crops whatever overflows. Right for photos and full bleed heroes.containfits the whole image inside and leaves gaps. Right for logos and illustrations that must not be cropped.- Pair
coverwith a deliberatebackground-positionsuch as50% 30%so the crop keeps the subject's face or the product in view. - Texture tiles want
repeatand a fixed size, for example200px 200px, so the grain stays the same at every screen width. background-attachment: fixedcreates a parallax look on desktop, but many mobile browsers ignore it, so never depend on it.
.card {
background:
url("grain.png") 0 0 / 200px 200px repeat,
linear-gradient(135deg, #7c2d12 0%, #ea580c 55%, #fdba74 100%);
}One more longhand earns its place: background-clip: text cuts the background to the shape of the letters, which is how gradient lettering is made in CSS. Keep it for large display words, set a solid color fallback, and see CSS gradient text for the full pattern.
Mistakes to avoid
Avoid
- Setting
background-sizeand then the shorthand after it, which resets it - A colour in any layer except the last
- No fallback colour, so text is unreadable while the image loads
- A huge photo for a box that is 400 pixels wide
- Important content only in a background image, invisible to screen readers
Do
- Write the shorthand first, then any longhand overrides
- One colour, at the end of the list
- A fallback colour close to the image's average tone
- Images sized to the box, served as WebP or AVIF
- Meaningful images as
imgwith alt text; decoration as backgrounds
Responsive and fast background images
image-set() lets the browser choose between formats or resolutions, much like srcset on an img. And because background images are only discovered once the CSS is parsed, a hero background can arrive late; a preload hint in the head fixes that.
<link rel="preload" as="image" href="/hero-1920.avif" type="image/avif" fetchpriority="high">
<style>
.hero {
background: image-set(
url("/hero-1920.avif") type("image/avif"),
url("/hero-1920.webp") type("image/webp"),
url("/hero-1920.jpg") type("image/jpeg")
) center / cover no-repeat #0f172a;
}
</style>Often the fastest background is no image at all. Layered gradients cost no download, and a tiny grain tile adds texture for a few kilobytes. CSS gradient performance and WebP vs PNG and JPG cover the trade offs. If you would rather not hand tune light and grain, a Gradiently Mark gives you a finished, distinctive background at the 1920×1080 website hero size, with the same look as your social posts.
Questions people ask
How do I add a background image in CSS?
Use background: url("image.webp") center / cover no-repeat; on the element, with a fallback colour at the end in case the image is slow to load.
How do I put a gradient over a background image?
List the gradient first and the image second in one background declaration, separated by a comma. The first layer is painted on top.
Can I use multiple backgrounds in CSS?
Yes. Separate layers with commas; each can have its own position, size and repeat, and only the last layer may include a colour.
What is the difference between background-size cover and contain?
cover fills the whole element and crops the overflow; contain shows the whole image and can leave empty space around it.
Why does my background-size stop working?
The background shorthand resets every longhand it doesn't mention. Set background-size after the shorthand, or include it in the shorthand after the position with a slash.
Written by Gradiently
The team behind Gradiently, a design tool built around Marks: living gradients that make everything you design look like yours.
See our profile