Skip to content
Tutorials Center
My learning

Topics

Lesson 22 of 139 ยท Values and Units

Colors

Learn every everyday way to write a CSS color, from named colors and hex codes to rgb() and hsl(), plus transparency, opacity, currentColor and transparent.

Color is one of the first things people change with CSS, and one of the places where CSS gives you the most choice. The same orange can be written as a name, a hex code, an rgb() triplet or an hsl() angle. They all produce the same pixels; what differs is how easy the value is to read, tweak and reuse.

This lesson covers the color formats you'll see in almost every stylesheet, how transparency works, and two special keywords, currentColor and transparent, that make components easier to theme. The next lesson moves on to modern formats like oklch() and color-mix().

Where colors go

Any property that takes a <color> accepts every format below. The most common ones:

CSS
.card {
  color: #1f2937;                 /* text */
  background-color: white;        /* background */
  border: 1px solid #e5e7eb;      /* border color inside the shorthand */
  box-shadow: 0 4px 12px rgb(0 0 0 / 0.1);
  text-decoration-color: hsl(200 80% 45%);
  accent-color: rebeccapurple;    /* checkboxes, radios, range sliders */
}

Colors also appear inside gradients, outline-color, caret-color, fill and stroke for SVG, and many more.

Named colors

CSS has about 150 color names, from red and navy to papayawhip, cornflowerblue and rebeccapurple. They're case-insensitive and great for quick experiments and teaching, but they're a small, uneven palette: darkgray is actually lighter than gray, for historical reasons. For a real design, you'll want exact values.

Hex colors

A hex color starts with # and gives the red, green and blue channels as two hexadecimal digits each, from 00 (none) to ff (full, 255):

Hex Channels Color
#ff0000 red 255, green 0, blue 0 red
#ffa500 red 255, green 165, blue 0 orange
#1e293b red 30, green 41, blue 59 dark slate
#ffffff all 255 white

There are shorter and longer forms:

  • Three digits, #f80, doubles each digit: #ff8800. Only works when both digits of every channel are equal.
  • Eight digits, #ff880080, adds an alpha (opacity) channel at the end. 80 is 128 out of 255, about 50%.
  • Four digits, #f808, is the short form of the eight-digit version.

Hex is compact and it's what design tools export, which is why it's everywhere. Its weakness is that you can't read it: nobody can look at #3b82f6 and know how to make it 10% darker.

rgb(): red, green and blue

rgb() takes the same three channels as numbers from 0 to 255 (or percentages), with an optional alpha after a slash:

CSS
color: rgb(59 130 246);          /* modern syntax: spaces */
color: rgb(59 130 246 / 50%);    /* with 50% alpha */
color: rgb(59, 130, 246);        /* older syntax: commas, still valid */
color: rgba(59, 130, 246, 0.5);  /* older alpha form, still valid */

In modern CSS, rgba() is just another name for rgb(), so you don't need it. The space-separated syntax with / alpha is the current style, and it's shared by all the newer color functions.

hsl(): hue, saturation and lightness

hsl() describes a color the way people think about it:

  • Hue is an angle on the color wheel, in degrees: 0 is red, 120 is green, 240 is blue, and 360 wraps back to red.
  • Saturation is how colorful it is, from 0% (gray) to 100% (vivid).
  • Lightness goes from 0% (black) through 50% (the pure color) to 100% (white).
CSS
color: hsl(217 91% 60%);         /* a bright blue */
color: hsl(217 91% 60% / 0.5);   /* same blue, half transparent */

The big advantage: you can adjust a color by changing one number. Darker? Lower the lightness. Muted? Lower the saturation. A matching color? Keep saturation and lightness and change the hue. Try it:

Explore hsl()

Move the hue slider with saturation fixed and notice something: yellow at 50% lightness looks much brighter than blue at 50% lightness. HSL's "lightness" is math, not perception. That's the problem oklch() solves.

There is also hwb() (hue, whiteness, blackness), which some people find even more intuitive for tints and shades. It's supported everywhere but much less common.

Transparency: alpha vs opacity

There are two ways to make something see-through, and they behave very differently:

  • An alpha channel in a color (rgb(0 0 0 / 0.5), #00000080) makes only that color transparent. A semi-transparent background leaves the text on top fully solid.
  • The opacity property makes the whole element transparent, including its text, borders, images and children.
Alpha color vs opacity

For overlays, tinted panels and shadows, you almost always want alpha. Save opacity for fading whole elements in and out, for example in transitions.

currentColor

currentColor is a keyword that means "the value of this element's color property". It lets borders, shadows, icons and other parts follow the text color automatically:

currentColor keeps parts in sync

You've actually been using currentColor all along: it's the default for border-color, outline-color and text-decoration-color. A border: 2px solid with no color already uses the text color.

transparent

transparent is fully transparent black, the same as rgb(0 0 0 / 0). It's the initial value of background-color, and it's handy for borders that should take up space without showing, so that a hover color can appear without the layout shifting:

CSS
.tab { border-bottom: 3px solid transparent; }
.tab:hover { border-bottom-color: #4f46e5; }

System colors

CSS also has keywords for the colors of the user's operating system, such as Canvas (the page background), CanvasText, LinkText, ButtonFace and ButtonText. They matter most in dark mode and Windows high-contrast mode, where the user picks the colors. You'll see them again in Accessible CSS.

Contrast comes first

Whatever format you use, text must stand out from its background. The WCAG guidelines ask for a contrast ratio of at least 4.5:1 for normal text and 3:1 for large text. Light gray text on white looks elegant and is hard to read for many people. Browser DevTools show the contrast ratio when you click a color swatch in the Styles panel, so checking takes seconds.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does the hex color #0f08 expand to?

    A four-digit hex color doubles each digit: 0โ†’00, fโ†’ff, 0โ†’00, 8โ†’88. The last pair is alpha.
  2. 2. In hsl(30 100% 50%), what does the 30 describe?

    The first value of hsl() is the hue in degrees; 30 is an orange.
  3. 3. You want a dark overlay behind white text, with the text fully solid. What should you use?

    Alpha affects only that color. opacity would fade the text as well.
  4. 4. An element has color: teal and border: 2px solid. What color is the border?

    border-color defaults to currentColor, the element's text color.
  5. 5. Which statement about rgba() is true in modern CSS?

    rgb() takes an optional / alpha, and rgba() is kept as an equivalent name for compatibility.

Summary

  • Named colors are fine for experiments; use exact values for real designs.
  • Hex is compact (#rrggbb, with #rgb, #rrggbbaa and #rgba variants) but hard to adjust by hand.
  • rgb() and hsl() use the modern syntax rgb(r g b / alpha); rgba() and hsla() are old aliases.
  • hsl() makes tweaks easy, but its lightness doesn't match how bright colors actually look.
  • Alpha in a color affects only that color; opacity fades the whole element.
  • currentColor follows the text color; transparent is fully see-through; always check contrast.

Next: Modern Color: oklch() and color-mix() โ€” perceptual colors, wide-gamut screens and mixing colors in CSS.