Skip to content
Tutorials Center
My learning

Topics

Lesson 49 of 139 ยท Backgrounds and Effects

Multiple Backgrounds and background-clip

Stack several background layers on one element, control where each is painted with background-origin and background-clip, and fill text with a gradient.

One element can have more than one background. You can lay a pattern over a photo, put a soft shading gradient over both, and still finish with a solid color underneath, all without a single extra <div>. That keeps your HTML clean and your decoration in the stylesheet where it belongs.

This lesson shows how background layers stack, then looks at two properties that decide where a background lives inside the box: background-origin and background-clip. Finally you will use background-clip: text to fill headings with a gradient. If the basic background properties are new to you, read Backgrounds first.

Layering backgrounds with commas

To add more layers, separate them with commas:

CSS
.card {
  background-image:
    url("/samples/css-backgrounds-tile.svg"),
    url("/samples/sunset.svg");
}

The order is the opposite of what many people guess: the first layer is on top. Think of it as a stack of transparent sheets, where you list them from the one nearest your eyes to the one furthest away. An image that has no transparent areas will completely hide every layer listed after it.

background-color is special. There is only ever one color, and it is always painted below all the image layers, no matter where you write it.

Matching values to layers

Each of the other background properties can also take a comma-separated list. The first value applies to the first layer, the second value to the second, and so on:

CSS
.card {
  background-image: url("badge.svg"), url("photo.jpg");
  background-position: right 10px top 10px, center;
  background-size: 48px, cover;
  background-repeat: no-repeat;
}

Here the badge sits in the top-right corner at 48px, and the photo covers the box. When a list is shorter than the number of images, the browser repeats the list to fill it in. That is why the single no-repeat above applies to both layers. When a list is longer, the extra values are ignored.

With the shorthand, each comma-separated chunk describes one complete layer, and the color goes in the last chunk only:

CSS
.card {
  background:
    url("badge.svg") right 10px top 10px / 48px no-repeat,
    url("photo.jpg") center / cover no-repeat,
    #1e293b;
}

Putting a color in any other chunk is invalid, and the browser will throw away the whole declaration.

Three layers on one element

Swap the order of the first two image layers and nothing much changes, because they barely overlap. Now move the sunset to the top of the list: it is fully opaque, so it hides the fade and the pattern completely.

A gradient is an image. Anywhere you can write url(), you can write a gradient such as linear-gradient(...). Darkening a photo with a gradient layer on top is one of the most useful tricks in this lesson. Gradients covers them in depth.

The three boxes: border, padding and content

Every element has three nested areas, as you learned in The Box Model:

  • the border box, the outside edge of the border;
  • the padding box, just inside the border;
  • the content box, just inside the padding.

Two background properties use these boxes as values: border-box, padding-box and content-box.

background-origin: where the image starts

background-origin sets the positioning area: the rectangle that background-position measures from and that percentage sizes like 100% or cover refer to.

  • padding-box (the default): position 0 0 is the top-left corner inside the border.
  • border-box: position 0 0 is the outer corner of the border.
  • content-box: position 0 0 is the top-left of the content, inside the padding.

It changes where the first tile starts and how big a cover image is. It does not, on its own, stop the background painting elsewhere, because repeated tiles still fill the rest of the box.

background-clip: where painting stops

background-clip sets the painting area. Anything outside it is cut off.

  • border-box (the default): the background reaches under the border.
  • padding-box: the background stops at the inside edge of the border.
  • content-box: the background only fills the content area, leaving the padding empty.

The clip also applies to background-color, which makes it handy for effects such as a "frame" of empty space between a border and the fill.

Comparing background-clip

The dashed border makes the difference easy to see. With border-box, yellow fills the gaps between the dashes. With padding-box, those gaps show the page behind. With content-box, the 20px of padding is empty too.

Origin and clip in the shorthand

In the background shorthand, a box keyword sets both properties at once. With two box keywords, the first is the origin and the second is the clip:

CSS
.frame {
  /* origin: content-box, clip: padding-box */
  background: url("/samples/sunset.svg") content-box padding-box no-repeat;
}

A gradient border with two clips

Here is a classic use for per-layer clipping. You want a border made of a gradient, but border-color only accepts plain colors. The trick: give the element a transparent border, then paint two layers. The top layer is a solid fill clipped to the padding box; the bottom layer is a gradient clipped to the border box. The gradient only shows through where the top layer does not reach, which is exactly the border.

Gradient border with background-clip

Notice linear-gradient(white, white). The top layer has to be an image, because the color can only be the bottom layer. A gradient from white to white is simply a white image.

Filling text with background-clip: text

There is one more clip value: text. It clips the background to the shapes of the element's text, so the background only shows through the letters. To see it, the text itself must be transparent:

CSS
.headline {
  background-image: linear-gradient(90deg, #6366f1, #ec4899);
  background-clip: text;
  color: transparent;
}

background-clip: text works in all current browsers. Older Safari and Chrome versions only understood the prefixed -webkit-background-clip: text, so many stylesheets still include both lines. Including the prefix is harmless and helps old devices.

Gradient-filled heading

This example uses -webkit-text-fill-color: transparent instead of color: transparent. That property only changes the fill of the glyphs, so the normal color stays available as a fallback (and for things like currentColor and text decorations). If the clip is ever unsupported, the heading shows in solid purple instead of disappearing.

Check the contrast. Gradient text is still text. Make sure every part of the gradient has enough contrast with the page background, and keep the effect for short, large headings where it is easy to read.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. With background-image: url(a.svg), url(b.svg);, which image is drawn on top?

    Background layers are listed from top to bottom, so the first one is nearest the viewer.
  2. 2. In the background shorthand with several layers, where may the color go?

    The color is always painted under every image, so it is only allowed in the final comma-separated layer. Anywhere else makes the declaration invalid.
  3. 3. You set three images but only background-repeat: no-repeat, repeat;. What repeat does the third image get?

    A short list is repeated from the beginning, so the third layer takes the first value again.
  4. 4. Which property decides where a background stops being painted?

    background-clip sets the painting area. background-origin only sets the area used for positioning and sizing.
  5. 5. What else is needed for background-clip: text to show the background through letters?

    The background is clipped to the glyph shapes, but opaque text would paint over it. Making the text fill transparent reveals the background.

Summary

  • Separate background layers with commas; the first layer is on top, and the color is always underneath.
  • Other background properties take matching comma lists; short lists repeat to fill the layers.
  • In the shorthand, the color is allowed only in the last layer.
  • background-origin sets the area used for position and size; background-clip sets where painting stops.
  • Two layers with different clips create gradient borders that follow border-radius.
  • background-clip: text plus transparent text fill makes gradient or image-filled headings; keep a solid color as a fallback.

Next: Gradients โ€” linear, radial and conic gradients, color stops and repeating patterns.