Guide
HEX Color Codes Explained
A HEX color code is a short, six-digit way to write a color for the web. The first two digits are red, the next two are green and the last two are blue. For example, #4169E1 is royal blue.
How to read a HEX code
A HEX code is a # followed by six hexadecimal digits, written #RRGGBB. Each pair of digits sets the strength of one light channel on a scale from 00 (none) to FF (full). Here is royal blue, #4169E1, split into its three channels:
| Channel | HEX pair | Decimal | Share of full strength |
|---|---|---|---|
| Red | 41 | 65 | 25% |
| Green | 69 | 105 | 41% |
| Blue | E1 | 225 | 88% |
So #4169E1 is the same color as rgb(65, 105, 225): a little red, some green and a lot of blue, which is why it looks blue. You can see it on its own page: Royal Blue.
Hexadecimal in a minute
Hexadecimal counts in sixteens instead of tens. After the digits 0 to 9 it uses the letters A to F for the values 10 to 15. Two hex digits can therefore hold 16 × 16 = 256 different values, from 0 to 255, which is exactly the range of one color channel. Three channels give 256 × 256 × 256 = 16,777,216 possible colors.
00is 0: the channel is off.80is 128: about half strength.FFis 255: the channel is fully on.
In each pair, the first digit counts sixteens and the second counts ones. E1 is 14 × 16 + 1 = 225, because E stands for 14.
Common HEX codes
Mixing the three channels at full strength or zero gives the basic colors. Equal channels always give a gray.
| Color | HEX | RGB | Why |
|---|---|---|---|
| Black | #000000 | 0, 0, 0 | All channels off |
| White | #FFFFFF | 255, 255, 255 | All channels fully on |
| Red | #FF0000 | 255, 0, 0 | Only red on |
| Lime | #00FF00 | 0, 255, 0 | Only green on. The CSS keyword green is the darker #008000. |
| Blue | #0000FF | 0, 0, 255 | Only blue on |
| Yellow | #FFFF00 | 255, 255, 0 | Red and green together |
| Cyan | #00FFFF | 0, 255, 255 | Green and blue together |
| Magenta | #FF00FF | 255, 0, 255 | Red and blue together |
| Gray | #808080 | 128, 128, 128 | Equal channels, about half strength |
Shorthand and transparency
Three-digit shorthand. When each pair is two identical digits, CSS lets you write one digit per channel: #F80 means #FF8800. A color such as #4169E1 has no shorthand because its pairs are not doubled digits. Many teams skip shorthand so that every color in a style sheet is written the same way.
Transparency. Add two more digits at the end to set alpha (opacity): #RRGGBBAA. #4169E180 is royal blue at about 50% opacity, because 80 is 128 out of 255, or 50.2%. A four-digit form, #RGBA, also exists. Current major browsers support both.
Watch out on Android. Android writes 8-digit colors as #AARRGGBB, with alpha first. CSS puts alpha last. The same eight digits mean different colors in the two systems.
Letter case does not matter: #4169e1 and #4169E1 are the same color. This site shows codes in capital letters.
Converting by hand
To turn a decimal channel value into hex, divide it by 16. The whole number is the first digit and the remainder is the second. Using the channels of royal blue:
- 65 ÷ 16 = 4 remainder 1, so red is
41. - 105 ÷ 16 = 6 remainder 9, so green is
69. - 225 ÷ 16 = 14 remainder 1, and 14 is the letter E, so blue is
E1.
Put the pairs together and you get #4169E1. To go the other way, multiply the first digit by 16 and add the second. Or skip the arithmetic: the HEX to RGB converter and the RGB to HEX converter work in both directions.
HEX, RGB or HSL?
All three describe the same colors, so the choice is about convenience. HEX is short and easy to copy, which is why it is common in design hand-offs and style sheets. RGB is easier to read when you want to see how much of each channel there is. HSL is the easiest to adjust by hand, because lightness and saturation are separate numbers. Use the format your team prefers and convert when you need to; the color converter shows all of them at once.
Common mistakes
- Leaving out the
#. In CSS,4169E1on its own is not a valid color value. - Wrong length. Valid lengths are 3, 4, 6 and 8 digits. A 5- or 7-digit code is invalid.
- Letters beyond F. HEX only uses 0 to 9 and A to F, so
#4169G1is not valid. A common slip is typing the letter O instead of the number 0. - Judging closeness by looking at the digits. The first digit of each pair matters most.
#4169E1and#4169E2are practically identical, but#4169E1and#9169E1are clearly different.