Cheat sheet #5

Colours in CSS

A cheat sheet for web design students. Learn the modern ways to write a colour value, starting with hexadecimal, the everyday default, and find out how to make colours partly transparent with an alpha channel.

The short version

Use hex by default. Reach for oklch() when you want to build or adjust a palette, and only add an alpha value when you need transparency.

color: #14213d; background-color: #2a5bd780; /* hex with alpha */ border-color: oklch(70% 0.15 250); /* perceptual */
On this cheat sheet

Hexadecimal: the default

Six-digit hex

A hex colour is a # followed by six hexadecimal digits, written as three pairs representing red, green and blue. Hexadecimal counts in base 16, so it uses the digits 0–9 plus the letters a–f (a = 10, b = 11, c = 12, d = 13, e = 14, f = 15).

#2a5bd7  ↑     ↑     ↑ red  green  blue

Each pair runs from 00 (none of that colour) to ff (the maximum). In decimal, that’s 0 to 255, so each channel has 256 levels. For example, the pair 2a equals 2 × 16 + 10 = 42.

ColourHexHow to read it
Black#000000No red, no green, no blue
White#ffffffMaximum of all three
Red#ff0000Full red only
Bright green#00ff00Full green only
Blue#0000ffFull blue only
Mid grey#808080Equal amounts of each. Whenever all three pairs match, you get a grey
Royal blue#2a5bd7A mix: 42 red, 91 green, 215 blue
Pros
  • It’s short, compact and understood everywhere.
  • It’s what design tools and browser dev tools give you by default.
  • It’s easy to copy and paste, and easy to compare.
Cons
  • It’s hard to read. Nothing about #2a5bd7 tells you that it’s a blue.
  • It’s hard to adjust by hand when you want a colour to be lighter or darker.
  • It’s limited to the sRGB range of colours.

Three-digit shorthand

When each of the three pairs is made of two identical digits, you can write just one digit from each pair, and the browser will expand it by repeating each digit.

ShorthandExpands toColour
#f00#ff0000Red
#09c#0099ccCyan-blue
#fc0#ffcc00Gold
#333#333333Dark grey
#fff#ffffffWhite
Can be shortened#ffffff → #fff #aa00cc → #a0c #112233 → #123
Can’t be shortened#2a5bd7 (2 and a differ) #ff0001 (the last pair differs) #f8f8f7 (not repeated pairs)

Shorthand is just a convenience. It doesn’t give you a different colour, so only use it where the six-digit version really does have repeated pairs.

Hex with alpha: eight and four digits

Add a fourth pair to set the transparency, known as the alpha channel. A value of 00 is fully transparent, and ff is fully opaque (the same as leaving it off).

#2a5bd780 8 digits: #rrggbbaa (here 80 ≈ 50% opaque) #25d8 4 digits: #rgba (shorthand of #2255dd88)

The four-digit form follows the same rule as the three-digit form, so each digit is doubled, including the alpha digit.

OpacityAlpha pairExample on #2a5bd7Result
100%ff#2a5bd7ff
80%cc#2a5bd7cc
60%99#2a5bd799
50%80#2a5bd780
40%66#2a5bd766
20%33#2a5bd733
10%1a#2a5bd71a
0%00#2a5bd700

The alpha pair is the opacity percentage × 255, written in hex. So, 50% is about 127.5, which is usually rounded to 80. Because you can’t read that at a glance, many designers prefer to use the slash syntax (section 5) when they need an exact percentage.

Hex rules

RGB

The rgb() function

RGB uses the same red, green and blue channels as hex, but the values are written in decimal, which is easier to read. Note that the modern syntax separates the values with spaces, not commas.

rgb(42 91 215) /* same as #2a5bd7 */ rgb(0 0 0) /* black */ rgb(100% 50% 0%) /* percentages work too: orange */ rgb(42 91 215 / 50%) /* with alpha */
Dorgb(42 91 215) rgb(42 91 215 / 0.5) rgb(42 91 215 / 50%)
Don’t (old comma style)rgb(42, 91, 215) rgb(42 91 215 / 50) rgb(42, 91, 215 / 50%)
Pros
  • The numbers are readable, and they match what tools such as image editors show you.
  • It’s easy to add a clear alpha value.
Cons
  • It’s still hard to tell what colour you’ll get.
  • A lighter or darker version means changing all three numbers.

OKLCH

The oklch() function

OKLCH describes a colour the way people perceive it. It uses three values: Lightness, Chroma (how vivid the colour is) and Hue (the colour itself, as an angle on a colour wheel).

oklch(70% 0.15 250) /* a mid-light blue */ oklch(70% 0.15 250 / 50%) /* with alpha */ oklch(0.7 0.15 250) /* lightness as a number: same colour */
ValueRangeMeaning
L lightness0%–100% (or 0–1)0% is black, 100% is white
C chroma0 to about 0.40 is a pure grey. Higher is more vivid. The maximum depends on the hue and lightness.
H hue0–360 (degrees)The position on the colour wheel

Hue wheel (at L 70%, C 0.15), from 0 to 330 in steps of 30:

Roughly: 20–40 red, 60–90 orange to yellow, 140–160 green, 190–220 teal to cyan, 250–270 blue, 300–330 purple to pink.

Why OKLCH is useful

Equal lightness looks equally light. Compare these 12 hues in HSL, each with the same lightness value, with the same 12 hues in OKLCH:

hsl(H 100% 50%): yellows and greens look much brighter than blues and purples.

oklch(70% 0.15 H): the lightness looks consistent across hues.

Building a palette is predictable. If you keep the hue and chroma the same and change only the lightness, you’ll get a ramp of tints and shades:

oklch(L 0.15 250), with L from 95% down to 25%.

Pros
  • It’s predictable. Change one value to change just the lightness, the vividness or the hue.
  • Colours have a consistent perceived lightness, and that helps with accessible contrast.
  • It can describe colours that are more vivid than sRGB allows, on screens that support them.
Cons
  • It’s less familiar, and design tools often still give you hex.
  • Some combinations of values are outside what a screen can display, so the browser quietly adjusts them.
  • It’s supported in all current browsers, but not in older ones.

For more information about the OKLCH colour model, see Explore color as we see it.

Other methods

HSL

HSL stands for hue, saturation and lightness. It’s easier to reason about than RGB, and it’s the readable cousin of OKLCH, although its lightness is not perceptually even (see the strips above).

hsl(220 70% 50%) /* hue 0-360, saturation %, lightness % */ hsl(220 70% 50% / 50%) /* with alpha */ hsl(220 70% 85%) /* a lighter version: change only L */

Saturation and lightness need the % sign, but hue is a plain number of degrees.

Named colours

CSS has 148 colour names. They’re quick to type and handy for quick tests, but you can’t fine-tune them, and they limit you to a fixed set of colours.

NameSwatchNameSwatchNameSwatch
tomatoseagreensteelblue
goldenrodrebeccapurpleslategray

There are also two special keywords. The first, transparent, is fully see-through. The second, currentcolor, is the element’s current text colour, which is handy for borders and icons. Note that named colours can’t take an alpha value directly.

Other modern functions you may meet

SyntaxWhat it does
hwb(220 10% 20%)Hue, whiteness, blackness. Think “pick a hue, then add white and black”.
oklab(70% -0.05 -0.1), lab(), lch()Related perceptual colour spaces. OKLCH is the most used.
color(display-p3 1 0.4 0)Specifies a colour in a named colour space, such as the wider-gamut Display P3.
color-mix(in oklch, red 70%, white)Mixes two colours in the colour space you choose.
oklch(from #2a5bd7 calc(l + 0.2) c h)Relative colour syntax: derive a new colour from an existing one.

Alpha channels and opacity

Alpha in every method

Alpha controls how see-through a colour is. A value of 1 or 100% is fully opaque, and 0 or 0% is fully transparent. Anything in between lets the content behind show through.

MethodSyntax50% example
Hex (8 digits)#rrggbbaa#2a5bd780
Hex (4 digits)#rgba#25d8
RGBrgb(r g b / alpha)rgb(42 91 215 / 50%)
HSLhsl(h s l / alpha)hsl(220 70% 50% / 50%)
OKLCHoklch(l c h / alpha)oklch(70% 0.15 250 / 50%)
Named colourNo alpha syntaxUse color-mix() or convert to another method

The swatches sit on a checkerboard so that you can see the transparency. Alpha can be written as a number from 0 to 1 (0.5) or as a percentage (50%), and either works after the slash.

Alpha is not the same as the opacity property

An alpha value makes only that colour transparent, whereas the opacity property fades the whole element, including its text, its borders and any children.

/* Only the background is see-through; text stays sharp */ .panel { background-color: rgb(42 91 215 / 50%); } /* The entire element, text included, fades */ .panel { background-color: #2a5bd7; opacity: 0.5; }

Remember that reducing alpha also reduces the contrast with whatever is behind, so always check that your text is still readable.

Which one should I use?

MethodLooks likeAlphaUse it for
Hex (default)#2a5bd7#2a5bd780Everyday colour values, and colours from design tools
RGBrgb(42 91 215)/ 50%When you want readable numbers, or a clear alpha percentage
OKLCHoklch(70% 0.15 250)/ 50%Building palettes, tints and shades, and vivid colours
HSLhsl(220 70% 50%)/ 50%Simple adjustments when you’re already comfortable with it
NamedtomatoNoneQuick tests and demos

Common mistakes

Invalid colour values are ignored

If the browser can’t read a colour value, it throws away the whole declaration, and nothing changes. So, if a colour mysteriously fails to appear, check the value first. It’s a good idea to use a reliable CSS colour picker to make sure your colour values use the correct syntax.

Do#2a5bd7 #2a5bd780 rgb(42 91 215 / 50%) hsl(220 70% 50%)
Don’t2a5bd7 (no #) #2a5bd (5 digits) #2a5bg7 (g isn't hex) hsl(220 70 50) (needs % signs)

Choose readable colour combinations

Text and its background need enough contrast. The WCAG AA guidelines ask for a contrast ratio of at least 4.5:1 for normal text, and 3:1 for large text. Use a contrast checker, such as the one built into your browser’s dev tools, and never rely on colour alone to convey meaning.

Check before you upload

Learn more

Tip: in your browser’s dev tools, click any colour swatch in the Styles panel. You can then switch the value between hex, RGB, HSL and OKLCH, and see its contrast ratio.