On this page9 sections
  1. Learning Objectives
  2. 1. Colour Names
  3. 2. RGB
  4. 3. HEX
  5. 4. HSL
  6. Common Beginner Mistakes
  7. Practice Questions
  8. Practice Answers
  9. Key Takeaways

Lecture 008: Colors: Names, RGB, HEX & HSL

Category2: Text & Styles
Lecture008 of 048
Estimated video length12 min
TargetHTML5 · any modern browser
LevelBeginner

Colours on a web page can be written four ways: a name such as tomato, an RGB value, a HEX code, or HSL. They all describe the same colours, so once you know how each one works you can read any colour you find in a design file.


Learning Objectives

  • Use colour names for text, backgrounds and borders
  • Write RGB and RGBA colours
  • Read and write HEX colours
  • Use HSL to make lighter and darker shades

1. Colour Names

Browsers know about 140 colour names.

Example

html
<p style="background-color: tomato; padding: 8px;">tomato</p>
<p style="background-color: dodgerblue; padding: 8px;">dodgerblue</p>
<p style="background-color: mediumseagreen; padding: 8px;">mediumseagreen</p>
<p style="background-color: gold; padding: 8px;">gold</p>

Colours work on text (color), backgrounds (background-color) and borders (border):

html
<h3 style="color: slateblue; border: 2px solid slateblue; padding: 6px;">Slate blue everywhere</h3>

2. RGB

A screen makes every colour by mixing Red, Green and Blue light. Each one goes from 0 (off) to 255 (full).

Example

html
<p style="background-color: rgb(255, 0, 0); color: white; padding: 6px;">rgb(255, 0, 0) is pure red</p>
<p style="background-color: rgb(0, 160, 0); color: white; padding: 6px;">rgb(0, 160, 0) is green</p>
<p style="background-color: rgb(0, 0, 0); color: white; padding: 6px;">rgb(0, 0, 0) is black (all off)</p>
<p style="background-color: rgb(255, 255, 255); border: 1px solid #ccc; padding: 6px;">rgb(255, 255, 255) is white (all full)</p>
<p style="background-color: rgb(120, 120, 120); color: white; padding: 6px;">Equal values make grey</p>

RGBA adds a fourth value, alpha, for transparency: 0 is invisible, 1 is solid.

html
<p style="background-color: rgba(255, 99, 71, 1); padding: 6px;">alpha 1</p>
<p style="background-color: rgba(255, 99, 71, 0.5); padding: 6px;">alpha 0.5</p>
<p style="background-color: rgba(255, 99, 71, 0.15); padding: 6px;">alpha 0.15</p>

3. HEX

A HEX colour is the same red, green and blue, written as #rrggbb in base-16, where each pair goes from 00 to ff.

output
#ff6347
 ff = red 255,  63 = green 99,  47 = blue 71   → tomato

Example

html
<p style="background-color: #ff6347; padding: 6px;">#ff6347</p>
<p style="background-color: #3cb371; padding: 6px;">#3cb371</p>
<p style="background-color: #6a5acd; color: white; padding: 6px;">#6a5acd</p>
<p style="background-color: #eee; padding: 6px;">#eee (short for #eeeeee)</p>

HEX is what designers usually hand you, so it's the format you'll see most.


4. HSL

Hue, Saturation, Lightness:

PartRangeMeaning
Hue0–360A point on the colour wheel: 0 red, 120 green, 240 blue
Saturation0%–100%0% grey, 100% full colour
Lightness0%–100%0% black, 50% normal, 100% white

Example: keep the hue and change only the lightness to get a family of shades:

html
<p style="background-color: hsl(9, 100%, 30%); color: white; padding: 6px;">hsl(9, 100%, 30%)</p>
<p style="background-color: hsl(9, 100%, 50%); color: white; padding: 6px;">hsl(9, 100%, 50%)</p>
<p style="background-color: hsl(9, 100%, 75%); padding: 6px;">hsl(9, 100%, 75%)</p>
<p style="background-color: hsl(9, 100%, 92%); padding: 6px;">hsl(9, 100%, 92%)</p>

HSLA adds alpha, just like RGBA: hsla(9, 100%, 50%, 0.4).


Common Beginner Mistakes

1. Forgetting # on HEX colours. ff6347 alone does nothing.

2. RGB values above 255. rgb(300, 0, 0) is clamped to 255. The range is 0–255.

3. Pale text on pale backgrounds. Pretty, but hard to read. Keep strong contrast.

4. Spelling it "colour" in code. The property is color, even though this site uses British English.


Practice Questions

  1. What does rgb(0, 0, 255) show?
  2. What colour do equal R, G and B values make?
  3. Convert #000000 and #ffffff into colour words.
  4. In hsl(120, 100%, 50%), which colour is it?
  5. Write a half-transparent black background using RGBA.
  6. Which HSL value would you change to make a colour lighter?

Practice Answers

  1. Pure blue.
  2. A shade of grey (black at 0, white at 255).
  3. #000000 is black; #ffffff is white.
  4. Green.
  5. background-color: rgba(0, 0, 0, 0.5);
  6. The lightness (the third value).

Key Takeaways

  • Colours can be names, rgb(), HEX (#rrggbb) or hsl().
  • RGB and HEX mix red, green and blue; HSL picks a hue then sets saturation and lightness.
  • RGBA and HSLA add an alpha value for transparency.
  • Use colour for text, backgrounds and borders, and always keep good contrast.