CSS / Reference / background-color: Background Alpha, Inheritance, and Contrast

CSS background-color sets the color behind an element’s content. To make only the background partly transparent, include alpha in the color, as in rgb(0 108 156 / 40%). Use opacity when you intend to fade the text and border as well.

Syntax and where the color is painted

The CSS background-color specification specifies transparent as the initial value and no automatic inheritance. A browser stylesheet or theme can supply a different value. Use the Computed panel in developer tools to inspect the color applied to your element.

Value Effect Visible result
transparent Fully transparent Shows the yellow backdrop
#e6f2ff Opaque color Covers the backdrop in pale blue
rgb(0 108 156 / 40%) Color with alpha Blends partly transparent blue with yellow
inherit Explicit inheritance Uses the parent’s computed yellow

By default, the color paints behind the content, padding, and border. The margin is outside the painted area. Set background-clip to padding-box to keep the fill from extending under the border.

Make only the background partly transparent

In the CSS rgb() alpha syntax, 40% after the slash is the opacity of the color. An alpha of 0% is fully transparent and 100% is fully opaque. In the example, partly transparent blue blends with the yellow underneath.

.alpha { background-color: rgb(0 108 156 / 40%); }
.clear { background-color: transparent; }
.faded { background-color: rgb(0 108 156); opacity: .4; }

Color alpha affects the background. opacity applies to the whole rendered element, so the text and border also blend with the backdrop. For cards that need crisp text, apply alpha to the background color.

Distinguish transparency from inheritance

A transparent child lets the parent’s background show through. With inherit, the child uses the parent’s computed background-color to paint a background of its own.

Change the .clear value to inherit in the example. Both versions look yellow here, but the computed value changes from transparent to yellow. Restore transparent before the next comparison.

Choose a readable fallback for background images

The color paints below background-image. It remains visible if a photograph fails to load or a gradient is disabled. A later background shorthand can reset an omitted color. Include the fallback color in the shorthand or keep separate property declarations.

.banner {
  color: #fff;
  background-color: #17314a;
  background-image: linear-gradient(135deg, #204b6b, #315a70);
}

The Web Content Accessibility Guidelines (WCAG) on contrast require at least 4.5:1 for normal text and 3:1 for large text. Large generally means at least 18pt or bold 14pt, with exceptions such as logos. Test against the final color after compositing or against the parts of a photo behind the text.

Run the example and change one setting at a time

Save the code as a UTF-8 file named background-color-example.html and open it in a browser. It uses no external files. Make one change at a time and restore the original value after comparing.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Background color comparison</title>
  <style>
    body { font: 1rem/1.5 system-ui, sans-serif; color: #20242b; margin: 1rem; }
    main { max-width: 65rem; margin-inline: auto; }
    .lab {
      display: grid;
      grid-template-columns: repeat(auto-fit, minmax(min(100%, 13rem), 1fr));
      gap: .75rem;
      background-color: #ffd166;
      padding: .75rem;
    }
    .slot { background-color: #ffd166; }
    .label { font-weight: 700; margin: .25rem .5rem; }
    .sample {
      border: 6px dashed #203446;
      padding: 1rem;
      margin: .5rem;
      min-height: 3rem;
    }
    .banner { padding: 1rem; margin-block: 1rem; }
    .alpha { background-color: rgb(0 108 156 / 40%); }
    .clear { background-color: transparent; }
    .faded { background-color: rgb(0 108 156); opacity: .4; }

    .banner {
      color: #fff;
      background-color: #17314a;
      background-image: linear-gradient(135deg, #204b6b, #315a70);
    }
  </style>
</head>
<body>
  <main>
    <h1>Background color comparison</h1>
    <div class="lab">
      <div class="slot">
        <p class="label">transparent</p>
        <div class="sample clear">Compare the text and borders of the three boxes.</div>
      </div>
      <div class="slot">
        <p class="label">Background alpha at 40%</p>
        <div class="sample alpha">Compare the text and borders of the three boxes.</div>
      </div>
      <div class="slot">
        <p class="label">Element opacity at 40%</p>
        <div class="sample faded">Compare the text and borders of the three boxes.</div>
      </div>
    </div>
    <p class="banner">This text remains visible with the image layer disabled.</p>
  </main>
</body>
</html>

The first box shows the yellow backdrop. The second and third have similar fills, but only the third fades its text and border. Change the alpha on .alpha to 100% to hide the yellow beneath the fill. Restore 40%, then add background-clip: padding-box to reveal yellow between the border dashes.

Disable the banner’s background-image in developer tools. The white text stays visible over the fallback. With a real photo, check both light and dark areas behind the text. For applying these ideas to a table, see the table background and striping examples.