Skip to main content

CSS Colors and Backgrounds

Layered Backgrounds

Written by Updated Layered Backgrounds exercises

Layer background images, control their painting area, and use borders that are more than a solid line. You will layer gradients and images, control their size and painting boxes, and create image- or gradient-based border treatments.

One element can have several backgrounds stacked on top of each other: a dark overlay on top of a photo, or a gradient on top of a pattern. You list them with commas, and the first one you write sits on top.

That saves extra HTML. Before multiple backgrounds, a dark overlay on a hero image needed an extra div just for the overlay. The building blocks are CSS Background Image and CSS Gradients.

Top layer first

Example

css

.hero {
  background:
    linear-gradient(rgb(15 23 42 / 0.72), rgb(15 23 42 / 0.72)),
    url("https://picsum.photos/seed/coast/1200/600") center / cover no-repeat;
}

The first layer is a dark, see-through gradient. The second is the photo, centered and covering the box. Because the gradient comes first, it sits on top, darkening the photo so white text on it stays readable.

Only the last layer may have a background color, and it is painted underneath everything. If you put a color in an earlier layer, the whole declaration is invalid and ignored.

A gradient border

Example

css

.label {
  border: 0.5rem solid transparent;
  padding: 1rem;
  background:
    linear-gradient(white, white) padding-box,
    linear-gradient(135deg, #06b6d4, #7c3aed) border-box;
}

Here both layers are gradients. The white one only fills the padding area (padding-box), and the colorful one fills everything up to the outer edge of the border (border-box). Because the border itself is transparent, the colorful layer shows through only where the border is. The result is a gradient border with rounded-corner support, something border-color cannot do.

background-clip decides how far a layer is painted, and background-origin decides where its position is measured from.

Useful companion properties

  • background-size: explicit sizes, cover, or contain
  • background-attachment: scrolling behavior of the image
  • border-image: slices an image or gradient around the border
  • box-decoration-break: controls decoration across fragmented inline boxes

Background Layer Properties

PropertyPurposeExample
background-imageLists paint layers from front to back.linear-gradient(...), url(...)
background-positionPositions each background layer.center, top right
background-sizeSets each layer's rendered size.cover, 4rem 4rem
background-clipLimits the painting area.padding-box

Complete Example

Complete runnable example

html

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Layered Backgrounds example</title>
  <style>
    * { box-sizing: border-box; }
    body {
      margin: 0;
      padding: 2rem;
      font-family: system-ui, sans-serif;
      line-height: 1.5;
    }
    main { max-width: 70rem; margin-inline: auto; }
    article, section, aside, form, .card, .panel {
      padding: 1rem;
      border: 1px solid #cbd5e1;
      border-radius: 0.6rem;
    }

.hero {
  background:
    linear-gradient(rgb(15 23 42 / 0.72), rgb(15 23 42 / 0.72)),
    url("https://picsum.photos/seed/coast/1200/600") center / cover no-repeat;
}

.label {
  border: 0.5rem solid transparent;
  padding: 1rem;
  background:
    linear-gradient(white, white) padding-box,
    linear-gradient(135deg, #06b6d4, #7c3aed) border-box;
}
  </style>
</head>
<body>
  <main><section class="hero"><h1>Layered backgrounds</h1><p>The gradient remains visible even if the image is unavailable.</p></section><p class="label">Gradient border</p></main>
</body>
</html>

Browser Support

Feature
Chrome
Edge
Firefox
Safari
Multiple backgrounds1123.61.3

Versions show the first stable desktop release with unprefixed support. Data source: MDN Browser Compatibility Data 8.0.8. A partial-support note is included where it changes how the example behaves.

Good to know

  • Each layer can have its own size, position and repeat. Separate them with commas in the same order as the images.
  • If you give fewer sizes than layers, the list repeats from the start, which can be confusing. Write one value per layer.
  • Backgrounds are decoration. An image that carries meaning, such as a product photo, should be an <img> with alt text, because a background has no text alternative.

Layered Backgrounds exercises

Two layers

Give the card two background images: first https://html5andcss3.org/samples/star.svg, then under it a gradient from yellow to orange.

The HTML your code works on
<div class="card">Card</div>

Result

Check your answer to see it here.