On this page11 sections
Lecture 003: Colours
| Category | 1: CSS Basics |
| Lecture | 003 of 057 |
| Estimated video length | 10 min |
| Target | CSS3 · any modern browser |
| Level | Beginner |
Colour is the quickest way to give a page personality. CSS lets you write the same colour in several ways: a name, a hex code, RGB or HSL. Each has its uses, and transparency is only one extra number away.
Learning Objectives
- Set text colour with
colorand fill colour withbackground-color - Write colours as names, hex,
rgb()andhsl() - Add transparency with alpha values
- Keep text readable with good contrast
1. Text Colour and Background Colour
Example
<style>
.banner { color: white; background-color: darkslateblue; padding: 12px; }
</style>
<p class="banner">White text on a dark blue background.</p>color: the text colourbackground-color: the fill behind the element
2. Colour Names
CSS knows about 140 names: red, tomato, gold, teal, navy, skyblue…
Example
<p style="color: tomato;">tomato</p>
<p style="color: seagreen;">seagreen</p>
<p style="color: royalblue;">royalblue</p>Names are easy to read, but there aren't many, and brand colours rarely match one exactly.
3. Hex Colours
A hex colour is # followed by red, green and blue values in hexadecimal, from 00
(none) to ff (full).
#ff6600
│ │ └── blue 00
│ └──── green 66
└────── red ff → a bright orangeExample
<style>
.a { background: #ff6600; }
.b { background: #0468b5; }
.c { background: #222; } /* short form of #222222 */
div { color: white; padding: 8px; margin: 4px 0; }
</style>
<div class="a">#ff6600</div>
<div class="b">#0468b5</div>
<div class="c">#222</div>Hex is the most common format. Design tools such as Figma give you colours as hex.
4. RGB and HSL
rgb(red, green, blue): each value from 0 to 255:
color: rgb(255, 102, 0); /* the same orange as #ff6600 */hsl(hue, saturation, lightness):
- hue: a position on the colour wheel, 0–360 (0 red, 120 green, 240 blue)
- saturation: 0% grey → 100% vivid
- lightness: 0% black → 50% normal → 100% white
Example: HSL makes shades of one colour easy: keep the hue, change the lightness:
<style>
div { padding: 8px; margin: 3px 0; }
</style>
<div style="background: hsl(210, 80%, 25%); color: white;">hsl(210, 80%, 25%)</div>
<div style="background: hsl(210, 80%, 45%); color: white;">hsl(210, 80%, 45%)</div>
<div style="background: hsl(210, 80%, 70%);">hsl(210, 80%, 70%)</div>
<div style="background: hsl(210, 80%, 92%);">hsl(210, 80%, 92%)</div>5. Transparency
Add a fourth alpha value from 0 (invisible) to 1 (solid):
Example
<style>
.photo { background: url(https://picsum.photos/id/1018/500/200) center / cover; padding: 40px 16px; }
.caption { background: rgb(0 0 0 / 0.55); color: white; padding: 10px; }
</style>
<div class="photo">
<p class="caption">Semi-transparent black lets the photo show through.</p>
</div>You can also write rgba(0, 0, 0, 0.55) or a hex with alpha, such as #00000088.
6. Contrast
Light grey on white looks stylish but can be hard to read, especially on a phone in sunlight. Keep a clear difference between text and background.
Example
<p style="color: #ccc; background: white;">Hard to read</p>
<p style="color: #333; background: white;">Easy to read</p>Common Beginner Mistakes
1. Forgetting the #. color: ff6600; doesn't work. Write #ff6600.
2. Writing it the British way. The property is color, never colour, even though this
lecture's title says "Colours".
3. Poor contrast. Check that text stands out clearly from its background.
4. Opacity vs alpha. opacity: 0.5 fades the whole element, text included. To fade
only the background, use an alpha colour.
Practice Questions
- Which property sets the text colour? Which sets the fill?
- In
#00ff00, which colour channel is at full strength? - What does
#fffmean? - In HSL, which number would you change to make a colour lighter?
- Write a semi-transparent white background using
rgb(). - Why use an alpha colour instead of
opacityfor a see-through background?
Practice Answers
colorfor text,background-colorfor the fill.- Green.
- White: the short form of
#ffffff. - The third one, lightness.
background: rgb(255 255 255 / 0.5);(orrgba(255, 255, 255, 0.5))opacityalso fades the text and children; an alpha colour fades only the background.
Key Takeaways
color= text,background-color= fill.- Colours can be names, hex (
#ff6600),rgb()orhsl(). - Alpha (
/ 0.5orrgba) adds transparency without fading the text. - Always keep strong contrast between text and background.