On this page
CSS Colors (HEX, RGB, HSL, OKLCH)
What Are CSS Colors?
CSS colors control the look of text, backgrounds, borders, shadows and more. In this lesson our focus keyword is CSS colors, and you will learn every major format: HEX, RGB, HSL and the modern OKLCH. We also cover trending topics that developers search for: CSS color-mix(), relative color syntax, light-dark() for dark mode, wide-gamut Display P3 colors, CSS color palette generation, and color contrast for accessibility.
1. Named Colors and Keywords
CSS has about 148 named colors such as tomato, rebeccapurple and seagreen. Special keywords include transparent (fully transparent) and currentColor, which uses the element's current text color. currentColor is very useful for icons and borders that should follow the text color.
2. HEX Colors
HEX is the most common format. It starts with # and uses hexadecimal digits (0-9, a-f).
#rrggbb: for example#2563eb(red 25, green 63, blue eb).#rgb: shorthand when each pair repeats, so#fffequals#ffffff.#rrggbbaaand#rgba: the last pair is alpha (opacity).#00000080is black at about 50% opacity.
3. RGB Colors
rgb(37 99 235) defines red, green and blue channels from 0 to 255 (or percentages). Modern syntax uses spaces and a slash for alpha: rgb(37 99 235 / 50%). The old comma syntax rgba(37, 99, 235, 0.5) still works, but rgb() now accepts alpha too, so rgba() is no longer needed.
4. HSL Colors
hsl(221 83% 53%) stands for Hue, Saturation, Lightness. Hue is an angle on the color wheel (0 = red, 120 = green, 240 = blue), saturation is color intensity, and lightness runs from 0% (black) to 100% (white). HSL is human friendly: to make a hover color, just lower the lightness by 8%. It is great for building palettes with CSS variables.
5. OKLCH: The Modern Color Space
oklch(62% 0.2 260) has three parts: L lightness (0% to 100%), C chroma (color intensity, roughly 0 to 0.4) and H hue angle (0 to 360). OKLCH is perceptually uniform: two colors with the same lightness really look equally bright. In HSL, yellow at 50% lightness looks far brighter than blue at 50% lightness, which makes accessible palettes hard. OKLCH fixes this. It can also express wide-gamut colors beyond sRGB on modern screens. Tailwind CSS v4 and many design systems now use OKLCH for their palettes.
6. Format Comparison
| Format | Example | Best for |
|---|---|---|
| HEX | #2563eb | Copying from design tools, short values |
| RGB | rgb(37 99 235 / 50%) | Transparency, JavaScript work |
| HSL | hsl(221 83% 53%) | Simple palettes and hover states |
| OKLCH | oklch(62% 0.2 260) | Accessible, consistent, wide-gamut palettes |
7. Modern Color Functions
color-mix()
Mixes two colors in a chosen color space: color-mix(in oklch, #2563eb 70%, white) creates a lighter tint without extra variables.
Relative color syntax
Derive a new color from an existing one: oklch(from var(--brand) calc(l + 0.1) c h) makes a lighter version of --brand. You can also change alpha: rgb(from var(--brand) r g b / 40%).
light-dark()
Returns the first color in light mode and the second in dark mode: color: light-dark(#111, #eee);. It requires color-scheme: light dark on the element or on :root.
Display P3 with color()
color(display-p3 0 1 0) can show a more vivid green than any sRGB color. Always provide an sRGB fallback before it.
8. Colors and Accessibility
- WCAG requires a contrast ratio of at least 4.5:1 for normal text and 3:1 for large text (18px bold or 24px regular).
- Never use color as the only way to show meaning (for example errors). Add icons or text.
- Support dark mode with
prefers-color-schemeorlight-dark(). - Test with DevTools color picker, which shows the contrast ratio.
9. Best Practices
- Store brand colors in CSS custom properties:
--brand: oklch(62% 0.2 260);. - Prefer OKLCH or HSL for palettes and use HEX only when copying from tools.
- Use
currentColorfor icons and SVG. - Use
color-mix()for tints and shades rather than hand-typing many variants. - Provide fallbacks for very new features with
@supports.
10. Common Mistakes
- Using a comma inside the space-separated syntax, for example
rgb(37, 99 235), which is invalid. - Forgetting that
opacityfades the whole element, while alpha in a color fades only that color. - Using low-contrast light gray text on white.
- Writing HEX with 5 or 7 digits, which is invalid.
Summary
HEX, RGB, HSL and OKLCH all describe colors, but OKLCH gives the most predictable results for modern design systems. Combine them with color-mix(), relative colors and light-dark() for flexible themes. Try the live examples below.
Press Run to execute.
Press Run to execute.
Press Run to execute.
Press Run to execute.
Press Run to execute.
Style the four boxes so that they all show the same blue (#2563eb) but each box uses a different format: HEX, rgb(), hsl() and oklch(). Approximate values are fine for hsl and oklch: hsl(221 83% 53%) and oklch(55% 0.21 263).
Press Run to execute.
Show expected output
All four boxes look the same blue.This is a self-check — compare your result with the expected output above.
Define --brand as a custom property on :root. Give the .btn the brand color as background. On hover, darken it using color-mix() with black (20%). Add a .soft box whose background is a 15% mix of the brand color with white.
Press Run to execute.
Show expected output
Button darkens on hover and the soft box shows a pale tint of the brand color.This is a self-check — compare your result with the expected output above.
Was this page helpful?