Colours, Backgrounds and Fonts
- 6 min reading
- 6 min activity
0 of 4 tasks doneup to 75 XP
Today your pages get colours and fonts, and you check that everybody can read them.
- Set text and background colours in five ways.
- Make an element or a colour see-through with
opacityandrgba(). - Put a picture behind an element.
- Choose a font with a fallback, a size, a weight, a style and letter spacing.
- Check that text has enough contrast.
A rule has a selector, a property and a value.
Remember: the parts of a rule
A quick question from lesson 3.1. It is optional.
Text colour and background colour
color sets the colour of the text, background-color the colour behind an element. You can write the same colour in several ways:
| Way | Example | What it means |
|---|---|---|
| Name | navy | A colour name that CSS knows, in English. |
| Hex | #043E74 | Red, green and blue as three pairs of characters. |
| RGB | rgb(4, 62, 116) | Red, green and blue from 0 to 255. |
| RGBA | rgba(4, 62, 116, 0.8) | RGB plus alpha: 0 is see-through, 1 is solid. |
| HSL | hsl(209, 93%, 24%) | Hue (the colour wheel), saturation and lightness. |
- Colours from the slide
The slide shows three ways in one stylesheet. Our orange is darker than the slide's; you find out why below.
p { color: #B53D1D; /* hex colour code */ } h1 { color: blue; /* colour name */ } span { color: rgba(4, 62, 116, 0.9); /* RGB with transparency (a = alpha) */ }
Can everybody read it?
Some people see colours less clearly, and many people read on a phone in bright light. So text needs enough contrast: a big difference between the text colour and the background. We measure it as a ratio from 1 : 1 (the same colour) to 21 : 1 (black on white).
Normal text needs a contrast ratio of at least 4.5 : 1. Large text (24px, or about 19px and bold) needs at least 3 : 1 (WCAG 2.1, W3C, 2018).
The slide uses the orange #ff5733 for text. On white it has only 3.15 : 1. That is why our example uses the darker #B53D1D (5.75 : 1). Now fix three slide colours yourself.
Fix the slide colours
Three colours from the course slides are too light for text. Change one colour in each task until the contrast ratio is 4.5 : 1 or more.
See-through colours
opacity makes the whole element see-through: the box, its text and everything inside it. rgba() works on one colour only, so the text can stay solid. Both go from 0 (fully see-through) to 1 (solid).
.box {
opacity: 0.5; /* the whole box, text too, is half see-through */
}
.soft-box {
background-color: rgba(255, 0, 0, 0.5); /* only the background; the text stays solid */
}
A picture in the background
background-image puts a picture (or a gradient, a smooth change of colours) behind an element. Text on a picture needs good contrast too.
header {
background-image: url('park.jpg');
}
Fonts
This rule from the course slide sets five font properties for paragraphs:
p {
/* the font family: if the first font is missing, the browser takes the next one */
font-family: 'Open Sans', Arial, sans-serif;
/* the size: px, rem, em or % */
font-size: 16px;
/* the weight: normal, bold or a number from 100 to 900 */
font-weight: 400;
/* the style: normal, italic or oblique */
font-style: normal;
/* space between the letters */
letter-spacing: 0.5px;
}
font-family: the device uses the first font in the list that it has. Always end with a general family,seriforsans-serif, so there is always a fallback.font-size: px is a fixed size.remfollows the text size that the reader chose on the device, so many designers prefer it for text.font-weight: 400 is normal, 700 is bold.font-style: italicmakes slanted letters.
More: the font short form and your own font files
You can write the font properties in one line, in this order: style, weight, size/line height, family.
h1 {
font: italic 700 24px/1.5 Georgia, serif;
}@font-face loads a font file that you host yourself. Font services on the web usually give you a <link> tag for the head instead.
@font-face {
font-family: 'MyFont';
src: url('myfont.woff2') format('woff2');
}
body {
font-family: 'MyFont', sans-serif;
}What does each property do?
Tap a property, then tap what it does.
Your turn: a readable card
Guess first, then make the card readable and give it your own font.
A readable card
The card uses the slide colour #ff5733 with white text. Make the colours readable, give the card a font with a fallback, add a little letter spacing to the h2, make the paragraph bold and use rgba() for a see-through background.
- Hex code
- A colour as
#and six characters, for example#043E74. - RGBA
- A colour with a fourth number, alpha, for transparency.
- Opacity
- How see-through a whole element is, from 0 to 1.
- Font family
- A list of fonts. The browser uses the first one the device has.
- General family
- A font type that every device has, like
seriforsans-serif. - Contrast ratio
- How different the text colour and the background are. Normal text needs 4.5 : 1.
In short
coloris for the text,background-colorfor the space behind it.opacitychanges the whole element;rgba()changes one colour only.- End every font list with
seriforsans-serif. - Check the contrast: at least 4.5 : 1 for normal text.
Alignment, sizes, borders and space: you give your boxes room to breathe.
Sources
- MDN Web Docs (2026). color (text colour).
- MDN Web Docs (2026). <color> (names, hex, rgb(), hsl(), alpha).
- MDN Web Docs (2026). background-color.
- MDN Web Docs (2026). opacity (the whole element and its children).
- MDN Web Docs (2026). background-image.
- MDN Web Docs (2026). font-family (always include a generic family).
- MDN Web Docs (2026). font-size (px and rem).
- MDN Web Docs (2026). font (the short form).
- MDN Web Docs (2026). @font-face.
- W3C WAI (2026). Understanding Success Criterion 1.4.3: Contrast (Minimum) (4.5 : 1, large text 3 : 1; WCAG 2.1, 2018).
End of the lesson
This lesson: 0 of 85 XP
Code Cards in this lesson: 8
colorbackground-coloropacitybackground-imagefont-familyfont-sizefont-weightcontrast ratio