CSS Typography: Fonts, Sizes, Line Height and Weight
Most of a web page is text, so typography matters more than any other styling. A few properties do most of the work.
font-family and font stacks
font-family takes a comma-separated list. The browser uses the first font that is installed and falls back along the list:
body { font-family: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
Names with spaces need quotes. Always finish with a generic family: serif, sans-serif, monospace or system-ui. system-ui asks for the operating system's interface font, which looks native and costs no download, a good default for fast sites.
Size and weight
font-sizesets the height of the text. Useremso readers' font preferences carry through.font-weighttakesnormal(400),bold(700) or a number from 100 to 900. A number only has a visible effect if the font has that weight.font-style: italicslants the text.
Line height and measure
line-height controls the vertical space each line occupies. Write it without a unit, like line-height: 1.6: a unitless value is multiplied by each element's own font size, so headings and small print both get proportional spacing. With a unit such as px, the computed value is fixed and inherited as-is, which cramps larger text. Body text reads comfortably around 1.5 to 1.7.
Line length matters too. Lines of roughly 60 to 75 characters are easiest to follow, and max-width: 65ch keeps a column near that, since ch is the width of the "0" character.
Alignment and spacing
text-align takes left, right, center or justify. letter-spacing adds space between characters, useful for small uppercase labels. text-transform: uppercase changes the displayed case without changing the HTML, so screen readers and copy-paste still get the original text.
The example sets these properties on a heading and a paragraph and prints a few computed values. Note how the unitless line height becomes a pixel value based on each element's own size.
Expected page text
CHAPTER 7 Readable text Comfortable line height and a sensible line length make long reading easy. h1 line-height: 48px p line-height: 28.8px h1 font-weight: 700 label transform: uppercase
Web fonts
To use a font the reader may not have, declare it with @font-face and a woff2 file served from your own site, and set font-display: swap so text appears immediately in a fallback font while the custom one loads. Each weight is a separate file, so load only the weights you use.
Summary
- End every
font-familylist with a generic family;system-uiis fast and native. - Use
remforfont-sizeand a unitlessline-height. - Keep lines readable with
max-widthinch.
Quiz: Text and fonts