edubun
English
1 / 12

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 questions

You 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.

background
<p style="background: #1a5fb4; color: white; padding: 12px">Corner Bakery</p>
Preview

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.

Text colour
<p style="color: #bbbbbb; background: #ffffff; font-size: 18px">Orders close at 6 pm on Friday.</p>
Preview

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.

font-size
<div style="font-size: 20px">
  <p style="font-size: 16px; margin: 0">Text at 16px</p>
</div>
Preview

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.

Checks
  • hsl background
  • white text
  • 1.25rem = 20px
  • 0.5em = 10px
Your code
<style>
  .badge {

  }
</style>

<p>Today: <span class="badge">Fresh rye</span></p>
Preview
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.

Checks
  • text #333333
  • 1rem
  • 2px border
  • border in currentColor
Your code
<style>
  .note { color: #bbbbbb; background: #ffffff; }
</style>

<p class="note">Orders close at 6 pm on Friday.</p>
Preview
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

Mistake
<style>
  p { font-size: 28; }
</style>
<p>Fresh rye</p>
Fixed
<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).

Next unitThe box model
Full text · The whole lesson as one text

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. rgb(26 95 180) says the same thing in decimal. hsl(210 80% 40%) describes a colour by hue (an angle on the colour wheel, where 0 is red, 120 green and 240 blue), saturation and lightness. This makes it easy to produce a lighter or darker shade of the same hue by changing one number.

color: navy;
color: #1a5fb4;              /* same as #1A5FB4 */
color: #36c;                 /* short form of #3366cc */
color: rgb(26 95 180);
color: rgb(26 95 180 / 50%); /* half transparent */
color: hsl(210 80% 40%);
background-color: transparent;
The older comma syntax, rgb(26, 95, 180), still works everywhere.

A value after a slash controls opacity, which means how much of the background shows through. For example, rgb(0 0 0 / 50%) is black at half opacity, so whatever is behind shows through. color sets the text colour, background-color the fill behind an element, and border-color its border. The keyword currentColor means the element's text colour, which is handy for icons and borders that should match the text.

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. Dark grey #767676 on white just passes at 4.54 to 1; #777777 just fails at 4.48. Browser developer tools show the ratio when you inspect a text element, so there is no need to guess.

Colour should also never be the only way information is shown. A form that marks errors only by turning a border red leaves out everyone who cannot see red well; add a message or an icon as well.

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. % is relative to the parent, usually its width. vw and vh are hundredths of the viewport width and height, and ch is about the width of the digit zero in the current font.

Which to use? Sizing text in rem respects the reader: if someone sets their browser to larger text, everything sized in rem grows with it, while text fixed in px may not. Padding in em grows with the text it surrounds. Pixels are fine for things that should not scale, such as a 1 pixel border.

Key points
  • 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).

Practice questions from Colour and units on edubun

Embed this quiz

Free to embed. Please keep the credit line.

Sources for this course (5)
  • W3C CSS Working Group specifications: CSS Snapshot 2025, Selectors Level 4, CSS Cascading and Inheritance Level 5, CSS Color Level 4, CSS Values and Units Level 4, CSS Box Model Level 3, CSS Box Sizing Level 3, CSS Fonts Level 4, CSS Flexible Box Layout Level 1, CSS Grid Layout Level 2, Media Queries Level 4 and Level 5 (w3.org/TR/, checked 2026-10-17)
  • MDN Web Docs, Learn web development: CSS styling basics and CSS layout, and the CSS reference (developer.mozilla.org/en-US/docs/Learn_web_development/Core/Styling_basics, .../Core/CSS_layout, developer.mozilla.org/en-US/docs/Web/CSS, checked 2026-10-17)
  • W3C, CSS history and CSS1 Recommendation of 17 December 1996 (w3.org/Style/CSS20/history.html, w3.org/press-releases/1996/css1-rec, checked 2026-10-17)
  • W3C WCAG 2.2, success criteria 1.4.3 Contrast (Minimum)
  • 1.4.8 Visual Presentation, 1.4.10 Reflow, 1.4.12 Text Spacing (w3.org/TR/WCAG22/, checked 2026-10-17).

Checked: 2026-10-17