How Hex Color Codes Work (and How to Read Them at a Glance)
By the Hex Color Picker Team. Published . 8 minute read.
A hex color code is three numbers written as six characters. The first two characters are the amount of red, the next two are green and the last two are blue. Each pair is a number in base 16, from 00 (none) to FF (full strength). So #FF0000 is full red with no green or blue, #000000 is black, and #FFFFFF is white.
Once you know that, you can read most codes at a glance and convert any of them by hand. This guide shows how, with worked examples, then covers the shorthand and eight-digit forms, how to estimate a color from its code, and the mistakes that make a code invalid. If you'd rather not do the arithmetic, the hex to RGB converter and the RGB to hex converter do it instantly.
What the code is made of
A code such as #1E90FF has two parts:
- The
#is a marker that says "what follows is a hex color". In CSS it is required. - The six characters after it are three pairs:
1Efor red,90for green andFFfor blue.
Each pair is a number between 0 and 255, written in base 16 instead of base 10. That is why two characters are enough: the largest two-digit base-16 number, FF, is 255, which is the strongest a screen channel can be.
The total number of possible codes is 16 to the sixth power, which is 16,777,216. That is the same as 256 x 256 x 256, because each of the three channels has 256 possible values. Hex and RGB describe exactly the same set of colors.
Base 16 in one minute
You count in base 10, with ten digits from 0 to 9. Base 16 needs sixteen digits, so it borrows the first six letters of the alphabet:
| Hex digit | 0 to 9 | A | B | C | D | E | F |
|---|---|---|---|---|---|---|---|
| Value | 0 to 9 | 10 | 11 | 12 | 13 | 14 | 15 |
In a two-digit number, the left digit counts sixteens and the right digit counts ones. So 3C is 3 x 16 + 12 = 60, and FF is 15 x 16 + 15 = 255. The letters are not case sensitive, so ff and FF mean the same thing.
Reading a code pair by pair
Take #1E90FF. Split it into three pairs and convert each:
- Red:
1Eis 1 x 16 + 14 = 30. - Green:
90is 9 x 16 + 0 = 144. - Blue:
FFis 15 x 16 + 15 = 255.
The color is rgb(30, 144, 255), which CSS names dodgerblue. Blue is at full strength, green is a little over half, and red is low, so the code describes a bright blue that leans a little toward cyan.
You can often get a rough idea of a color before converting anything:
- A pair of
FFmeans that channel is full, and00means it is off. - A pair near the middle, such as
80, means about half strength, since80is 128 out of 255. - Whichever pair is largest tells you the main color.
#1E90FFhasFFin the blue slot, so it is mostly blue. - When two pairs are large and the third is small, you get a secondary color.
#FFFF00is yellow, because red and green are both full.
Converting RGB to hex by hand
Going the other way means turning each decimal number into two hex digits. Divide by 16: the whole-number result is the first digit, and the remainder is the second.
Take rgb(220, 20, 60), the CSS color crimson:
- Red: 220 divided by 16 is 13 remainder 12. Thirteen is
Dand twelve isC, so the pair isDC. - Green: 20 divided by 16 is 1 remainder 4, so the pair is
14. - Blue: 60 divided by 16 is 3 remainder 12, so the pair is
3C.
Join them and add the marker: #DC143C.
One detail catches people out. A single-digit result still needs two characters, so a channel value of 5 is 05, not 5. So rgb(5, 5, 5) is #050505. Without the leading zero the code would be the wrong length and the color would break.
Shorthand codes
When each of the three pairs is made of two identical digits, CSS lets you write just one digit from each pair. The browser repeats every digit to rebuild the full code:
| Short | Expands to | Color |
|---|---|---|
#F00 | #FF0000 | Red |
#0F0 | #00FF00 | Lime |
#F80 | #FF8800 | A bright orange, rgb(255, 136, 0) |
#FFF | #FFFFFF | White |
#369 | #336699 | A muted blue |
The shorthand only works when every pair repeats. #1E90FF can't be shortened, because 1E is two different digits. It is a convenience for typing, and nothing more: #F80 and #FF8800 are the same color.
Eight-digit codes and transparency
Add two more digits and you get an alpha channel, which controls transparency. The format is #RRGGBBAA. The alpha pair runs from 00 (fully transparent) to FF (fully opaque), the same 0 to 255 scale as the colors.
To pick an alpha value, multiply the opacity you want by 255 and convert to hex. These are the values you'll use most:
| Opacity | Alpha pair |
|---|---|
| 100% | FF |
| 90% | E6 |
| 75% | BF |
| 50% | 80 |
| 25% | 40 |
| 10% | 1A |
So #1E90FF80 is dodgerblue at about 50% opacity. There is also a four-digit form, #RGBA, which is the shorthand for it. If you prefer decimals, rgb(30 144 255 / 50%) says the same thing in plainer terms, and it is covered in the guide to colors in CSS and HTML.
Greys and equal pairs
When all three pairs are the same, the color is a grey, because equal amounts of red, green and blue light give no hue. #000000 is black, #808080 is the middle grey, #C0C0C0 is a light grey called silver in CSS, and #FFFFFF is white. If you see a code like #333333 or #EEEEEE, you know without converting that it is a neutral grey, and the number tells you roughly how dark: 33 is 51 out of 255, quite dark, while EE is 238, very light.
A common design trick is a "tinted grey", where the three pairs are almost equal but not quite, such as #EEF1F7. It looks neutral but carries a faint blue cast, which often feels more natural next to a brand color than a pure grey.
Estimating a color from the code
With a little practice you can guess the family of a color from the code alone. Here is a quick checklist:
- Which pair is highest? That is the dominant color: red, green or blue.
- Which pair is lowest? That channel is mostly missing, which pushes the color toward its opposite. A low red pushes toward cyan.
- How far apart are the pairs? Close together means a muted or greyish color. Far apart means a vivid one.
- How high are all the pairs? High values across the board mean a light color, and low values mean a dark one.
Try it on #2E8B57. The green pair, 8B, is clearly the largest, so it is a green. Red (2E) and blue (57) are low and fairly close, so it is not a neon green but a muted, dark one. That is seagreen. Now #F5F5DC: all three pairs are very high, with blue a little lower, so it is a very light color with a faint yellow cast. That is beige.
Case, the hash and other details
- Case doesn't matter.
#1e90ffand#1E90FFare identical. This site's tools output capitals so codes are easy to compare. - The hash is required in CSS.
color: 1E90FF;is invalid. Some design apps and form fields accept the code without the#, and the tools here do too, but a stylesheet doesn't. - Spaces are not allowed.
#1E 90 FFis not valid. - Only 3, 4, 6 and 8 digits are valid. A five-digit or seven-digit code is a typo, and the browser ignores the entire declaration.
Common mistakes
- A missing or extra digit. The most common typo, and it produces an invalid color or, with a three-digit code, a completely different one. If a color looks wrong, count the characters.
- Confusing the letter O with the digit 0, or I with 1. Hex only uses 0 to 9 and A to F, so
Ois never valid. - Reading the code left to right as one number. It is three separate numbers, not one big one.
#1E90FFis not a single value of 2 million. - Assuming bigger always means brighter in a way the eye sees. Equal numbers don't give equal brightness across colors. A full green looks much brighter than a full blue. The contrast guide explains how to measure it.
- Forgetting the zero in a small channel.
#5in a channel is05.
Why hex is so widely used
Hex is compact, it can be pasted anywhere, it has no spaces or commas to mistype, and it round-trips exactly with RGB. That is why brand guidelines, design tools and stylesheets all default to it. It isn't the friendliest format for adjusting a color, though, and for that you may want HSL. For the bigger picture of how hex fits with the other formats, read Hex, RGB, HSL and CMYK explained.
Frequently asked questions
What does the # mean in a hex color code?
It marks the text as a hex color, so the browser knows to read the next characters as base-16 digits. It carries no color information itself.
How many colors can a hex code represent?
16,777,216, which is 16 to the power of 6, or 256 values for each of three channels.
What is the hex code for white and black?
White is #FFFFFF (or #FFF) and black is #000000 (or #000).
Can a hex code have more than 6 digits?
Yes, 8 digits, where the last two are the alpha (transparency) channel. Other lengths, apart from 3 and 4 for shorthand, are invalid.
How do I convert a hex code to a name?
CSS defines 148 color names, and the named colors section lists the ones we cover. If your code matches one exactly, the hex color picker tells you the name. Otherwise it shows the closest CSS name.
Is a hex code the same as an RGB value?
Yes. They are the same three numbers written differently, and the conversion is exact in both directions.