HEX, RGB and HSL: a practical guide to colour codes
How each colour format works, when to use which in CSS and design tools, and how to check your colours are readable.
Every colour on a screen is a mix of red, green and blue light. HEX, RGB and HSL are three ways of writing down the same mix, and each is easiest for a different job.
HEX
color: #a8d08d;
A HEX code is six hexadecimal digits: two each for red, green and blue, from 00 (none) to ff (full). #ff0000 is pure red, #000000 is black and #ffffff is white. A three-digit shorthand doubles each digit, so #abc means #aabbcc.
HEX is compact, and the most common way to share colours between design tools, developers and brand guidelines. Its weakness is that it's hard to read: you can't easily tell from #a8d08d how to make it a little darker.
An eight-digit version, sometimes called HEXA, adds a fourth pair for opacity: #a8d08d80 is the same green at about 50%.
RGB
color: rgb(168, 208, 141);
color: rgb(168 208 141 / 50%);
RGB writes the same three channels as decimal numbers from 0 to 255. It's the format most code and image software works in, and the one to use when you need to calculate with colours. A fourth value adds opacity (RGBA), from 0 (transparent) to 1 (solid).
HSL
color: hsl(96, 42%, 68%);
HSL describes a colour the way people think about it:
- Hue is the position on the colour wheel, from 0 to 360 degrees: 0 is red, 120 is green and 240 is blue.
- Saturation is how intense it is, from 0% (grey) to 100% (full colour).
- Lightness runs from 0% (black) through the pure colour at 50% to 100% (white).
That makes HSL the easiest format for building variations. Keep the hue and saturation, change the lightness, and you get a consistent set of tints and shades for buttons, hover states and backgrounds.
HSV, also called HSB
Design tools such as Photoshop and Figma often show HSV, or HSB. It has the same hue, but uses value (brightness) instead of lightness: 100% value is the pure colour, not white. CSS doesn't use it, so you'll convert it when moving a colour from a design tool into code.
Which to use
- Sharing colours and brand guidelines: HEX, with RGB alongside.
- Building a palette of shades: HSL.
- Anything with transparency: RGBA, HSLA or HEXA.
- Print: none of these. Printers use CMYK, and the conversion isn't exact, so ask your printer or designer for print values.
Building a small palette in HSL
A practical approach for a website:
- Start with the main brand colour, for example
hsl(96, 42%, 68%). - Make a darker shade for text and buttons by lowering the lightness, such as
hsl(96, 42%, 30%). - Make a very light tint for backgrounds by raising it, such as
hsl(96, 42%, 94%). - Choose greys with the same hue and very low saturation, such as
hsl(96, 8%, 20%)for body text, so they sit comfortably beside the brand colour.
Keeping the palette in CSS custom properties makes it easy to adjust later:
:root {
--brand: hsl(96, 42%, 68%);
--brand-dark: hsl(96, 42%, 30%);
--brand-tint: hsl(96, 42%, 94%);
--text: hsl(96, 8%, 20%);
}
Check the contrast
A colour that looks good can still be hard to read. The Web Content Accessibility Guidelines (WCAG) set a minimum contrast ratio between text and its background: 4.5:1 for normal text and 3:1 for large text. Light brand colours often fail as text on white. Use them for backgrounds and accents, and keep a darker version for text.
Convert in one step
Paste any colour into the Color converter to see it as HEX, HEXA, RGB, RGBA, HSV, HSL and HSLA at once, or choose one by eye with the Color picker. There are direct converters for each pair as well, such as HEX to RGB and HEX to HSL.
Try it
More posts
What a DNS lookup tells you about a domain
A, MX, TXT, CNAME and the other DNS records explained: what each one does, and what to check when a website or email stops working.
Read post →
How to check an SSL certificate, and what the details mean
Issuer, validity, covered names and the chain: how to read a certificate, and the errors that make browsers show a warning.
Read post →
PNG, JPG, WebP or GIF: choosing the right image format for the web
Which format to use for photos, logos, screenshots and animations, and how much lighter WebP can make your pages.
Read post →