CSS / Reference / background-image: Images, Gradients, Relative Paths, and Layers

CSS background-image paints an image file or a gradient behind an element. Pass a file URL to url() or create a color transition with linear-gradient(). In a comma-separated list, the first image is the top layer.

Set a file or gradient as the background

The CSS background-image specification gives none as the initial value. A parent’s background image is not inherited automatically. The samples show how each value looks over the same base color.

Value Use Sample
none Clear the background image
url(...) Use a saved image file
linear-gradient(...) Create a gradient in CSS
image1, image2 Stack multiple images in order

A background works for decoration or a supporting effect. If the image conveys essential information, such as a product defect or the conclusion of a chart, use <img> with an alt description that conveys its meaning. See the HTML rules for image text alternatives.

Resolve a relative image URL

Suppose the HTML is site/index.html, the stylesheet is site/css/style.css, and the image is site/images/scene.webp. In the external stylesheet, write ../images/scene.webp. The CSS rules for resolving relative URLs resolve the reference from the stylesheet’s location.

.hero {
  min-block-size: 12rem;
  background-color: #16324b;
  background-image: linear-gradient(rgb(0 0 0 / 55%), rgb(0 0 0 / 55%)),
    url("../images/scene.webp");
  background-position: center;
  background-size: cover;
  background-repeat: no-repeat;
  color: #fff;
}

The rule places your scene.webp at the center and scales it to cover the box. The dark gradient sits above the photo, and the base color keeps white text readable if the file is unavailable. In an embedded HTML stylesheet, the relative URL uses the document’s base URL.

Understand the stacking order of backgrounds

Later images are painted beneath earlier images. The background-color remains underneath the entire image stack. If the top image is opaque and covers the same area, lower layers are hidden.

Comma-separated lists in background-size, background-position, and background-repeat correspond to the image list in order. A single value is reused across the layers, so both layers in the example have the same size and position.

Run the example and reverse the layers

Save the complete code as background-image-example.html in UTF-8 and open it in a browser. The shape is Scalable Vector Graphics (SVG) stored in a data URL. No external image file is needed.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Background image comparison</title>
  <style>
    :root { --badge: url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20120%2060%22%3E%3Crect%20width%3D%22120%22%20height%3D%2260%22%20fill%3D%22%230c5676%22%2F%3E%3Ccircle%20cx%3D%2230%22%20cy%3D%2230%22%20r%3D%2218%22%20fill%3D%22%23ffcd68%22%2F%3E%3C%2Fsvg%3E"); }
    body { font: 1rem/1.5 system-ui, sans-serif; margin: 1rem; }
    .samples {
      display: grid;
      grid-template-columns: repeat(auto-fit, minmax(min(100%, 14rem), 1fr));
      gap: 1rem;
      max-width: 60rem;
    }
    .tile {
      block-size: 100px;
      background-color: #e7f1f6;
      background-position: center;
      background-repeat: no-repeat;
      border-radius: .4rem;
    }
    .gradient { background-image: linear-gradient(135deg, #0c5676, #bfe3ff); }
    .file, .layered { background-size: 120px 60px; }
    .file { background-image: var(--badge); }
    .layered {
      background-image: linear-gradient(rgb(255 255 255 / 40%), rgb(255 255 255 / 40%)), var(--badge);
    }
  </style>
</head>
<body>
  <main>
    <h1>Background image comparison</h1>
    <div class="samples">
      <div><p>Single gradient</p><div class="tile gradient" aria-hidden="true"></div></div>
      <div><p>Image set with a URL</p><div class="tile file" aria-hidden="true"></div></div>
      <div><p>Gradient over the image</p><div class="tile layered" aria-hidden="true"></div></div>
    </div>
  </main>
</body>
</html>

The first box is filled with a gradient. The second centers a shape with a circle. The third puts a partly transparent white gradient above that shape, lightening it. To compare the order, replace the .layered rule in the example with the following one.

.layered {
  background-image: var(--badge),
    linear-gradient(rgb(255 255 255 / 40%), rgb(255 255 255 / 40%));
}

With the shape in front, the center matches the second box. The gradient is hidden behind the shape. Restore the original order once you’ve compared them.

Diagnose a background that does not appear

Loading a background doesn’t size the box. Its space comes from content, padding, or dimensions you set. For an empty box with zero height, give it space with min-height or aspect-ratio. Set background-repeat to no-repeat for a single copy and use background-size to choose how it fits.

If the file is missing, inspect the requested URL and response in the browser’s Network panel. If the computed background-image is none, also look for a later background shorthand that reset it.