Please enable JavaScript to use CodeHS

Web Design Tutorial

HTML Colors

Every color on a screen is a recipe of red, green, and blue light. Learn to write those recipes three ways: color names, rgb() values, and hex codes, then mix your own.

html + css ~20 min Beginner friendly
The Big Idea

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.

Put It On The Page

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:

index.html
<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>
Browser preview

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.


The Easy Way

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.


The Science

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:

+=yellow
+=cyan
+=magenta
++=white
++=black (all off)
½+½=olive (half power)

In CSS, you write a recipe with rgb() notation: the red amount, then green, then blue.

index.html
<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>
Browser preview

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!

Interactive

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 color mixer
R 39
G 169
B 225
RGB valuergb(39, 169, 225)
Hex code#27A9E1
Closest named colorDodgerBlue

Secret Code

Hex 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:

0123456789

The hexadecimal system has 16 digits. After 9, it keeps counting with letters:

0123456789ABCDEF

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.

#27A9E1
27 → red2×16 + 7 = 39
A9 → green10×16 + 9 = 169
E1 → blue14×16 + 1 = 225

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.

Interactive

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.

Valid hex code!
Red pair
27
= 39 out of 255
Green pair
A9
= 169 out of 255
Blue pair
E1
= 225 out of 255

Your Turn

Practice: your own colors

1

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:

index.html
<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>
Browser preview

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.


Code

A color example

You can set colors in HTML with names, hex codes, or RGB values. This example shows all three ways.


Check Yourself

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.