background-color
Example
.overlay {
background-color: black;
opacity: 0.5;
}.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
opacityfor 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%), notopacity, 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.