3 / 9 · Basic
Colour and units
Light grey #bbbbbb text on white has a contrast ratio of 1.92 to 1. WCAG asks for at least 4.5 to 1 for normal text.
Jump to the 10 practice questionsYou will learn
- Keep enough contrast between text and background
- Choose px, rem, em and vw for the job
Before this: Selectors and the cascade
1Explore
Ways to write a colour
CSS accepts colours in several notations, and they all end up as the same thing: amounts of red, green and blue light on the screen. Named colours such as red, navy or rebeccapurple are easy to read but limited. Hex notation writes red, green and blue as two hexadecimal digits each, from 00 to ff (0 to 255): #1a5fb4 is 26 red, 95 green and 180 blue, a medium blue.
The first three write almost the same blue in three notations; the last one lets half of the white page show through.
Tap a choice: the code and the preview change together.
<p style="background: #1a5fb4; color: white; padding: 12px">Corner Bakery</p>Nothing is drawn: there is no visible content.
2Problem
Red in #ff8000
How much red is in #ff8000, on a 0 to 255 scale?
Hint
The first two hex digits are red; ff is the largest value.
Solution
Answer: 255
ff in hexadecimal is 15 times 16 plus 15 = 255.
3Problem
Green in #ff8000
How much green is in #ff8000?
Hint
80 in hexadecimal is 8 × 16.
Solution
Answer: 128
80 in hexadecimal is 8 times 16 = 128, so the colour is orange.
4Explore
Contrast is not optional
Text that is too close in lightness to its background is hard to read for many people: anyone with low vision, older readers, and everyone reading a phone in sunlight. WCAG 2.2, the W3C accessibility guidelines, measures this as a contrast ratio from 1 to 1 (no difference) to 21 to 1 (black on white). Success criterion 1.4.3 asks for at least 4.5 to 1 for normal text and 3 to 1 for large text, which WCAG defines as at least 18 point, or 14 point bold.
On white these grays reach about 1.9, 3.0, 4.5 and 12.6 to 1. Normal text needs at least 4.5 to 1.
Tap a choice: the code and the preview change together.
<p style="color: #bbbbbb; background: #ffffff; font-size: 18px">Orders close at 6 pm on Friday.</p>Nothing is drawn: there is no visible content.
5Problem
#777777 on white
Does #777777 on white pass the 4.5 to 1 requirement?
Hint
It is a little lighter than #767676, which just reaches 4.5 to 1.
Solution
Answer: No, it is 4.48 to 1
#777777 on white has a contrast ratio below the stated 4.5 to 1 threshold. The darker #767676 reaches 4.54 to 1 and meets that threshold.
6Explore
Absolute and relative units
Lengths need a unit, except zero. px is the CSS pixel, a fixed unit; on high-density screens one CSS pixel covers several physical pixels, so it stays roughly the same size to the eye. Relative units scale with something else. em is relative to the element's own font size, so padding: 0.5em on 20 pixel text is 10 pixels. rem is relative to the root (html) font size, which is 16 pixels in browsers unless the reader has changed it, so 1.25rem is normally 20 pixels.
The box around the text is 20px. em follows that parent; rem follows the root size, 16px here.
Tap a choice: the code and the preview change together.
<div style="font-size: 20px">
<p style="font-size: 16px; margin: 0">Text at 16px</p>
</div>Nothing is drawn: there is no visible content.
7Problem
1.5rem in pixels
The root font size is 16px. How many pixels is 1.5rem?
px
Hint
rem multiplies the root size.
Solution
Answer: 24 px
1.5 times 16 = 24.
8Problem
em padding
An element has font-size: 20px and padding: 0.5em. How many pixels of padding?
px
Hint
em uses the font size of the element itself.
Solution
Answer: 10 px
em uses the element own font size: 0.5 times 20 = 10.
9Practice
Style the badge
Style .badge: background hsl(210 80% 40%), text colour white, font size 1.25rem and padding 0.5em.
- hsl background
- white text
- 1.25rem = 20px
- 0.5em = 10px
<style>
.badge {
}
</style>
<p>Today: <span class="badge">Fresh rye</span></p>Hint
0.5em of a 20px font is 10px. Keep each value exactly as written in the task.
A solution
<style>
.badge {
background: hsl(210 80% 40%);
color: white;
font-size: 1.25rem;
padding: 0.5em;
}
</style>
<p>Today: <span class="badge">Fresh rye</span></p>10Practice
Fix the contrast
This text has a contrast ratio of 1.92 to 1. Change the text colour to #333333 (12.63 to 1) and make the note 1rem with a 2px solid border in currentColor.
- text #333333
- 1rem
- 2px border
- border in currentColor
<style>
.note { color: #bbbbbb; background: #ffffff; }
</style>
<p class="note">Orders close at 6 pm on Friday.</p>Hint
Four values: color, font-size, and a border with width, style and colour.
A solution
<style>
.note { color: #333333; background: #ffffff; font-size: 1rem; border: 2px solid currentColor; }
</style>
<p class="note">Orders close at 6 pm on Friday.</p>11Explain
Common mistakes
<style>
p { font-size: 28; }
</style>
<p>Fresh rye</p><style>
p { font-size: 28px; }
</style>
<p>Fresh rye</p>A length needs a unit (only 0 may go without). Without px the declaration is invalid and is skipped.
12
Summary
- Named, hex, rgb() and hsl() colours; a slash adds alpha.
- WCAG 1.4.3: 4.5:1 for normal text, 3:1 for large text; never colour alone.
- px is fixed; em follows the element font size; rem follows the root (16px by default).