background-color

Sets the background colour of an element.

Initial value
transparent
Inherited
No
Animatable
yes, as a colour
Applies to
all elements
JavaScript
element.style.backgroundColor = "#1e1533"
On this page

Example

#
CSS
.alert-success {
  background-color: rgb(16 185 129 / 0.12);
  border: 1px solid rgb(16 185 129 / 0.4);
  color: #6ee7b7;
}
Preview

Definition and usage

#

The background-color property fills an element’s background with a colour. The background covers the content and padding areas, and by default also the area under the border (visible with a dashed border).

The default value is transparent, so elements without a background show their parent’s background. A colour with an alpha channel, for example rgb(124 58 237 / 0.2), tints the background while keeping what is underneath visible.

The background colour is drawn beneath any background image (background-image). This way it works as a fallback colour if the image fails to load.

Syntax

#
Syntax
background-color: <color>;

Values

#

Values in practice

#

transparent

No background, the parent’s background shows through.
.demo { background-color: transparent; }

#7c3aed

A solid, opaque colour.
.demo { background-color: #7c3aed; }

rgb(124 58 237 / 0.25)

The same purple at 25% opacity.
.demo { background-color: rgb(124 58 237 / 0.25); }

color-mix(…)

A mix of two colours in the given proportion.
.demo { background-color: color-mix(in oklab, #7c3aed 40%, #0ea5e9); }

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • Semi-transparent backgrounds for status colours (success, error) look good in both light and dark themes.
  • Set a background colour under the background image too, so the text is readable before the image loads.

Related entries

#

Practise it in lessons

#

Spotted a mistake or missing an example? Write to us.