Colour is the first thing people notice about a website. A bank's site feels trustworthy in deep blue, a food delivery app feels energetic in red and orange, and a badly chosen grey-on-grey paragraph feels impossible to read. CSS gives you many ways to write a colour: names like red, hex codes like #ff0000, functions like rgb(), hsl() and the modern oklch().
In this chapter you will learn every common colour format, how to convert between them by hand, how transparency works, the modern colour functions such as color-mix() and light-dark(), and how to choose colours that everyone can read.
Where Colours Are Used
Many properties accept a colour value. The most common ones:
| Property | What it colours |
|---|---|
color | The text (foreground) colour. It is inherited. |
background-color | The background of the element's box. Not inherited. |
border-color | The border (covered in Borders & Outlines). |
outline-color | The focus outline. |
text-decoration-color | Underlines and line-throughs. |
caret-color | The blinking text cursor in inputs. |
accent-color | Checkboxes, radio buttons, range sliders and progress bars. |
body {
color: #1f2937; /* dark grey text */
background-color: #f9fafb; /* off-white page */
}
input[type="checkbox"] {
accent-color: #2563eb; /* blue checkboxes */
}1. Named Colours
CSS defines over 140 colour keywords, such as red, navy, tomato, teal, gold and rebeccapurple. They are case-insensitive, so Red and RED also work.
h1 { color: tomato; }
p { color: darkslategray; }Named colours are handy for quick experiments, but real designs need exact shades, so you will mostly use the numeric formats below. Two special keywords are worth remembering:
transparent: fully see-through. It is the initial value ofbackground-color.currentColor: "whatever thecolorproperty of this element is". We will see it in action later.
A small trap: darkgray (#a9a9a9) is actually lighter than gray (#808080). The names come from old X11 colour lists and are not always logical.
2. Hexadecimal Colours
Screens build every colour by mixing red, green and blue light. Each channel goes from 0 (off) to 255 (full). A hex colour writes those three numbers in base 16:
#RRGGBB
│ │ └── blue (00 to ff)
│ └──── green (00 to ff)
└────── red (00 to ff)Each pair is a two-digit hexadecimal number. Hex digits are 0 to 9 followed by a (10) to f (15).
Worked example: converting #ff6347 to decimal
ff → 15 × 16 + 15 = 255 (red)
63 → 6 × 16 + 3 = 99 (green)
47 → 4 × 16 + 7 = 71 (blue)
#ff6347 = rgb(255, 99, 71)That is exactly the named colour tomato; Chrome reports tomato as rgb(255, 99, 71).
Worked example: decimal to hex
Convert rgb(30, 64, 175). Divide each by 16; the quotient is the first digit and the remainder the second:
30 ÷ 16 = 1 remainder 14 → 1e
64 ÷ 16 = 4 remainder 0 → 40
175 ÷ 16 = 10 remainder 15 → af
rgb(30, 64, 175) = #1e40afShort hex and alpha hex
| Form | Example | Meaning |
|---|---|---|
#RGB | #f80 | Each digit is doubled: #ff8800 |
#RRGGBB | #ff8800 | Full form |
#RGBA | #f808 | #ff880088 |
#RRGGBBAA | #00000080 | Last pair is alpha: 80 hex = 128, and 128/255 ≈ 0.5, so 50% black |
The short form can only express colours where both digits of each pair are equal, so #1e40af has no short form. Hex is case-insensitive: #FF8800 equals #ff8800.
Some colours to know by heart:
#000000 black #ffffff white
#ff0000 red #00ff00 lime (pure green light)
#0000ff blue #808080 gray
#ffff00 yellow (red + green light make yellow)Note that the keyword green is #008000, a darker shade; pure #00ff00 is called lime.
3. The rgb() Function
rgb() writes the same three channels in decimal, which is easier to read and to change by hand:
.a { color: rgb(255 99 71); } /* modern, space-separated */
.b { color: rgb(255 99 71 / 0.5); } /* with 50% alpha */
.c { color: rgb(100% 39% 28%); } /* percentages also allowed */
.d { color: rgb(255, 99, 71); } /* legacy comma syntax, still valid */
.e { color: rgba(255, 99, 71, 0.5); } /* legacy rgba() */Modern CSS uses spaces between channels and a slash before the alpha. The older comma form and the rgba() name still work everywhere and you will see them in a lot of existing code. Today rgba() is simply an alias of rgb(); both accept an optional alpha.
The alpha value goes from 0 (invisible) to 1 (solid), or 0% to 100%.
4. The hsl() Function
RGB matches how screens work, but it is not how humans think. If a designer says "make this blue a bit lighter", which RGB numbers do you change? HSL describes a colour the way people talk about it:
Hue: the position on the colour wheel, in degrees from 0 to 360.
Saturation: how vivid it is. 0% is grey, 100% is full colour.
Lightness: 0% is black, 100% is white, 50% is the "pure" colour.
Hue wheel (degrees)
0 / 360 red
60 yellow
120 green
180 cyan
240 blue
300 magenta.dark { background-color: hsl(220 60% 30%); }
.normal { background-color: hsl(220 60% 50%); }
.light { background-color: hsl(220 60% 70%); }Rendered in Chrome these give a navy, a medium blue and a soft sky blue: the same hue, only lightness changes. This is why HSL is popular for building a palette of shades from one brand colour.
Worked example: hsl(120 100% 25%) to RGB
The standard conversion for this simple case:
Chroma C = (1 − |2L − 1|) × S = (1 − |0.5 − 1|) × 1 = 0.5
Hue 120° is pure green, so the colour is (0, C, 0) shifted by m
m = L − C/2 = 0.25 − 0.25 = 0
RGB = (0, 0.5, 0) × 255 = (0, 127.5, 0) → rgb(0, 128, 0)That is #008000, the CSS keyword green, and Chrome computes exactly rgb(0, 128, 0).
Like rgb(), hsl() accepts a slash alpha (hsl(220 60% 50% / 0.3)), and hsla() is a legacy alias.
The weakness of HSL
HSL's "lightness" is mathematical, not perceptual. hsl(60 100% 50%) (yellow) and hsl(240 100% 50%) (blue) have the same lightness value, yet yellow looks far brighter to the eye than blue. So if you build a palette by changing only the hue in HSL, the colours end up with very uneven visual weight and uneven text contrast. That problem led to the newer colour spaces below.
5. hwb()
hwb() means Hue, Whiteness, Blackness: pick a hue, then mix in white and black, like a painter. hwb(0 0% 0%) is pure red; hwb(0 40% 0%) is a pink. It is less common in practice but you may see it in colour pickers.
6. Modern Perceptual Colours: oklch() and oklab()
CSS Color Level 4 added colour spaces designed around human vision: lab(), lch(), and their improved versions oklab() and oklch(). All of these have been supported in every major browser since 2023.
oklch() is the one most designers now recommend:
.brand { color: oklch(0.6 0.2 240); }
/* │ │ └── H: hue angle, 0 to 360
│ └────── C: chroma (colourfulness), 0 up to about 0.4
└────────── L: perceived lightness, 0 (black) to 1 (white) */Why it matters:
Perceptually uniform lightness.
oklch(0.5 0.15 30),oklch(0.5 0.15 140)andoklch(0.5 0.15 260)(a red, a green and a blue) render with very similar visual brightness. In HSL that is not the case.Predictable shades. To make a darker version, lower L. The hue does not drift the way it can in HSL.
Wider gamut. It can describe colours beyond the old sRGB range, which modern phone and laptop screens (Display P3) can show. On older screens the browser maps them to the nearest displayable colour.
For fixed colours that must work in very old browsers, you can provide a fallback first, using the "last valid declaration wins" rule from the previous chapter:
.brand {
color: #2f6fd6; /* fallback */
color: oklch(0.58 0.17 258); /* used where supported */
}There is also a general color() function for named colour spaces, for example color(display-p3 1 0 0) for the most vivid red a P3 screen can show.
Transparency: Alpha vs opacity
There are two ways to make something see-through, and they behave very differently.
.box-opacity { background-color: rgb(30 64 175); opacity: 0.5; }
.box-alpha { background-color: rgb(30 64 175 / 0.5); }<div class="box-opacity">Some black text</div>
<div class="box-alpha">Some black text</div>In the rendered page both backgrounds look equally faded, but in the opacity box the text is faded too (the black text turns grey), while in the alpha box the text stays crisp and solid black.
| Alpha in the colour | opacity property | |
|---|---|---|
| Affects | Only the property it's used in (e.g. just the background) | The whole element: background, border, text, images and all children |
| Can a child undo it? | Not needed | No. A child with opacity: 1 is still inside a faded parent |
| Typical use | Translucent overlays, tinted backgrounds | Fading a whole component in or out |
currentColor
currentColor means "the value of color on this element". It lets other properties follow the text colour automatically.
.alert {
color: #cc0000;
border: 3px solid currentColor; /* border becomes #cc0000 too */
}
.alert.success {
color: #15803d; /* border follows automatically */
}Chrome computes the border colour of .alert as rgb(204, 0, 0), the same as the text. Change only color and the border follows. Many properties (border colour, text-decoration colour, SVG icons with fill: currentColor) already default to the current colour, which is why underlines match the link colour.
Mixing Colours: color-mix()
color-mix() blends two colours in a chosen colour space. It has been supported in all major browsers since 2023.
.a { background-color: color-mix(in srgb, blue, white); } /* 50% each */
.b { background-color: color-mix(in oklab, blue, white); }
.c { background-color: color-mix(in srgb, red 25%, blue); } /* 25% red, 75% blue */Results in Chrome:
.a → color(srgb 0.5 0.5 1) a lavender, slightly purple-looking
.b → oklab(0.726 -0.016 -0.156) a cleaner, lighter sky blue
.c → color(srgb 0.25 0 0.75) = rgb(64 0 191), a violetNotice how mixing blue and white in sRGB gives a purplish tint, while mixing in oklab stays a natural light blue. The colour space you mix in really does change the result. The newest version of the specification (CSS Color Level 5) lets you omit the in ... part, in which case oklab is used, but older browser versions treat a missing colour space as an error. Always write it explicitly: it is clearer and works everywhere.
A very practical use is creating hover and border shades from one brand colour:
.btn { background-color: #2563eb; }
.btn:hover { background-color: color-mix(in oklab, #2563eb, black 15%); }Relative Colours
Relative colour syntax lets you take an existing colour, break it into channels, and change some of them. It became available in all major browsers in 2024.
/* Same colour, 50% transparent */
.overlay { background-color: rgb(from #3366cc r g b / 0.5); }
/* Same hue and saturation, 15 points darker */
.btn:hover { background-color: hsl(from #3366cc h s calc(l - 15)); }After from you give the origin colour, and the letters r g b (or h s l, l c h, etc.) stand for its channels. Here #3366cc is hsl(220 60% 50%), so the hover colour is hsl(220 60% 35%). Chrome computed it as color(srgb 0.14 0.28 0.56), which is rgb(36 71 143), a darker blue. In the chapter CSS Variables (Custom Properties) you will see how powerful this becomes when the origin colour is a variable.
Light and Dark Themes: light-dark()
Many users set their phone or laptop to dark mode. CSS can follow that preference with light-dark(), available in all major browsers since May 2024:
:root {
color-scheme: light dark; /* required: tells the browser both schemes are supported */
}
body {
color: light-dark(#111827, #e5e7eb); /* light value, dark value */
background-color: light-dark(#ffffff, #111827);
}The first value is used in light mode, the second in dark mode. light-dark() only works when color-scheme allows both schemes; without that line the browser always uses the first value. Setting color-scheme also makes the browser's own scrollbars and form controls switch to dark styles. When we ran this in Chrome with the system set to dark mode, the text colour computed to rgb(229, 231, 235) (the dark-mode value #e5e7eb); without the color-scheme line it stayed rgb(17, 24, 39) (#111827), the light value.
The other way to do theming, the prefers-color-scheme media query, is covered in Responsive Design & Media Queries.
Accessibility: Colour Contrast
A beautiful light-grey paragraph is useless if people can't read it. The Web Content Accessibility Guidelines (WCAG) define a contrast ratio between text and its background:
contrast ratio = (L1 + 0.05) / (L2 + 0.05)
L1 = relative luminance of the lighter colour (0 = black, 1 = white)
L2 = relative luminance of the darker colour
Range: 1:1 (no contrast) to 21:1 (black on white)| WCAG level | Normal text | Large text (about 24px, or 18.66px bold) |
|---|---|---|
| AA (the usual target) | at least 4.5 : 1 | at least 3 : 1 |
| AAA (stricter) | at least 7 : 1 | at least 4.5 : 1 |
Some ratios against a white background, calculated with the WCAG formula:
#000000 on #ffffff → 21.00 : 1 passes everything
#3366cc on #ffffff → 5.37 : 1 passes AA
#767676 on #ffffff → 4.54 : 1 just passes AA (the lightest grey that does)
#999999 on #ffffff → 2.85 : 1 fails AA for normal textAlso, never use colour as the only way to show information. "Fields in red are required" fails for colour-blind users (about 1 in 12 men has some form of colour vision deficiency). Add an icon, text or underline as well. Browser DevTools show the contrast ratio when you click a colour in the Styles panel.
Which Format Should I Use?
| Format | Good for | Drawback |
|---|---|---|
| Named | Quick demos, transparent, currentColor | Limited, inconsistent names |
| Hex | Copying from design tools, compact | Hard to tweak by hand |
rgb() | Readable channels, adding alpha | Not intuitive for "lighter/darker" |
hsl() | Easy shades of one hue | Lightness is not perceptually even |
oklch() | Design systems, consistent palettes, wide-gamut colours | Newer; old browsers (pre-2023) need a fallback |
Common Mistakes
Forgetting the
#.color: ff0000;is invalid and ignored.Mixing syntaxes.
rgb(255, 0, 0 / 0.5)(commas plus slash) is invalid. Use eitherrgb(255 0 0 / 0.5)orrgba(255, 0, 0, 0.5).Using
opacityto fade only a background. It fades the text and children too; use an alpha colour instead.Expecting
background-colorto be inherited. It isn't; children are transparent by default so you see the parent through them.Using
light-dark()withoutcolor-scheme: light dark. Only the light value will ever be used.Low-contrast grey text. Check the ratio; aim for at least 4.5:1.
Hue values with a percent sign. Hue is an angle:
hsl(220 60% 50%), nothsl(220% ...).
Interview / Exam Questions
1. Convert #4CAF50 to rgb().
4C = 4×16 + 12 = 76; AF = 10×16 + 15 = 175; 50 = 5×16 + 0 = 80. So rgb(76 175 80).
2. What does #0f08 expand to, and how transparent is it?
Each digit doubles: #00ff0088. Alpha 88 hex = 136, and 136/255 ≈ 0.53, so it is lime green at about 53% opacity.
3. Difference between opacity: 0.5 and background-color: rgb(0 0 0 / 0.5)?
opacity makes the entire element including text and children semi-transparent. The alpha colour makes only the background semi-transparent; text stays fully opaque.
4. Why might a designer prefer oklch() over hsl()?
oklch() is perceptually uniform: equal lightness values look equally bright across hues, so palettes and contrast are predictable. It can also express wide-gamut (Display P3) colours. HSL's lightness is mathematical, so yellow and blue at 50% look very different in brightness.
5. What is currentColor?
A keyword that resolves to the element's computed color value. It lets borders, shadows or SVG fills automatically match the text colour.
6. What minimum contrast ratio does WCAG AA require for normal body text?
4.5 : 1 (and 3 : 1 for large text).
Summary
Colours can be written as names, hex (
#rgb,#rrggbb, with optional alpha),rgb(),hsl(),hwb(),lab()/lch(),oklab()/oklch()andcolor().Hex pairs are base-16 numbers from 00 to ff (0 to 255) for red, green and blue.
Modern syntax uses spaces and a slash for alpha:
rgb(255 0 0 / 0.5); the comma syntax andrgba()/hsla()still work.HSL is intuitive but not perceptually uniform;
oklch()fixes this and supports wide-gamut colours.Alpha in a colour affects one property;
opacityfades the whole element and its children.currentColor,color-mix(), relative colours andlight-dark()let you derive colours instead of hard-coding them.Always check contrast: at least 4.5:1 for normal text.
Next, in CSS Units, we will learn how to express sizes: pixels, ems, rems, percentages and viewport units.