CSS Reference ยท Colors and Backgrounds
CSS color values
Reference for CSS color notations: named colors, hex, rgb(), hsl(), hwb(), lab(), lch(), oklab(), oklch(), color(), alpha and relative colors, with examples.
A CSS <color> is any value you can give to properties such as color, background-color, border-color or accent-color. CSS offers many notations for it. They all describe a color in some color space; the difference is which numbers you write and how easy they are to reason about.
This page is a concept overview. Each function has its own reference page, linked below.
.a { color: rebeccapurple; } /* named */ .b { color: #7c3aed; } /* hex */ .c { color: rgb(124 58 237); } /* rgb */ .d { color: hsl(263 83% 58%); } /* hsl */ .e { color: oklch(55% 0.23 293); } /* oklch */ .f { color: rgb(124 58 237 / 50%); } /* with alpha */
Try it
All rows except the last show the same red (the oklch() value is a rounded equivalent, so it may differ by a hair). The last row is the same red with transparency.
In the hsl() strip yellow and green look much brighter than blue at the same "lightness". The oklch() strip is built to look evenly light across hues.
Notations
| Notation | Example | Notes |
|---|---|---|
| Named color | rebeccapurple |
148 keywords. See named colors. |
transparent |
transparent |
Fully transparent black (rgb(0 0 0 / 0)). |
currentcolor |
currentcolor |
Uses the element's computed color. Case-insensitive. |
| Hex | #7c3aed, #7c3aed80, #73e, #73e8 |
3, 4, 6 or 8 digits; the extra digit pair or digit is alpha. |
rgb() / rgba() |
rgb(124 58 237 / 50%) |
sRGB channels 0โ255 or percentages. rgba() is an alias. See rgb(). |
hsl() / hsla() |
hsl(263 83% 58%) |
Hue angle, saturation, lightness. See hsl(). |
hwb() |
hwb(263 20% 5%) |
Hue, whiteness, blackness. See hwb(). |
lab() / lch() |
lch(50% 80 300) |
Device-independent CIE spaces. See lab(), lch(). |
oklab() / oklch() |
oklch(55% 0.23 293) |
Perceptually uniform spaces; the best choice for generating palettes. See oklab(), oklch(). |
color() |
color(display-p3 1 0.2 0.2) |
Any predefined space: srgb, display-p3, rec2020, xyz and others. See color(). |
color-mix() |
color-mix(in oklch, red, blue) |
Mixes two colors. See color-mix(). |
light-dark() |
light-dark(#fff, #111) |
Picks by color scheme. See light-dark(). |
| System colors | Canvas, ButtonText |
Follow the platform and color scheme. See system colors. |
Usage notes
- Modern syntax uses spaces and a slash. Write
rgb(255 0 0 / 50%)rather thanrgba(255, 0, 0, 0.5). The comma form still works forrgb()andhsl(), but the space form is required for newer functions and cannot be mixed with commas. - Alpha can be a number from 0 to 1 or a percentage. Hex colors carry alpha in the last pair of digits.
noneand missing components. In modern functions a component can benone, which acts like zero for rendering but behaves differently when interpolating. It matters for gradients and transitions.- Relative colors. Many functions accept
from <color>to derive a new color:oklch(from var(--brand) calc(l + 0.1) c h). Check a compatibility table for support. - Gamut.
display-p3,lab(),lch(),oklab()andoklch()can describe colors outside sRGB. On screens that cannot show them, the browser maps them to the nearest displayable color. - Pick one primary notation for design tokens, and use custom properties so the palette lives in one place.
- Contrast. Always check text against its background. Notation never changes the contrast result, only the numbers you edit.
- Interpolation. Gradients, transitions and animations blend colors in a color space. Newer syntax lets you choose it, for example
linear-gradient(in oklch, ...).
Browser support
Named colors, hex, rgb() and hsl() work everywhere. hwb(), lab(), lch(), oklab(), oklch(), color() and color-mix() are supported in current releases of all major browsers; relative color syntax is newer, so check a compatibility table before relying on it.
Related
colorโ sets the text colorbackground-colorโ sets a background colorcolor-schemeโ scheme-aware system colorsopacityโ transparency for the whole element- Lessons: Colors, Modern Colors