Chapter 06

Type on the web

The browser is a typesetting engine you do not fully control: the reader picks the size, the viewport picks the measure, and the network decides when your font arrives. Good web typography is about designing for that uncertainty.

06.1

Units, and which to trust

  • pxAbsolute. Fine for borders and shadows; a poor choice for font-size because it ignores the reader's browser setting.
  • remRelative to the root font size. The correct default for type — it honours user preferences and stays predictable when nested.
  • emRelative to the current element. Ideal for spacing that should track the local size, such as letter-spacing and margins inside a component.
  • chThe advance width of the digit zero. Use it for measure: max-width: 65ch.
  • %, vw, vhViewport units scale with the window and ignore the reader. Only ever use them inside clamp().
06.2

Fluid type with clamp()

Fluid sizing interpolates between a minimum and a maximum across the viewport range, replacing a stack of breakpoints with one declaration. The middle term must include a rem component so that zooming still works.

h1 {
  /* min 2.5rem, fluid, max 8rem */
  font-size: clamp(2.5rem, 1.5rem + 5vw, 8rem);
  line-height: 0.95;
  text-wrap: balance;
}

p {
  font-size: clamp(1rem, 0.95rem + 0.25vw, 1.125rem);
  max-width: 65ch;
  text-wrap: pretty;
}

text-wrap: balance evens out headline line lengths; text-wrap: pretty prevents single-word last lines in paragraphs. Both are progressive enhancements — nothing breaks without them.

06.3

Font loading

Between first paint and the arrival of a web font, the browser must show something. Your choice of font-display decides whether that is nothing (invisible text) or a fallback that later reflows.

@font-face {
  font-family: "Site Sans";
  src: url("/fonts/site-sans.woff2") format("woff2");
  font-weight: 300 700;      /* variable range */
  font-display: swap;        /* show fallback, swap when ready */
  size-adjust: 104%;         /* match the fallback's x-height */
  ascent-override: 92%;
}
  • Subset to the characters you actually ship and serve WOFF2 only.
  • preconnect to the font host, or self-host to remove the extra connection entirely.
  • Tune the fallback with size-adjust and the ascent/descent overrides so the swap does not shift layout.
06.4

Variable fonts

A variable font holds a continuous design space in one file. Instead of shipping four static weights you ship one file and interpolate. Standard axes are weight (wght), width (wdth), slant (slnt), italic (ital) and optical size (opsz); families may add custom axes.

.headline {
  font-variation-settings: "wght" 620, "opsz" 48;
  /* prefer the high-level properties where they exist */
  font-weight: 620;
  font-optical-sizing: auto;
}
06.5

OpenType features worth switching on

  • tnumTabular figures — required for any column of numbers or a live-updating counter.
  • onumOldstyle figures for running prose, where lining figures shout.
  • liga / dligStandard ligatures are on by default; discretionary ones are display-only.
  • smcp / c2scReal small caps, instead of the browser's synthetic squash.
  • ss01…Stylistic sets — often where a single-storey a or a straight-leg R hides.
  • fracProper fractions instead of 1/2 typed as three characters.
.data-table td {
  font-variant-numeric: tabular-nums lining-nums;
}
.prose {
  font-variant-numeric: oldstyle-nums proportional-nums;
  font-feature-settings: "ss01" 1;
}