Which color format should you choose in web design?
In CSS, there are several ways to declare a color. The choice of format often depends on what you are trying to achieve (animations, transparency, global theme). Here is a guide to help you choose:
1. The HEX code (hexadecimal)
Example: #FF5252
This is the original and most widely used format on the web. It is very short and easy to copy and paste from design software such as Figma, Photoshop or Illustrator. It is made of 3 pairs of characters representing the Red, Green and Blue (RGB) values in base 16.
Drawback: It is hard to manipulate mentally (how do you darken #FF5252 by 10% without a tool?).
2. The RGB and RGBA format (red, green, blue, alpha)
Example: rgba(255, 82, 82, 0.5)
This format gives the amount of red, green and blue light on a scale from 0 to 255. It is essential if you need transparency. The A (alpha) adds an opacity channel ranging from 0 (invisible) to 1 (fully opaque).
3. The HSL format (hue, saturation, lightness)
Example: hsl(0, 100%, 66%)
It is the favorite format of modern frontend developers. It matches the way the human brain perceives color:
- Hue: A degree on the color wheel from 0 to 360 (0 = red, 120 = green, 240 = blue).
- Saturation: From 0% (gray) to 100% (pure color).
- Lightness: From 0% (absolute black) to 100% (absolute white).
Advantage: To create a button's "hover" state, you just lower the lightness by 10% in CSS. It is also the perfect format for generating dynamic Dark Modes using CSS variables (custom properties).