Lesson 4 of 7 · 5 min

Typography and colour

Fonts, sizes, line height and measure, colour formats, and design tokens with CSS custom properties, including a dark theme in a few lines.

On this page
  1. Font family
  2. Size, weight and spacing
  3. Fluid type with clamp()
  4. Colour
  5. Design tokens with custom properties
  6. Try it

Most of a web page is text, so good typography does more for a design than anything else. A small set of well-chosen values goes a long way.

Font family

List fonts in order of preference, ending with a generic family the browser always has:

body {
  font-family: "Geist", system-ui, -apple-system, "Segoe UI", sans-serif;
}
code {
  font-family: "Geist Mono", ui-monospace, Menlo, monospace;
}

To use a web font, either self-host it with @font-face or install a package such as @fontsource-variable/geist and import it once. Self-hosting avoids an extra request to another server.

@font-face {
  font-family: "Geist";
  src: url("/fonts/geist.woff2") format("woff2");
  font-weight: 100 900;   /* a variable font: every weight in one file */
  font-display: swap;     /* show fallback text while the font loads */
}

Size, weight and spacing

h1 {
  font-size: 3rem;
  font-weight: 600;
  line-height: 1.1;
  letter-spacing: -0.03em; /* large headings look better slightly tighter */
}
p {
  font-size: 1.125rem;
  line-height: 1.7;
  max-width: 65ch; /* about 65 characters per line */
}

Three rules of thumb:

  • Line height: around 1.5 to 1.75 for body text, 1.1 to 1.25 for headings.
  • Measure: lines of 50 to 75 characters are easiest to read. ch is the width of the "0" character, so max-width: 65ch does this for you.
  • Scale: pick a handful of sizes and reuse them. Random sizes make a page feel messy.

Fluid type with clamp()

clamp(min, preferred, max) lets a size grow with the screen but stay within limits:

h1 {
  font-size: clamp(2rem, 1.6rem + 2vw, 3.25rem);
}

On a phone the heading is 2rem; on a wide screen it stops at 3.25rem; in between it scales smoothly. No media query needed.

Colour

CSS accepts several formats. They all describe the same colours:

color: #0a0a0a;                 /* hex */
color: rgb(10 10 10);           /* red, green, blue */
color: rgb(10 10 10 / 50%);     /* with transparency */
color: hsl(0 0% 4%);            /* hue, saturation, lightness */
color: oklch(0.15 0 0);         /* perceptual lightness, chroma, hue */

A restrained palette is easier to keep consistent. A strictly black-and-white design, like this site, needs only a background, an ink colour, one muted grey for secondary text, and a light grey for lines.

Check contrast: body text should have a contrast ratio of at least 4.5:1 against its background. The developer tools show the ratio when you click a colour swatch.

Design tokens with custom properties

Custom properties (CSS variables) store a value once and reuse it everywhere:

:root {
  --bg: #ffffff;
  --ink: #0a0a0a;
  --muted: #666666;
  --line: #e5e5e5;
  --radius: 10px;
}

body { background: var(--bg); color: var(--ink); }
.card { border: 1px solid var(--line); border-radius: var(--radius); }
.meta { color: var(--muted); }

Because they are live values, a dark theme only has to redefine the tokens:

:root[data-theme="dark"] {
  --bg: #0a0a0a;
  --ink: #ededed;
  --muted: #a1a1a1;
  --line: #262626;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0a0a0a;
    --ink: #ededed;
  }
}

Every rule that uses var(--bg) updates instantly. A theme toggle is just JavaScript that sets data-theme on the <html> element.

Try it

Define --bg, --ink and --muted on :root, use them for the page, then add a button that runs document.documentElement.dataset.theme = "dark". Watch the whole page switch.

More from CSS Crash Course