Skip to content
Tech Atlas, home
Search
Esc
  • All roadmapsBrowse every learning path
  • ExploreFind a technology area
  • ProjectsGuided projects and builds
  • My AtlasYour learning dashboard
Lesson 4 of 6 · Concept · 25 min · revision 2

Units, Type, Color and Motion

Choose relative units that scale with the reader, set type and colour that stay readable, and animate only what the browser can move cheaply — with the motion switched off for readers who ask.

Contentsand lesson path

What you will learn

  • Apply typography, color and relative units (rem, em, %, viewport units, ch) and place elements with relative, absolute, fixed and sticky positioning.
  • Animate state changes with transitions, transforms, keyframes and view transitions, animating only compositor-friendly properties and honouring prefers-reduced-motion.

Prerequisites: Semantic HTML

01. Why it matters

Four decisions shape how a page feels more than any layout trick: what your sizes are measured in, how the type is set, which colours sit next to each other, and what moves. Get them right and the page adapts to the reader's font size, zoom level and operating system settings. Get them wrong and the page fights the reader: text that will not grow, greys that disappear, motion that makes people ill.

The rule that ties the lesson together is relate to the reader, not to the pixel. Every measurement below was taken in Chromium at a 1280 × 720 window with the default root font size of 16 pixels, and the snippets are included so you can reproduce — or resize and watch — every number.

02. Relative units: rem, em, ch, % and the viewport

  • rem is relative to the root element's font size — 1.5rem is 1.5 × whatever html uses, regardless of where the declaration sits. Use it for type scale and spacing that should respond to the reader's browser font setting.
  • em is relative to the current element's font size — which, for font-size itself, means the parent's. That nesting is its power and its trap: em font sizes compound through the tree.
  • ch is the width of a 0 glyph in the current font — the honest unit for line lengths in text and input widths in monospace.
  • % is relative to the containing block: width percentages answer "how much of the available space?"
  • vw and vh are 1% of the viewport width and height. Good for full-height heroes; use them sparingly, and not on their own for body text, because text sized only in viewport units ignores the reader's font setting (mixing them with rem inside clamp(), as the responsive layout skill shows, keeps part of the size tied to it).
CSS · styles.css
:root { font-size: 100%; }
.card { font-size: 1.5rem; max-width: 22rem; }
.card p { font-size: 0.75em; }
.column { font-size: 1rem; width: 30ch; }
.half { width: 50%; }
.band { width: 25vw; height: 10vh; }
.code-sample { font-family: monospace; width: 10ch; }

Save every CSS block of this lesson into one styles.css next to the page below, open it in a 1280 × 720 window, and paste each snippet into the DevTools console as you reach it:

HTML · index.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Units, type, colour and motion</title>
  <link rel="stylesheet" href="styles.css">
</head>
<body>
  <div class="card">
    Card heading
    <p>Card text</p>
  </div>
  <div class="column">A column of running text, sized in ch.</div>
  <div class="half">Half of the body width</div>
  <div class="band">A band sized from the viewport</div>
  <pre class="code-sample">npm test</pre>
  <div id="drawer" class="drawer">Drawer</div>
</body>
</html>
DevTools console
const fs = (sel) =>
  getComputedStyle(document.querySelector(sel)).fontSize;
const w = (sel) =>
  document.querySelector(sel).getBoundingClientRect().width;
console.log('.card font-size = ' + fs('.card'));
console.log('.card p size   = ' + fs('.card p'));
console.log('.card width    = ' + w('.card').toFixed(1) + 'px');
console.log('.half width    = ' + w('.half').toFixed(1) + 'px');
console.log('.band width    = ' + w('.band').toFixed(1) + 'px');
Expected output (notes in parentheses are not printed)
.card font-size = 24px     (1.5rem = 1.5 x 16px root)
.card p size   = 18px     (0.75em of the parent's 24px)
.card width    = 352.0px  (max-width 22rem)
.half width    = 632.0px  (50% of the 1264px body box)
.band width    = 320.0px  (25vw of the 1280px viewport)

Watch the paragraph: 0.75em of 24 gives 18 — em compounds because the element's own font size is its children's measuring stick. The practical convention that falls out: rem for type and spacing, em for lengths that should track the local text (padding inside a button, the gap around a heading), % and ch where the available space or the glyph decides. The snippet leaves .column out on purpose: 30ch is thirty times the width of a 0 in the element's font, so it measured 240px with Chromium's default serif on the test machine and will differ a little with other fonts. And note what rem buys the reader: bump the browser's default font size to 20 pixels and every rem in the page grows with it — pixel sizes would not.

03. Type and colour that stay readable

For body text, a font-size around 1rem with a line-height near 1.5 is the accessible starting point; headings scale from the same root with rem. Cap line length with ch — roughly 45 to 75 characters per line reads comfortably, so max-width: 60ch is a good column for running text. Prefer system font stacks (system-ui, sans-serif) for interface text: they cost nothing to download and render in the reader's own idiom. If you load a web font, keep it to the weights you use.

Colour in CSS is written as hex (#b45309), as rgb(), or best as hsl(), where the three numbers are hue, saturation and lightness — lightness is the one to move when you need a darker shade of the same colour. Whatever the notation, what matters is the contrast between text and its background. WCAG 2.2 requires 4.5:1 for body text and 3:1 for large text; measuring beats eyeballing, and the ratio is pure arithmetic on the two colours:

Node · contrast.js (WCAG 2.2 relative-luminance ratio)
function luminance(hex) {
  const [r, g, b] = [1, 3, 5].map((i) => {
    const c = parseInt(hex.slice(i, i + 2), 16) / 255;
    return c <= 0.04045 ? c / 12.92 : Math.pow((c + 0.055) / 1.055, 2.4);
  });
  return 0.2126 * r + 0.7152 * g + 0.0722 * b;
}
function ratio(fg, bg) {
  const [l1, l2] = [luminance(fg), luminance(bg)].sort((a, b) => b - a);
  return ((l1 + 0.05) / (l2 + 0.05)).toFixed(2);
}
console.log('grey text #595959 on white = ' +
  ratio('#595959', '#ffffff') + ':1');
console.log('grey text #999999 on white = ' +
  ratio('#999999', '#ffffff') + ':1');
Expected output (notes in parentheses are not printed)
grey text #595959 on white = 7.00:1   (comfortable body text)
grey text #999999 on white = 2.85:1   (fails even for large text)

Two greys, a world of difference: #595959 passes the strictest body-text tier at 7:1, while #999999 — the same hue, merely lighter — fails everything. In between, #767676 measures 4.54:1, just over the AA line. Contrast is also why "light grey placeholder text" is a recurring accessibility complaint. The same arithmetic applies to white text on coloured buttons: #ffffff on #b45309 gives 5.02:1, comfortably passing; the next lighter orange of the same palette, #d97706, gives only 3.19:1 and fails for body text.

04. Motion: cheap properties, and a switch for the reader

A transition interpolates between an old and a new computed value whenever a property changes — usually because a class changed. The cost depends on the property. Browsers can usually run transitions of transform and opacity on the compositor, off the main thread, so they tend to stay smooth even while scripts keep the main thread busy. Geometry properties — left, top, width, margin — make the browser redo layout, and usually paint, on every frame. Slide things with transform: translateX(), fade them with opacity, and reserve geometry changes for the rare cases where layout must actually move.

The drawer in the probe below declares transition: transform 200ms ease, opacity 200ms ease. A .closed class sets transform: translateX(-100%); opacity: 0. Add the class and sample the state at about 100 milliseconds of the 200:

CSS · styles.css
.drawer { transition: transform 200ms ease, opacity 200ms ease; }
.drawer.closed { transform: translateX(-100%); opacity: 0; }

/* the switch the reader owns: */
@media (prefers-reduced-motion: reduce) {
  .drawer { transition: none; }
}
DevTools console
console.log('reduce matches = ' +
  matchMedia('(prefers-reduced-motion: reduce)').matches);
console.log('transition-property = ' +
  getComputedStyle(document.getElementById('drawer'))
    .transitionProperty);
document.getElementById('drawer').classList.add('closed');
setTimeout(() => {
  const cs = getComputedStyle(document.getElementById('drawer'));
  console.log('at ~100ms: transform = ' + cs.transform);
  console.log('at ~100ms: opacity   = ' + cs.opacity);
}, 100);
Illustrative output (timing-dependent: sampled at about 100 ms)
reduce matches = false
transition-property = transform, opacity
at ~100ms: transform = matrix(1, 0, 0, 1, -891.337, 0)
at ~100ms: opacity   = 0.294828

Halfway through, the drawer is mid-transit and partially faded — the matrix represents translation across its own width, and opacity is progressing toward zero. Two facts worth knowing: a transition interrupted midway reverses from where it is, not from the start, and getComputedStyle returns the interpolated value, which is why the Computed panel is where you confirm an animation is actually running.

The @media block is the reader's exit. Operating systems offer a reduce-motion setting; the browser does not act on it — it only reports it through prefers-reduced-motion: reduce, and your CSS must answer. The courtesy scale: remove large movement (parallax, sliding panels, zooms) entirely, let small fades survive, and keep every state change legible without animation. Keyframe @keyframes animations take the same medicine via animation: none. For bigger state changes, the View Transitions API (document.startViewTransition) snapshots the old and new views and cross-fades between them — the same reduce-motion rule applies to its ::view-transition pseudos.

05. Check your understanding

Practice check · 3 questions

Units and motion

Decide from the rules alone, then read the reasoning.

Q1. The reader sets their browser default font size to 20px and your stylesheet never mentions `html { font-size }`. What is `1.5rem` then?
Q2. Your stylesheet has no `prefers-reduced-motion` block. A reader turns on their system's reduce-motion setting. What happens to your 300ms fade-in?
Q3. A drawer has `transition: transform 200ms`. You add `.closed`, wait 50ms, then remove the class. What does the reader see?
Practice only: not graded, not saved, not evidence.

06. Apply it

Convert your article page's remaining pixel sizes to relative units: type and spacing in rem, the column width as ch, one vw- or %-based measure where the viewport genuinely should decide. Check the greys you used against the contrast script from this lesson and fix any pair below 4.5:1 for body text. Then add one transition — a card that lifts with transform: translateY(-2px) and a shadow on :hover — plus the prefers-reduced-motion block that removes it, and verify with the console snippet that transition-property is transform and nothing else. Finish the skill's practice brief here too: a menu transition on transform and opacity only, switched off under reduced motion. The debugging lab that follows will hold this stylesheet up to the light.

07. Review prompt

Without looking back: what is the difference between rem and em in a font-size declaration, and why does em compound? What does ch measure, and why is it the right unit for a readable column? Recite the WCAG contrast thresholds for body and large text. Name the two properties a compositor can animate without layout, and say exactly what the browser does — and does not do — when a reader turns on reduce motion.

References

Your progress

Reading state

Sign in to save that you finished reading this lesson.

Sign in to mark reading complete

Prove this skill

10 questions · about 8 minutes · scored automatically. Marking a lesson as read never demonstrates a skill.
Take the skill check