rgb() / rgba()
Example
.overlay {
background: rgb(0 0 0);
opacity: 0.5;
}.overlay {
background: rgb(0 0 0 / 50%);
}Alpha in the color fades only the background, while opacity also fades the text and children.
Common mistakes
- Mixing commas with the slash, which makes the color invalid.
- Using
opacityto fade a background, which also fades the text.
Key points
- Code uses the space-separated form
rgb(0 0 0 / 50%)rather thanrgba()with commas. - Commas and the slash are not mixed in one function, like
rgb(0, 0, 0 / 50%). - When only the background should fade, alpha goes in the color, not in
opacity.
Interview questions
What is the difference between rgb() and rgba()?
None in practice. Both accept an alpha value, and rgba() is kept as an alias of rgb().
Can rgb() values be percentages?
Yes, like rgb(100% 50% 0%). The space syntax allows mixing numbers and percentages; the comma form needs all channels of one type.