On this page9 sections
Lecture 008: Colors: Names, RGB, HEX & HSL
| Category | 2: Text & Styles |
| Lecture | 008 of 048 |
| Estimated video length | 12 min |
| Target | HTML5 · any modern browser |
| Level | Beginner |
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
<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):
<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
<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.
<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.
#ff6347
ff = red 255, 63 = green 99, 47 = blue 71 → tomatoExample
<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:
| Part | Range | Meaning |
|---|---|---|
| Hue | 0–360 | A point on the colour wheel: 0 red, 120 green, 240 blue |
| Saturation | 0%–100% | 0% grey, 100% full colour |
| Lightness | 0%–100% | 0% black, 50% normal, 100% white |
Example: keep the hue and change only the lightness to get a family of shades:
<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
- What does
rgb(0, 0, 255)show? - What colour do equal R, G and B values make?
- Convert
#000000and#ffffffinto colour words. - In
hsl(120, 100%, 50%), which colour is it? - Write a half-transparent black background using RGBA.
- Which HSL value would you change to make a colour lighter?
Practice Answers
- Pure blue.
- A shade of grey (black at 0, white at 255).
#000000is black;#ffffffis white.- Green.
background-color: rgba(0, 0, 0, 0.5);- The lightness (the third value).
Key Takeaways
- Colours can be names,
rgb(), HEX (#rrggbb) orhsl(). - 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.