Color

Functions that produce a <color>, from the long-standing rgb() and hsl() to the perceptual Lab and OKLCH spaces, plus functions that mix, adjust or choose colours. They work anywhere a colour is accepted, including inside gradients. The properties that take colours are under Color & backgrounds; the <color> type itself is under Types.

sRGB colours

rgb()

A colour from red, green and blue channels, each a number from 0 to 255 or a percentage, with an optional alpha after a slash. This is the same sRGB space that hex colours use.

Syntax[ <legacy-rgb-syntax> | <modern-rgb-syntax> ]
Returns<color>
Supportrgb(): Chrome 1 · Firefox 1 · Safari 1
Space-separated channels and alpha: Chrome 65 · Firefox 52 · Safari 12.1
Relative syntax (from): Chrome 122 · Firefox 128 · Safari 18
Speccss-color-4

The modern syntax is rgb( [ from <color> ]? [ <number> | <percentage> | none ]{3} [ / [ <alpha-value> | none ] ]? ), with spaces between channels. The legacy syntax separates the channels with commas, takes an optional fourth alpha argument, and does not allow mixing numbers and percentages or none.

Relative colour syntax. Start the arguments with from and a colour, and that origin colour is converted to the function's space. Inside the function, the channel keywords (r, g, b and alpha here) stand for the origin's channel values, so you can keep them, replace them or change them with calc(). Every colour function on this page except light-dark(), contrast-color() and the three mix functions accepts the same from form with its own channel keywords.

color: rgb(30 144 255);
background: rgb(30 144 255 / 0.2);
border-color: rgb(from var(--brand) r g b / 50%);
A solid colour, the same colour at 20% opacity, and a relative colour: the brand colour at half opacity.

rgba()

A legacy alias of rgb(). It accepts exactly the same arguments and gives the same result; it dates from when rgb() did not take an alpha value. New code can use rgb() throughout.

Syntax[ <legacy-rgba-syntax> | <modern-rgba-syntax> ]
Returns<color>
SupportChrome 1 · Firefox 1 · Safari 1
Speccss-color-4

hsl()

An sRGB colour given as hue, saturation and lightness. The hue is an angle around the colour wheel (a plain number means degrees: 0 red, 120 green, 240 blue); saturation and lightness are percentages.

Syntax[ <legacy-hsl-syntax> | <modern-hsl-syntax> ]
Returns<color>
Supporthsl(): Chrome 1 · Firefox 1 · Safari 3.1
Relative syntax (from): Chrome 125 · Firefox 128 · Safari 18
Speccss-color-4

As with rgb(), the modern syntax separates the values with spaces and puts alpha after a slash; the legacy syntax uses commas. HSL lightness is not perceptual: a yellow and a blue with the same lightness look very different in brightness. Use oklch() when that matters.

hsla()

A legacy alias of hsl() with the same arguments and result.

Syntax[ <legacy-hsla-syntax> | <modern-hsla-syntax> ]
Returns<color>
SupportChrome 1 · Firefox 1 · Safari 3.1
Speccss-color-4

hwb()

An sRGB colour given as a hue plus how much white and how much black to mix into it, which is close to how people pick colours from a palette.

Syntaxhwb([from <color>]? [<hue> | none] [<percentage> | <number> | none] [<percentage> | <number> | none] [ / [<alpha-value> | none] ]? )
Returns<color>
SupportChrome 101 · Firefox 96 · Safari 15
Speccss-color-5

Lab & LCH colours

lab()

A colour in the CIE Lab space: lightness from 0 to 100, then two axes, a (green to red) and b (blue to yellow). Lab can describe every visible colour, not just those inside sRGB.

Syntaxlab([from <color>]? [<percentage> | <number> | none] [<percentage> | <number> | none] [<percentage> | <number> | none] [ / [<alpha-value> | none] ]? )
Returns<color>
SupportChrome 111 · Firefox 113 · Safari 15
Speccss-color-5

lch()

The polar form of Lab: lightness, chroma (how colourful) and hue angle. Changing the hue keeps the lightness the same, which HSL does not.

Syntaxlch([from <color>]? [<percentage> | <number> | none] [<percentage> | <number> | none] [<hue> | none] [ / [<alpha-value> | none] ]? )
Returns<color>
SupportChrome 111 · Firefox 113 · Safari 15
Speccss-color-5

oklab()

A colour in the OKLab space, a refinement of Lab whose lightness and hue stay more even across the whole range. Lightness runs from 0 to 1 (or 0% to 100%); a and b are roughly between -0.4 and 0.4.

Syntaxoklab([from <color>]? [<percentage> | <number> | none] [<percentage> | <number> | none] [<percentage> | <number> | none] [ / [<alpha-value> | none] ]? )
Returns<color>
SupportChrome 111 · Firefox 113 · Safari 15.4
Speccss-color-5

oklch()

The polar form of OKLab: lightness from 0 to 1, chroma from 0 to about 0.4, and a hue angle. It is the most practical space for building palettes, because equal steps in lightness look like equal steps, whatever the hue.

Syntaxoklch([from <color>]? [<percentage> | <number> | none] [<percentage> | <number> | none] [<hue> | none] [ / [<alpha-value> | none] ]? )
Returns<color>
Supportoklch(): Chrome 111 · Firefox 113 · Safari 15.4
Relative syntax (from): Chrome 122 · Firefox 128 · Safari 18
Relative syntax from currentcolor: Chrome 131 · Firefox 133 · Safari 18
Speccss-color-5

In relative syntax the channel keywords are l, c, h and alpha. Adjusting l darkens or lightens a colour without shifting its hue, and rotating h gives harmonious companions.

--brand: oklch(62% 0.19 255);
--brand-dark: oklch(from var(--brand) calc(l - 0.15) c h);
--brand-muted: oklch(from var(--brand) l calc(c / 2) h);
--accent: oklch(from var(--brand) l c calc(h + 180));
A darker shade, a less saturated tint and the complementary colour, all derived from one brand colour.

Other colour spaces

color()

A colour in a named colour space: srgb, srgb-linear, display-p3, a98-rgb, prophoto-rgb, rec2020, or the XYZ spaces xyz, xyz-d50 and xyz-d65. Use it to reach the wider gamuts of modern displays.

Syntaxcolor( [from <color>]? <colorspace-params> [ / [ <alpha-value> | none ] ]? )
Returns<color>
SupportChrome 111 · Firefox 113 · Safari 15
Speccss-color-5

<colorspace-params> is the colour-space name followed by its three channel values, usually numbers from 0 to 1.

color: color(display-p3 1 0.2 0.1);
A red more vivid than sRGB can show; screens that cannot display it map it to the nearest colour they can.

Mixing, choosing & adjusting

color-mix()

Mixes colours in a chosen colour space, with optional percentages for how much of each to use. The colour space after in changes the result: oklab and oklch usually give the most even blends.

Syntaxcolor-mix( <color-interpolation-method>? , [ <color> && <percentage [0,100]>? ]#)
Returns<color>
Supportcolor-mix(): Chrome 111 · Firefox 113 · Safari 16.2
More than two colours: Chrome no · Firefox 150 · Safari 27
Speccss-color-5

<color-interpolation-method> is in followed by a colour space, and for polar spaces (hsl, hwb, lch, oklch) an optional shorter, longer, increasing or decreasing hue. Leaving out a percentage splits the remainder evenly.

background: color-mix(in oklch, var(--brand) 20%, white);
border-color: color-mix(in srgb, currentcolor 30%, transparent);
A pale tint of the brand colour, and the text colour at 30% opacity.

light-dark()

Picks the first value when the element's used colour scheme is light and the second when it is dark. It only switches when color-scheme allows both, for example color-scheme: light dark on the root.

Syntax<light-dark-color> | <light-dark-image>
Returns<color>, or <image> for the image form
SupportColours: Chrome 123 · Firefox 120 · Safari 17.5
Images: Chrome 150 · Firefox 150 · Safari 27
Speccss-color-5

The colour form is light-dark(<color>, <color>); the image form takes two images, either of which may be none.

:root { color-scheme: light dark; }
body { background: light-dark(#fff, #111); color: light-dark(#111, #eee); }
Page colours that follow the user's light or dark preference.

contrast-color()

Returns white or black, whichever contrasts more with the given colour. Use it for text on a background colour you do not control, such as a user-chosen tag colour.

Syntaxcontrast-color( <color> )
Returns<color>
SupportChrome 147 · Firefox 146 · Safari 26
Speccss-color-5

Picking the higher-contrast of black and white does not guarantee the result meets accessibility contrast guidelines for every mid-tone background.

.tag { background: var(--tag); color: contrast-color(var(--tag)); }
Readable text on any tag colour.

alpha()

Takes a colour and changes only its alpha, keeping the colour in whatever space it was given. It is a short form of the relative colour syntax for the common case of making a colour more or less transparent.

Syntaxalpha(from <color> / [<alpha-value> | none])
Returns<color>
SupportChrome 152 · Firefox 155 · Safari 27
Speccss-color-5
box-shadow: 0 4px 12px alpha(from var(--brand) / 25%);
A shadow in the brand colour at 25% opacity.

palette-mix()

Mixes two font palettes for colour fonts, for use in font-palette. It works like color-mix(), blending each pair of matching palette entries, and lets a transition move smoothly from one palette to another.

Syntaxpalette-mix( <color-interpolation-method>? , [ <'font-palette'> && <percentage [0,100]>? ]# )
ReturnsA font palette, as a value of font-palette
SupportChrome 121 · Firefox no · Safari no
Speccss-fonts-4

dynamic-range-limit-mix()

Mixes values of dynamic-range-limit by percentage, giving a limit on HDR brightness between those keywords. It also lets that limit be transitioned.

Syntaxdynamic-range-limit-mix( [ <'dynamic-range-limit'> && <percentage [0,100]> ]# )
ReturnsA value of dynamic-range-limit
SupportChrome 136 · Firefox no · Safari no
Speccss-color-hdr-1