How can I layer multiple CSS backgrounds without replacing the first one?
Quick answer
Put each image or gradient in a comma-separated background-image list. The first item is painted on top, with later items underneath. If you set position, repeat, or size separately, list values in the same layer order. A later background-image declaration containing just one image specifies a one-layer list, so include every layer you still want.
The question, the details, the next step.
A practical checklist
Write every desired image or gradient in one comma-separated
background-imagelist.Put the layer that should appear on top first; list lower layers afterward.
Align position, repeat, and size values with their layers and keep the same order.
Keep
background-colorseparate; it is not an image-list layer.When changing
background-image, include every layer you still want.
A few more useful answers
Does the first background layer go behind the others?
No. The first listed image layer is painted on top, and later listed layers are underneath it. [S1]
Is the background color another comma-separated layer?
No. background-color is separate from the comma-separated image layers. [S1]
Do layer positions and sizes follow the same order as the images?
Yes. Their comma-separated values correspond to the image layers in order, with the first value for the frontmost layer. [S1]
Sources
References used for this guide.
- [S1] MDN: Using multiple CSS background layers developer.mozilla.org · Checked
