Lesson

CSS Colors

10 min reading
Free Course

CSS Colors: Hex, RGB, HSL, and Opacity Formats

CSS colors control the text color (color), backgrounds (background-color), borders (border-color), and shadows across a web interface. You can write a color as a named keyword, a hexadecimal code, or an rgb() or hsl() function, and each format suits a slightly different workflow. This guide compares them, shows a working button and badge example, and covers the contrast and opacity mistakes that trip up most beginners.

Comparing CSS Color Formats

Hex

A hex color is a # followed by three, four, six, or eight hexadecimal digits. #fff is shorthand for #ffffff. The last pair of digits in an eight-digit value sets transparency, so #2563eb80 is the same blue at roughly 50% opacity (80 in hex is 128 out of 255). Four-digit shorthand like #fff8 works the same way.

RGB and RGBA

rgb() sets red, green, and blue intensity from 0 to 255. Modern CSS lets you separate values with spaces and add transparency after a slash, as in rgb(37 99 235 / 0.85). The alpha value can be a number from 0 to 1 or a percentage. The older comma syntax, rgba(37, 99, 235, 0.85), still works everywhere.

HSL and HSLA

hsl() takes a hue from 0 to 360 degrees, then saturation and lightness as percentages. It is the easiest format to adjust by hand. To make a hover color, keep the hue and saturation and lower the lightness by a few points. The MDN reference for CSS color values lists every accepted syntax.

currentColor

The currentColor keyword uses the element's current text color. If you write border: 1px solid currentColor; or fill: currentColor; on an inline SVG, the border or icon follows the text color automatically. A border with no color specified also defaults to currentColor.

A Practical CSS Colors Example

This code stores one hue in a custom property and builds the button and hover shades from it. See using CSS custom properties for how variables work.

:root {
    --brand-hue: 221;
    --brand-primary: hsl(var(--brand-hue) 83% 53%);
    --brand-hover: hsl(var(--brand-hue) 83% 45%);
}

.btn-primary {
    background-color: var(--brand-primary);
    color: #ffffff;
    border: 2px solid transparent;
    padding: 10px 18px;
}

.btn-primary:hover {
    background-color: var(--brand-hover);
}

.badge-soft {
    background-color: rgba(37, 99, 235, 0.12);
    color: #1d4ed8;
    border: 1px solid currentColor;
}

What You Should See

Apply btn-primary to a button and badge-soft to a <span>. The button should be a medium blue with white text, and it should turn a slightly darker blue on hover. The badge should have a pale blue background, dark blue text, and a thin border in the same dark blue as the text. If the border looks black or gray, check that the color property is set on the same element.

Switching Themes with Custom Properties

Because the colors live in root, you can change the whole palette in one place, for example for dark mode:

@media (prefers-color-scheme: dark) {
    :root {
        --brand-primary: hsl(var(--brand-hue) 83% 62%);
        --brand-hover: hsl(var(--brand-hue) 83% 70%);
    }
}

Dark backgrounds usually need lighter brand colors to stay readable, so re-check contrast after changing them. For more on variables, see our lesson on CSS variables.

Color Contrast and Accessibility

Low contrast is one of the most common accessibility failures. The WCAG contrast guidance sets these minimums for level AA:

  1. 4.5:1 for normal body text.

  2. 3:1 for large text (about 18pt, or 14pt bold) and for UI parts such as input borders and icons.

How to Check Contrast

  1. Open your browser's DevTools and inspect the element.

  2. Click the color swatch next to color in the Styles panel. Chrome and Edge show the contrast ratio and whether it passes AA.

  3. Or paste both colors into the WebAIM Contrast Checker.

Check the hover, focus, and visited states too, not just the default one. WCAG exempts disabled controls from the contrast rules, but they should still be visibly different from enabled ones.

Common CSS Color Mistakes

  • Using opacity to make a background transparent. opacity: 0.5 fades the whole element, including its text and icons. Use rgb(... / 0.5), hsl(... / 0.5), or an eight-digit hex color to fade only the background.

  • Hardcoding hex values everywhere. Put palette colors in root custom properties so rebranding or adding dark mode is a one-place change.

  • Mixing old and new syntax. rgb(37, 99, 235 / 0.5) is invalid because commas and the slash can't be combined. Use all commas (rgba(37, 99, 235, 0.5)) or all spaces (rgb(37 99 235 / 0.5)).

  • Forgetting states. A button that looks fine at rest can become unreadable on hover or focus.

  • Relying on color alone. Don't show errors or required fields using only red, because some users can't tell the difference. Add an icon or text label.

For related lessons on KoderSolution, see CSS backgrounds and CSS borders.

Frequently Asked Questions

What is the modern space-separated syntax in rgb() and hsl()?

Modern CSS Color Level 4 lets you omit commas and add alpha after a slash, like rgb(15 23 42 / 90%) or hsl(217 91% 60% / 0.2).

How does currentColor save CSS code?

Setting border: 1px solid currentColor; or fill: currentColor; on an inline SVG automatically syncs its border or icon color whenever the parent's text color changes.

Conclusion

CSS colors become easier to manage when you pick one format, store your palette in custom properties, and use alpha colors instead of opacity for transparent backgrounds. Check contrast in every state, not only the default one. As a next step, practice by applying your own palette to the CSS backgrounds lesson.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground
CSS Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum