AD JB Toolbox 01 EN

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

background-color sets an element’s background color. The painting area is also controlled by background-clip; margin is outside the painted box.

Syntax and value choices

Property Initial value Inherited Main application conditions
background-color transparent No All elements; visible effects depend on the text, image, or box conditions below

Initial values are not necessarily the values assigned to HTML elements by the browser’s stylesheet. Inspect the actual result in developer tools’ Computed panel.

.card { background-color: #e6f2ff; }
.overlay { background-color: rgb(0 0 0 / 40%); }
Value or category Behavior and choice
transparent Allow the background behind the element to show through.
#e6f2ff Specify a hexadecimal color.
rgb(0 0 0 / 40%) Make the background color partially transparent.
inherit Explicitly use the parent’s computed background color.

For supported values and conditions, see MDN background-color.

Run the example and compare results

Save this complete code as css-reference-10588-example.html in UTF-8 and open it in a browser. Keep its demonstration styles in that file rather than pasting them into a site-wide stylesheet.

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>background-color comparison example</title>
<style>
body { margin: 0; color: #20242b; background: #fff; font: 16px/1.7 "Malgun Gothic", sans-serif; }
#demo { padding: 24px; max-width: 820px; margin: auto; }
h1 { font-size: 22px; margin: 0 0 20px; }
.samples { display: flex; flex-wrap: wrap; gap: 24px; }
.label { margin: 0 0 8px; font-weight: 700; }
.samples { background: #ffd166; padding: 20px; }
.target, .reference { width: 220px; padding: 18px; border: 6px dashed #333; margin: 10px; }
.target { background-color: rgb(0 108 156 / 40%); }
.reference { background-color: transparent; }

/* Keep the practice box within a narrow viewport. */
.target, .reference { max-width: calc(100vw - 48px - 108px); }
</style>
</head>
<body>
<main id="demo">
<h1>background-color comparison</h1>
<section class="samples"><div class="a"><p class="label">Background alpha: 40%</p><div class="target">Compare the CSS result. This sample sentence demonstrates wrapping and the visible area.</div></div><div class="b"><p class="label">transparent</p><div class="reference">Compare the CSS result. This sample sentence demonstrates wrapping and the visible area.</div></div></section>
</main>
</body>
</html>

The partly transparent color composites with the yellow background while the text stays opaque. The margin exposes the parent’s background.

background-color browser comparison showing the specified and reference values in the same example

Transparency is not inheritance

A transparent child revealing its parent’s background is different from inheriting its color. background-color is not normally inherited; inherit explicitly assigns the parent’s computed color to a new background.

Images and readable contrast

Background color is painted below background-image layers. Choose a fallback color that remains readable if an image fails, and inspect text against both bright and dark image areas. A later background shorthand can reset the color.

Change one setting at a time

Use the complete example file above. Change only the specified selector or value rather than several comparison conditions at once.

  1. Change only .target’s background alpha from 40% to 100%, comparing compositing without changing text opacity, then restore it.
  2. Measure contrast between the final background and foreground. Distinguish the usual 4.5:1 text threshold from 3:1 for large text and applicable exceptions, accounting for alpha compositing.

When adding a real background image, retain a readable opaque background-color. Temporarily disable background-image and inspect the same foreground over that fallback, then restore it. WCAG large text generally means at least 18pt (24 CSS px), or bold 14pt (about 18.67 CSS px); logos and inactive controls have separate exceptions.

Check the relevant conditions in WCAG text contrast criteria.

For combining background properties in one practical design, see CSS Background Colors and Images: cover and contain. Related properties to compare alongside these conditions are opacity, background-clip.

When the result is missing or unexpected

  • Check whether an image layer or another element covers the color.
  • Compare background alpha with opacity if text also becomes faded.
More in This Category
CSS / Reference / font-size: Understand px, em, rem, and Text Enlargement

CSS / Reference / font-size: Understand px, em, rem, and Text Enlargement

Choose text-size units by their reference and test nested em scaling. Check the actual root size, browser enlargement, and layout constraints.

CSS / Reference / font-weight: Numeric Weights, Relative Weights, and Font Matching

CSS / Reference / font-weight: Numeric Weights, Relative Weights, and Font Matching

Request numeric or relative font weights and inspect the supplied faces. Compare static and variable-font behavior without assuming every number produces a unique weight.

CSS / Reference / empty-cells: Show or Hide Empty Table Cells

CSS / Reference / empty-cells: Show or Hide Empty Table Cells

Control empty-cell borders and backgrounds in a separated table while preserving its structure. Compare show, hide, whitespace, and the collapsed-border exception.

CSS / Reference / font-variant: Small Capitals and Font Features

CSS / Reference / font-variant: Small Capitals and Font Features

Use small capitals without changing the source string and choose specific variant longhands. Compare real font support with synthesized presentation.

CSS / Reference / text-indent: First-Line Indents and Hanging Paragraphs

CSS / Reference / text-indent: First-Line Indents and Hanging Paragraphs

Create first-line and hanging indents, distinguish them from paragraph padding, and check inheritance, writing direction, and narrow-screen clipping.

CSS / Reference / calc(): Calculate CSS Values and Understand Their Types

CSS / Reference / calc(): Calculate CSS Values and Understand Their Types

Combine percentages and lengths with calc(), inspect the actual parent width, and distinguish invalid expressions from newer typed arithmetic and special numeric results.

CSS / Reference / background-repeat: Tiling, Space, and Round

CSS / Reference / background-repeat: Tiling, Space, and Round

Control horizontal and vertical background tiling and compare repeat, no-repeat, space, and round with the same tile and positioning area.

CSS / Reference / quotes: Choose Nested Quotation Marks

CSS / Reference / quotes: Choose Nested Quotation Marks

Choose generated quotation pairs for nested text and distinguish them from literal punctuation. Compare automatic marks, none, and quotation-depth operations.

CSS / Reference / font-family: Font Lists, Glyph Fallback, and Loading

CSS / Reference / font-family: Font Lists, Glyph Fallback, and Loading

Build a font stack with appropriate generic fallbacks and inspect the fonts actually used. Distinguish naming a family from loading files and covering characters.

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

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

Set a background or a partly transparent color without fading the text. Distinguish transparency from inheritance and provide readable image fallbacks.