Multiple Backgrounds and the background Shorthand
Stacking several background layers on one element, how comma-separated lists line up and repeat, the background shorthand's syntax and the values it resets, and practical layered patterns: image overlays for readable text, icons, scroll shadows and patterns.
kvi5hnuCode Sprout team member7 min readOn this page
An element can have several background images, stacked as layers, each with its own position, size and repeat. That replaces extra wrapper elements for many effects: a dark overlay on a photo, an icon plus a texture, or shadows that appear only while a list can scroll. This article covers how the layers work, then the background shorthand that sets them all at once.
Layers
Give background-image a comma-separated list. The first image is on top, and each later one is painted below it. The background color is always painted under all of them:
.hero {
background-image:
linear-gradient(rgb(0 0 0 / 0.5), rgb(0 0 0 / 0.1)), /* top layer: a dark overlay */
url("images/harbor.jpg"); /* below it: the photo */
background-color: #2b2118; /* under everything: the fallback */
}
That's the reverse of the HTML source order, where later elements paint on top, so it's worth remembering: first listed, frontmost.

The dark gradient, listed first, ends up over both other layers, which is why the white text at the bottom stays readable.
Matching the lists
The other background properties also take comma-separated lists, and the values are matched to the images by position: the first background-size goes with the first image, and so on.
.badge {
background-image: url("icons/star.svg"), url("textures/paper.png");
background-position: right 0.5rem top 0.5rem, 0 0;
background-size: 1.25rem, auto;
background-repeat: no-repeat, repeat;
}
If a list is shorter than the image list, it's repeated to fill: with three images and background-size: 10px, 20px, the third image gets 10px again. If it's longer, the extra values are ignored. So one value, such as background-repeat: no-repeat, applies to every layer.
background-color isn't a list: there's one color per element, under all the layers.
The background shorthand
background sets every background property in one declaration. One layer looks like this:
.card {
background: url("icons/leaf.svg") right 1rem top 1rem / 2rem no-repeat padding-box #f4efe6;
}
| Part | Property | Rule |
|---|---|---|
url(...) or a gradient | background-image | |
right 1rem top 1rem | background-position | |
/ 2rem | background-size | only directly after a position, separated by a slash |
no-repeat | background-repeat | |
padding-box | background-origin and background-clip | one box keyword sets both; two set origin, then clip |
scroll, fixed or local | background-attachment | |
#f4efe6 | background-color | only in the last layer |
The parts can come in any order, with two restrictions: the size must follow the position after a /, and box keywords are read as origin then clip. A size alone, without a position, is invalid: write center / cover, or 0 0 / cover.
Layers are separated by commas, and the color goes in the last one:
.hero {
background:
linear-gradient(rgb(0 0 0 / 0.55), rgb(0 0 0 / 0.15)),
url("images/harbor.jpg") center / cover no-repeat,
#2b2118;
}
A color in any layer but the last makes the whole declaration invalid, so it's ignored and nothing is set.
The shorthand resets everything
Like every shorthand, background sets each property you leave out to its initial value: none for the image, 0% 0% position, auto size, repeat, padding-box origin, border-box clip, scroll, and transparent color. That catches people out:
.button { background-color: #2f7d4f; }
.button.icon { background: url("icons/arrow.svg") no-repeat; } /* the green is reset to transparent */
Use the longhand, background-image, when you only want to change one part. It's also why background: none is a short way to clear every background property.
Practical layers
body { font: 0.95rem/1.4 system-ui, sans-serif; margin: 1rem; display: grid; gap: 0.75rem; }
.hero {
color: white;
padding: 2rem 1rem 1rem;
border-radius: 10px;
/* top: a dark gradient so white text stays readable; below: the "photo" */
background:
linear-gradient(to top, rgb(0 0 0 / 0.65), rgb(0 0 0 / 0) 80%),
radial-gradient(circle at 88% 22%, #f4c95d 0 1.4rem, transparent 1.5rem),
linear-gradient(#7fb4d1 0 55%, #2f7d4f 55%),
#2b2118;
}
.hero h2 { margin: 0; } .hero p { margin: 0.2rem 0 0; }
.note {
margin: 0;
padding: 0.6rem 0.75rem 0.6rem 2.5rem;
border: 1px solid #d8cfc0;
border-radius: 8px;
/* an "icon" placed 0.75rem from the left, centered, once; and a color under it */
background: radial-gradient(circle, #8a3b12 0 45%, transparent 48%) left 0.75rem center / 1.2rem 1.2rem no-repeat, #f4efe6;
}
.pattern {
block-size: 4rem;
border-radius: 8px;
/* two stripe layers crossing make a check pattern */
background:
repeating-linear-gradient(45deg, rgb(138 59 18 / 0.25) 0 10px, transparent 10px 20px),
repeating-linear-gradient(-45deg, rgb(47 125 79 / 0.25) 0 10px, transparent 10px 20px),
#f4efe6;
}
Readable text over images
White text on a photo fails contrast wherever the photo is light. A gradient layer on top of the image darkens it behind the text, without editing the image. Make the overlay dark enough that the text keeps 4.5 contrast (article 35) on the lightest part of the image behind it, and keep the fallback color dark too, for when the image doesn't load.

The photo is the same in both; only the extra layer in front of it changed.
Scroll shadows
Layers with different background-attachment values can show a shadow only when a list has more content to scroll. Two "cover" layers scroll with the content (local) and hide the shadows when the list is at either end; two shadow layers stay at the edges of the box (scroll):
body { font: 0.95rem system-ui, sans-serif; margin: 1rem; }
.list {
max-inline-size: 16rem;
block-size: 11rem;
overflow: auto;
margin: 0; padding: 0 0.75rem; list-style: none;
border: 1px solid #d8cfc0; border-radius: 8px;
background:
linear-gradient(white 30%, rgb(255 255 255 / 0)) top / 100% 2.5rem no-repeat local,
linear-gradient(rgb(255 255 255 / 0), white 70%) bottom / 100% 2.5rem no-repeat local,
radial-gradient(farthest-side at 50% 0, rgb(0 0 0 / 0.25), transparent) top / 100% 0.75rem no-repeat scroll,
radial-gradient(farthest-side at 50% 100%, rgb(0 0 0 / 0.25), transparent) bottom / 100% 0.75rem no-repeat scroll,
white;
}
.list li { padding: 0.45rem 0; border-bottom: 1px solid #eee5d6; }
Scroll the list: at the top, the first white layer covers the top shadow; once you scroll, it moves away with the content, and the shadow shows. CSS also has scroll-state container queries for the same idea (article 79), but this background technique works everywhere.

The shadows alone are always on; it's the covers, moving with the content, that switch them off at each end.
Patterns
Repeating gradients layered at different angles or sizes produce stripes, checks, dots and grids with no image files. Keep patterns subtle behind text, since busy backgrounds hurt readability, especially for people with low vision or dyslexia.
What's current
- Multiple backgrounds and the full shorthand syntax have worked in every browser for over a decade.
- Scroll-state container queries (article 79) are a newer way to style scroll shadows; the
localbackground technique still works everywhere.
Common questions
Which background image is on top? The first one in the list.
Why did my background color disappear when I added an image with background?
The shorthand resets everything you leave out, including the color. Add the color to the shorthand's last layer, or use background-image.
Why is my background declaration ignored?
Common causes: a color in a layer other than the last, or a size without a position before it, like url(a.png) / cover. Both make the declaration invalid.
Can I give each layer a different color?
No. There's one background-color, under all layers. For a colored layer, use a gradient with the same color at both ends: linear-gradient(#f4c95d, #f4c95d).
Do the lists for size and position need as many values as there are images? No. Shorter lists repeat, and extra values are ignored.
Summary
background-imagetakes a comma-separated list of layers; the first is on top, and the color is under all of them.- Other background properties take matching lists; shorter ones repeat, longer ones are cut.
- The
backgroundshorthand: image, position/size, repeat, box keywords (origin then clip), attachment, and a color only in the last layer. - The shorthand resets every omitted property to its initial value. Use longhands to change one part.
- Layers make overlays for readable text, icons, patterns, and scroll shadows with
localandscrollattachment.
Practice questions
Write one background declaration with a 50% black overlay on top of photo.jpg, the photo covering the element and centered, and #222 as the fallback color.
Answer
.hero {
background:
linear-gradient(rgb(0 0 0 / 0.5), rgb(0 0 0 / 0.5)),
url("photo.jpg") center / cover no-repeat,
#222;
}
An element has three background images and the CSS below. What size is the third image?
.box { background-size: 2rem, 50%; }
Answer
2rem auto: 2rem wide, with the height from the image's ratio. The list repeats, so the third image takes the first value again.
Why is this declaration ignored?
.box { background: #f4efe6 url("a.png"), url("b.png"); }
Answer
The color is in the first layer. background-color may only appear in the last layer, so the whole declaration is invalid. Move #f4efe6 to the end: background: url("a.png"), url("b.png") #f4efe6;.
What are the background-origin and background-clip of this element?
.box { background: url("a.png") border-box content-box; }
Answer
Origin border-box, clip content-box. With two box keywords, the first is the origin and the second is the clip.
Previous: Backgrounds: Colors, Images, Position, Size, Repeat, Clip and Origin
Next: Linear Gradients: Directions, Color Stops, Hints and Repeating Gradients

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.