How screens see color
Have you ever wondered how a computer understands and displays color? Every picture on a screen is made of tiny dots called pixels. Each pixel gets a color code that tells it exactly what color to glow.
In HTML and CSS, you can write a color code in three different ways:
- A color name, like red or cyan
- An RGB value, like rgb(0, 255, 0)
- A hexadecimal code, like #FF00FF
All three are just different spellings of the same idea: how much red, green, and blue light a pixel should mix together. By the end of this tutorial you will be able to read and write all three.
Coloring with CSS
You color things on a web page with two CSS properties. The color property changes the text, and background-color changes the space behind it. Press Run to see the page this code makes:
<h1 style="color: red;">Reptile Club</h1> <p style="background-color: yellow;">Meetings are on Tuesdays.</p> <p style="color: white; background-color: navy;">Bring your gecko.</p>
Reptile Club
Meetings are on Tuesdays.
Bring your gecko.
Good to know: the last line uses both properties at once, white text on a navy background. Pick text and background colors that are very different from each other, like white on navy. High contrast keeps your page readable for everyone.
Named colors
HTML understands 140 color names, so for common colors you do not have to remember any numbers at all. Just write the name. Here are 16 favorites:
Wait, why is "Lime" the bright one? In HTML, the name green means a darker green. The super bright green made of pure green light is called lime. Notice how each chip also shows a hex code, every named color is secretly a number too.
Mixing light with RGB
Names only cover 140 colors. To get any color, you mix it yourself. Every pixel on your screen contains three tiny lights: one red, one green, and one blue. This is the RGB color system. Each light is a channel, and each channel gets a brightness from 0 (off) to 255 (full power).
Mixing light works differently from mixing paint. Adding more light makes things brighter, not muddier. Check out these recipes:
In CSS, you write a recipe with rgb() notation: the red amount, then green, then blue.
<p style="color: rgb(255, 0, 0);">Full red, no green, no blue</p> <p style="color: rgb(255, 165, 0);">Full red + some green = orange</p> <p style="background-color: rgb(0, 0, 0); color: rgb(255, 255, 255);">All lights on = white text</p>
Full red, no green, no blue
Full red + some green = orange
All lights on = white text
How many colors is that? Each channel has 256 choices (0 through 255), and there are three channels. That gives 256 × 256 × 256 = 16,777,216 possible colors. Almost 17 million!
Mix your own color
Time to be the pixel. Drag the three sliders to control the red, green, and blue lights, and watch the swatch change. The mixer also shows every way to write your color. Try to make: black, white, red, yellow, purple, and your favorite color.
rgb(39, 169, 225)#27A9E1Hex color codes
You just saw the mixer turn rgb(39, 169, 225) into #27A9E1. That shorter spelling is a hexadecimal code, or hex for short. It packs the same three numbers into just six characters.
Counting in base 16
Our everyday number system is decimal, which has 10 digits:
The hexadecimal system has 16 digits. After 9, it keeps counting with letters:
So A means 10, B means 11, and F means 15. Two hex digits together can spell any number from 0 (00) to 255 (FF). That is exactly the range of one RGB channel, which is no accident!
Reading a hex code
A hex color starts with # and then has three pairs. The first pair is the red channel, the middle pair is green, and the last pair is blue. To turn a pair into a normal number, multiply the first digit by 16 and add the second digit.
So #27A9E1 is just rgb(39, 169, 225) in disguise. A few famous ones to remember: #000000 is black (all lights off), #FFFFFF is white (all lights maxed), and #FF0000 is pure red.
Hex decoder
Type any six-character hex code and the decoder will split it into its red, green, and blue pairs, and show the color. Try #FFD700 (gold), #8CC63E (CodeHS green), or a random scramble of digits and letters.
Practice: your own colors
Restyle the Rainbow
Here is a page that shows off colors in all three styles. In your own editor, replace each color with one you choose. Use at least one color name, one rgb() value, and one hex code. Press Run to see what this version looks like first:
<h1 style="background-color: lightblue;">Check Out These Colors!</h1> <h2 style="color: rgb(0, 200, 80);">There</h2> <h3 style="color: rgb(255, 128, 0);">Are</h3> <h4 style="color: #408088;">So</h4> <h5 style="color: #E4592D;">Many</h5> <h6 style="color: darkred;">Different</h6> <h1 style="color: #E755D3;">COLORS!</h1>
Check Out These Colors!
There
Are
So
Many
Different
COLORS!
Stuck picking colors? Scroll back up to the RGB mixer, mix something you love, then copy its rgb() value or hex code into your page. The named colors gallery has ready-to-use names too.
A color example
You can set colors in HTML with names, hex codes, or RGB values. This example shows all three ways.
Quick quiz
1. What color is rgb(255, 255, 0)?
2. In the hex code #3A86FF, which two characters control the red channel?
3. What is the highest value one RGB channel can have?
Keep learning web design
Now you can paint a page any of 16.7 million colors. Next, give your pages structure with lists and tables, or jump into a full web design course.