The Complete Guide to Colors in CSS

Every way to write a color in CSS, from named colors and hex to hsl(), oklch(), color-mix() and custom properties.

Updated 26 November 2026 · 9 min read

CSS has come a long way from color: red. Modern browsers support wide-gamut colors, perceptually uniform color spaces, color mixing and even generating new colors from existing ones, all in plain CSS. This guide walks through every way to write a color in CSS, with practical advice on when to use each.

Named colors

CSS defines 148 named colors, from the obvious (red, blue) to the colorful (salmon, cornflowerblue, rebeccapurple).

.note { color: darkslategray; background: lightyellow; }

Named colors are handy for prototypes and debugging but rarely match a brand exactly, and some names are surprising. darkgray (#A9A9A9) is actually lighter than gray (#808080).

HEX

The most common format in real-world CSS. You can use three, four, six or eight digits:

color: #f60;        /* short for #ff6600 */
color: #ff6600;
color: #ff660080;    /* 50% transparent (alpha = 80 hex = 128) */
color: #f608;        /* short form with alpha */

HEX is compact and universal, and it is what you’ll get from design tools and from our image color picker. Its weakness is that it is hard to adjust by hand. Making a HEX color 10% lighter means recalculating all three channels.

rgb()

Specifies red, green and blue channels from 0 to 255 (or 0% to 100%), with optional alpha:

color: rgb(255 102 0);           /* modern space-separated syntax */
color: rgb(255 102 0 / 50%);     /* with alpha */
color: rgb(255, 102, 0);         /* legacy comma syntax – still valid */
color: rgba(255, 102, 0, 0.5);   /* legacy alpha – rgba() is now an alias of rgb() */

The modern space-separated syntax, with a slash before the alpha, works in all current browsers and is the same pattern used by every newer color function.

hsl() and hwb()

hsl() describes colors by hue (an angle on the color wheel), saturation and lightness. That makes variations easy to create:

--brand:        hsl(24 100% 50%);
--brand-light:  hsl(24 100% 90%);   /* same hue, much lighter */
--brand-muted:  hsl(24 30% 50%);    /* same hue, less saturated */
--brand-hover:  hsl(24 100% 44%);   /* slightly darker for :hover */

hwb() (hue, whiteness, blackness) is a lesser-known alternative that some people find more intuitive. You start from a pure hue and add white or black, like mixing paint:

color: hwb(24 0% 0%);     /* pure orange */
color: hwb(24 40% 0%);    /* orange mixed with white – a tint */
color: hwb(24 0% 40%);    /* orange mixed with black – a shade */
The HSL catch

HSL lightness isn’t perceptual. hsl(60 100% 50%) (yellow) and hsl(240 100% 50%) (blue) have the same “lightness” value but look completely different in brightness. If you generate a palette by rotating hue in HSL, the colors will look uneven. That’s the problem OKLCH solves.

oklch() and oklab()

OKLCH is a modern color space designed so that equal numeric changes look like equal visual changes. It has three components:

  • L: perceived lightness, from 0 to 1 (or 0% to 100%).
  • C: chroma, or how colorful it is, from 0 upwards (practically up to about 0.37).
  • H: hue angle, from 0 to 360.
--blue:   oklch(60% 0.15 250);
--green:  oklch(60% 0.15 150);   /* same perceived lightness and intensity */
--red:    oklch(60% 0.15 25);

Why it matters:

  • Consistent palettes. Colors with the same L value look equally light, so you can generate accessible color scales by changing only lightness.
  • Wide-gamut colors. OKLCH can describe colors beyond sRGB, the extra-vivid colors that modern P3 displays can show. Browsers map them to the nearest displayable color on older screens.
  • Better gradients. Gradients interpolated in OKLCH or OKLab avoid the muddy grey middle you often get with sRGB.
.hero {
  background: linear-gradient(in oklch, oklch(70% 0.2 30), oklch(70% 0.2 270));
}

oklch() is supported in all major browsers released since 2023. oklab() is the same color space expressed as lightness plus a/b axes instead of chroma and hue. It is useful for mixing, less intuitive for writing by hand.

color-mix()

color-mix() blends two colors in any color space, so you can create tints, shades and transparent versions without a preprocessor like Sass:

--brand: #5b4cf0;
--brand-50:  color-mix(in oklch, var(--brand) 10%, white);
--brand-700: color-mix(in oklch, var(--brand) 80%, black);
--brand-alpha: color-mix(in srgb, var(--brand) 20%, transparent);

The color space you choose affects the result. srgb matches traditional design tools, while oklch or oklab usually give more natural-looking mixes.

Relative color syntax

The newest addition lets you take an existing color, read its channels, and modify them. It works in every color function:

--brand: #5b4cf0;

/* same color, 50% opacity */
--brand-faded: rgb(from var(--brand) r g b / 50%);

/* lighter version: raise OKLCH lightness */
--brand-light: oklch(from var(--brand) calc(l + 0.2) c h);

/* complementary color: rotate hue 180° */
--brand-complement: oklch(from var(--brand) l c calc(h + 180));

Relative colors are supported in current versions of Chrome, Edge, Safari and Firefox. For older browsers, provide a fallback value before the relative declaration.

Organising colors with custom properties

Hard-coding the same HEX value in fifty places makes changes painful. Define your palette once with CSS custom properties, ideally in two layers: raw palette values and semantic roles.

:root {
  /* palette */
  --violet-500: #5b4cf0;
  --violet-50:  #efedff;
  --gray-900:   #16161d;
  --gray-500:   #5d5d6e;
  --white:      #ffffff;

  /* roles */
  --color-bg:      var(--white);
  --color-text:    var(--gray-900);
  --color-muted:   var(--gray-500);
  --color-accent:  var(--violet-500);
  --color-accent-soft: var(--violet-50);
}

.button { background: var(--color-accent); color: var(--white); }

Components reference roles, not raw colors, so rebranding or adding a dark theme only means changing the role mapping. The Copy as CSS button on our image color picker exports an extracted palette as custom properties ready to paste.

Dark mode

The cleanest approach is to remap your role variables using the prefers-color-scheme media query:

:root { color-scheme: light dark; }

@media (prefers-color-scheme: dark) {
  :root {
    --color-bg:    #111117;
    --color-text:  #ececf3;
    --color-muted: #a0a0b2;
  }
}

The newer light-dark() function lets you set both values in one place: color: light-dark(#16161d, #ececf3);. It requires color-scheme to be set. Remember to re-check contrast for dark themes with the contrast checker. Saturated colors often need to be lighter and less intense on dark backgrounds.

Special keywords

  • transparent: fully transparent black, equivalent to rgb(0 0 0 / 0).
  • currentColor: the element’s current color value. It is great for icons and borders that should match the text: border: 1px solid currentColor;
  • inherit, initial, unset, revert: the standard CSS-wide keywords.
  • System colors such as Canvas, CanvasText, LinkText and AccentColor match the user’s operating system theme. They are useful for forced-colors and high-contrast modes.
  • accent-color: a property, not a color value, that tints native checkboxes, radio buttons, range sliders and progress bars in a single line: accent-color: #5b4cf0;

Which should you use?

  • HEX for storing and sharing fixed brand colors.
  • OKLCH for building scales and themes, and for vivid wide-gamut colors.
  • color-mix() and relative colors for generating hover, focus and transparent variants from a single source color.
  • Custom properties for everything, so colors are defined in one place.

Need to convert a color between formats? Use the color converter. New to color formats? Start with HEX, RGB, HSL and CMYK explained.