Favorites

CSS Color Formats

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
CSS Color Formats

Why colour formats matter

Colour is one of the first things people notice about a website. In CSS you can colour almost anything: text, backgrounds, borders, shadows, underlines and more. A colour value is simply the part of a rule that says which colour to use.

p {
  color: yellow;            /* text colour */
  background-color: navy;   /* background colour */
  border: 2px solid gray;   /* border colour is part of the shorthand */
}

Here's the interesting part: CSS lets you write the same colour in many different ways. That isn't just to confuse beginners! Each colour format is good at something different, such as being short to type, easy to adjust, or able to show the extra-vivid colours that modern screens can display.

To keep things easy to compare, most examples in this post use the same colour, plain yellow. Every format below produces exactly this:

Results

Spelling note: CSS uses American spelling, so the property is always color, never colour. In this post we write "colour" in normal text, but the code must say color or the browser will ignore it.

Colour names and keywords

The simplest way to set a colour is by name. CSS understands 148 named colours, from everyday ones like red and yellow to more poetic ones like papayawhip and cornflowerblue.

p { background-color: yellow; }

Names are case-insensitive, so Yellow and YELLOW work too. They're great for quick experiments and teaching, but the choice is limited: there are millions of colours and only 148 names.

Results
tomato
gold
seagreen
cornflowerblue
rebeccapurple

Fun fact: rebeccapurple was added in 2014 in memory of Rebecca Meyer, the young daughter of well-known CSS author Eric Meyer. It's the newest named colour, which is why older articles (including the first version of this one) mention only 147.

Special keywords

A few keywords don't name a fixed colour but are just as handy:

  • transparent is completely see-through. It's useful for hiding a border while keeping its space.
  • currentColor means "whatever the text colour is". We'll see why that's so useful later on.

Our site has a full list of every name with its hex and RGB values on the CSS Color Names page.

Hexadecimal

Hexadecimal (or "hex") colours are the format you'll see most often in real stylesheets and design tools. A hex colour starts with a hash sign (#), followed by six characters:

p { background-color: #FFFF00; }

How to read a hex code

The six characters are three pairs, written #RRGGBB. The first pair is the amount of red, the second is green and the third is blue. Mixing light in these three colours on screen can produce every other colour.

"Hexadecimal" means counting in base 16. Instead of the digits 0 to 9, each character can be 0 to 9 then A to F, where A means 10 and F means 15. So each pair runs from 00 (none of that colour) to FF (the maximum, equal to 255).

Hex codeRedGreenBlueResult
#FF0000FullNoneNoneRed
#FFFF00FullFullNoneYellow
#000000NoneNoneNoneBlack
#FFFFFFFullFullFullWhite
#808080HalfHalfHalfGrey

Red plus green making yellow might surprise you if you learnt colour mixing with paint. Screens mix light, not pigment, so the rules are different: add all three at full strength and you get white.

The three-digit shortcut

When each pair is made of two identical characters, you can write just one of each. The browser doubles them back up, so #FF0 means #FFFF00.

p { background-color: #FFFF00; }
p { background-color: #FF0; }    /* exactly the same yellow */

Adding transparency to hex

Modern browsers also accept an extra pair at the end for alpha, which is how opaque (solid) the colour is. FF is fully solid and 00 is fully see-through. There's a short four-character version too.

p { background-color: #FFFF0080; }  /* yellow, about 50% see-through */
p { background-color: #FF08; }      /* short form of #FFFF0088 */

Tip: hex is case-insensitive, so #ffff00 and #FFFF00 are identical. Pick one style and stick with it so your stylesheet looks tidy.

RGB

RGB stands for red, green and blue. It describes the same mix of light as hex, but uses ordinary numbers from 0 to 255 instead of hexadecimal pairs. Many people find it easier to read.

p { background-color: rgb(255 255 0); }     /* yellow */
p { background-color: rgb(100% 100% 0%); }  /* also yellow, using percentages */

Adding transparency with a slash

To make a colour partly see-through, add a slash followed by the alpha value. Alpha can be any number from 0 (invisible) to 1 (solid), or a percentage from 0% to 100%.

p { background-color: rgb(255 255 0 / 0.5); }  /* half see-through */
p { background-color: rgb(255 255 0 / 50%); }  /* the same thing */
Results
alpha 1
alpha 0.6
alpha 0.3
alpha 0.1

What about rgba()?

Older tutorials use commas between the values, plus a separate rgba() function for transparency. Both still work, and you'll see them in plenty of existing code:

/* Older comma style: still valid */
p { background-color: rgb(255, 255, 0); }
p { background-color: rgba(255, 255, 0, 0.5); }

/* Modern space style: shorter and consistent */
p { background-color: rgb(255 255 0 / 0.5); }

Today rgb() and rgba() are simply two names for the same function, so rgb() can handle transparency on its own. The space-separated style is the modern standard and matches all the newer colour functions.

Correction: the original version of this post said alpha runs from 0.1 to 1. It actually starts at 0, which makes the colour completely invisible.

HSL and HWB

Hex and RGB describe colour the way a screen makes it. HSL describes colour the way people tend to think about it, using three ideas:

  • Hue is the basic colour, given as an angle around a colour wheel from 0 to 360 degrees.
  • Saturation is how vivid the colour is: 100% is full strength and 0% is plain grey.
  • Lightness is how light or dark it is: 0% is black, 100% is white and 50% is the "pure" colour.
p { background-color: hsl(60 100% 50%); }        /* yellow */
p { background-color: hsl(60deg 100% 50% / 0.5); } /* half see-through yellow */

Finding your way around the wheel

HueColour
0 (or 360)Red
60Yellow
120Green
180Cyan
240Blue
300Magenta

Why HSL is handy

HSL's big strength is making variations. Keep the hue and saturation the same, change only the lightness, and you get a matching set of shades for buttons, borders and hover states:

.button       { background-color: hsl(220 80% 45%); }
.button:hover { background-color: hsl(220 80% 35%); }  /* darker on hover */
.badge        { background-color: hsl(220 80% 90%); }  /* pale tint */
Results
90%
70%
45%
35%
20%

As with RGB, the older comma style (hsl(60, 100%, 50%)) and hsla() still work, but the space-separated version is now preferred.

HWB: an even simpler cousin

HWB stands for hue, whiteness and blackness. You pick a hue, then say how much white and black to mix in, much like mixing paint. It's a friendly format for beginners and is supported in all modern browsers.

p { background-color: hwb(60 0% 0%); }    /* pure yellow */
p { background-color: hwb(60 40% 0%); }   /* yellow with white mixed in */

Modern formats: OKLCH and friends

HSL has a hidden flaw. Its "lightness" isn't how light a colour actually looks to the human eye. Yellow and blue at the same HSL lightness look completely different in brightness:

Results
hsl(60 100% 50%)
hsl(240 100% 50%)

Both have "50% lightness", yet the yellow looks far brighter. That makes HSL unreliable when you need a set of colours to feel balanced, or when you need to keep text readable.

Meet OKLCH

OKLCH fixes this. It's what's called a perceptual colour format, designed around how human eyes actually see colour. Its three values are:

  • L for lightness, from 0% (black) to 100% (white), matching how bright the colour really looks.
  • C for chroma, which is how colourful it is. 0 is grey, and most colours sit between 0 and about 0.37.
  • H for hue, an angle from 0 to 360, just like HSL.
p { background-color: oklch(96.8% 0.211 109.8); }  /* our yellow */

The lightness of 96.8% tells you straight away that this yellow is very bright. Plain blue, #1d4ed8, comes out at about 49%, which matches what your eyes see.

Because lightness is reliable, you can build a set of colours with different hues and the same lightness, and they'll genuinely look equally bright:

.tag-red   { background-color: oklch(70% 0.15 25); }
.tag-green { background-color: oklch(70% 0.15 145); }
.tag-blue  { background-color: oklch(70% 0.15 255); }
Results
hue 25
hue 145
hue 255

Brighter than hex can go

Most modern phones and laptops can show colours more vivid than hex and RGB can describe. The range of colours a format can reach is called its gamut. Hex, RGB and HSL are limited to the older sRGB gamut; OKLCH can reach the wider gamut of newer screens.

If you ask for a colour a screen can't show, the browser picks the closest one it can. So you can safely use vivid OKLCH colours: they'll pop on modern screens and still look fine on older ones.

The rest of the family

FormatWhat it's for
oklch()Perceptual lightness, chroma and hue. The best all-rounder for design systems.
oklab()The same colour space as OKLCH, using a and b axes instead of chroma and hue. Mainly used for mixing.
lch() and lab()Older perceptual formats. OKLCH and OKLab are their improved versions.
color()Names a specific colour space directly, e.g. color(display-p3 1 1 0) for a wide-gamut yellow.

Transparency, mixing and other tricks

Alpha versus opacity

There are two ways to make something see-through, and they behave differently. An alpha value on a colour fades only that colour. The opacity property fades the whole element, including its text and everything inside it.

.box-a { background-color: rgb(29 78 216 / 0.4); }  /* only the background fades */
.box-b { background-color: rgb(29 78 216); opacity: 0.4; }  /* everything fades */
Results
Alpha: text stays solid
Opacity: text fades too

Want to experiment? Our RGBA Opacity Generator lets you see how different alpha levels look.

currentColor

currentColor borrows the element's text colour. Use it for borders, icons and shadows, and they'll automatically change whenever the text colour changes:

.alert {
  color: #b91c1c;
  border: 2px solid currentColor;  /* matches the text, no need to repeat it */
}

Storing colours in custom properties

A custom property (often called a CSS variable) lets you name a colour once and reuse it everywhere. Change it in one spot and your whole site updates:

:root {
  --brand: oklch(50% 0.2 264);
}
.button { background-color: var(--brand); }
a       { color: var(--brand); }

Mixing colours with color-mix()

The color-mix() function blends two colours together. You say which colour space to mix in, the two colours, and how much of the first to use. Mixing with white or black is a quick way to make tints and shades:

.light { background-color: color-mix(in oklch, #1d4ed8 30%, white); }
.mid   { background-color: color-mix(in oklch, #1d4ed8 70%, white); }
.dark  { background-color: color-mix(in oklch, #1d4ed8 70%, black); }
Results
30% + white
70% + white
original
70% + black

Adjusting a colour with relative colour syntax

Relative colour syntax takes an existing colour, breaks it into parts, and lets you change just one. The from keyword says which colour to start with, and l, c and h stand for its current lightness, chroma and hue:

.button:hover {
  /* same colour as --brand, but a bit lighter */
  background-color: oklch(from var(--brand) calc(l + 0.1) c h);
}

Light and dark mode with light-dark()

Many visitors set their device to dark mode. The light-dark() function takes two colours and uses the first in light mode and the second in dark mode. It only works when the page says it supports both, using color-scheme:

:root {
  color-scheme: light dark;
}
body {
  background-color: light-dark(#ffffff, #111827);
  color: light-dark(#1f2937, #f3f4f6);
}

Good to know: oklch(), color-mix(), relative colour syntax and light-dark() all work in current versions of Chrome, Edge, Firefox and Safari. If you need to support much older browsers, provide a hex fallback on the line before, and older browsers will use that instead.

Which format should you use?

Every format can produce the colours most websites need, so there's no single right answer. Here's a quick guide:

FormatBest forExample (yellow)
NameQuick tests and learningyellow
HexCopying colours from design tools; compact code#FFFF00
RGBReadable numbers, adding transparencyrgb(255 255 0)
HSLQuickly making lighter and darker shadeshsl(60 100% 50%)
OKLCHBalanced palettes, design systems, vivid coloursoklch(96.8% 0.211 109.8)

A good modern approach is to keep using hex wherever you copy colours from a design tool, and reach for OKLCH and color-mix() when you're building a palette or need shades of a colour. Whatever you pick, store your main colours in custom properties so they're easy to change.

Need to switch a colour from one format to another? Our CSS Color Converter will do the maths for you.

Don't forget contrast

The format doesn't matter if people can't read your text. Contrast is the difference in brightness between text and its background. The international accessibility guidelines (known as WCAG) recommend a ratio of at least 4.5 to 1 for normal text and 3 to 1 for large text.

Tip: our yellow example is a perfect warning. Yellow text on a white background has a contrast ratio of about 1.07 to 1, which makes it almost invisible. Use yellow as a background or highlight, with dark text on top. Most browser developer tools show the contrast ratio when you inspect a piece of text.

Wrapping up

You can now read every common way of writing a CSS colour: names, hex, RGB, HSL, HWB and the modern OKLCH. You also know how to add transparency, reuse colours with custom properties, and mix and adjust them right in your stylesheet.

Try converting one of your own brand colours to OKLCH, then use color-mix() to build a few tints. It's the quickest way to see why the modern formats are worth learning.

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee