Full answer
CSS accepts several ways to write the same color.
- Named colors such as
tomatoornavy. Handy for quick tests. - Hex:
#ff6347(red, green, blue in base 16).#f63is a short form when each pair repeats.#ff634780adds an alpha (transparency) pair. rgb():rgb(255 99 71). Add transparency with a slash:rgb(255 99 71 / 50%). The older comma formrgb(255, 99, 71)still works.hsl(): hue (an angle on the colour wheel, 0 to 360), saturation and lightness, as inhsl(9 100% 64%). This is the easiest format to adjust by hand. Keep the hue and raise the lightness for a lighter shade.transparentis fully transparent.currentColoris the element's current textcolor. It lets borders, SVG icons and shadows follow the text colour automatically.
Choose by purpose. Hex is compact and common in design tools. hsl() is better for building a family of shades. A custom property such as --brand-hue: 9 can feed several hsl() colours. Always check text contrast against its background, whichever format you use.
The first three swatches are the same color written three ways, and the browser reports all of them as rgb(255, 99, 71). The fourth is half transparent. The last two share one hue with different lightness. The underline of the link matches the teal text because of currentColor.
Wide-gamut formats such as oklch() also exist. They are covered in a separate question.
<style>
.swatch { display: inline-block; width: 60px; height: 40px; }
#a { background: #ff6347; }
#b { background: rgb(255 99 71); }
#c { background: tomato; }
#d { background: rgb(255 99 71 / 50%); }
.link { color: teal; border-bottom: 2px solid currentColor; }
:root { --hue: 200; }
#e { background: hsl(var(--hue) 70% 30%); }
#f { background: hsl(var(--hue) 70% 80%); }
</style>
<span class="swatch" id="a"></span><span class="swatch" id="b"></span><span class="swatch" id="c"></span>
<span class="swatch" id="d"></span><span class="swatch" id="e"></span><span class="swatch" id="f"></span>
<p><span class="link" id="link">Read more</span></p>Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- Hex, rgb() and hsl() can all describe the same color.
- Alpha adds transparency: #rrggbbaa or rgb(r g b / 50%).
- hsl() separates hue, saturation and lightness, so shades are easy to adjust.
- currentColor reuses the element's text color for borders, icons and shadows.
- Alpha on a color fades only that color; opacity fades the whole element.
Common mistakes
- Writing a hex color with five digits, or without the leading #. Valid hex has 3, 4, 6 or 8 digits.
- Using opacity on an element when only the background should be transparent. Opacity fades the text and children too, while an alpha color fades only that color.
- Picking text and background shades by eye, without checking the contrast ratio.
Follow-ups
- What is the difference between opacity: 0.5 and a color with 50 percent alpha?
- How would you build a lighter and a darker shade of a brand color?
Tips
- Show that you can convert in your head only in simple cases, such as #ff0000 equals rgb(255 0 0). For real work use DevTools' color picker.
- Mention currentColor for icons. It saves a lot of repeated color values.