background-color

Term side of the Card.
Topic: Background
Unit title: background-color
Categories: Property
Explanation side of the Card

Explanation: The property that sets the background color of an element. It fills the entire area of the element with the specified color, which can be solid or semi-transparent.

Browser compatibility:
  • The chrome browser supports this feature starting from version 1.
  • The edge browser supports this feature starting from version 12.
  • The firefox browser supports this feature starting from version 1.
  • The safari browser supports this feature starting from version 1.
Availability: 

Example

Wrongcss
.overlay {
  background-color: black;
  opacity: 0.5;
}
Rightcss
.overlay {
  background-color: rgb(0 0 0 / 0.5);
}

An alpha color makes only the background transparent, while opacity fades the content too.

Common mistakes

  • Using opacity for a see-through background and the text fades too.

Key points

  • Text on the background meets WCAG contrast minimums against its text color.
  • Semi-transparent backgrounds use an alpha color like rgb(0 0 0 / 50%), not opacity, so children stay opaque.
  • Theme colors come from custom properties or light-dark() instead of repeated hardcoded hex values.

Interview questions

What is the default background-color?

It is 'transparent', so the parent's background shows through.

Does background-color cover the padding and border?

It covers the padding and the area under the border by default. Use background-clip to change that.

Analytics cookiesWe'd like to use analytics cookies to see which pages are useful. They're set only if you accept. Privacy Policy