Lesson 15 of 52CSS foundations
Typography
Choose fonts with sensible fallbacks, load web fonts, and set size, weight, line height and line length so text is a pleasure to read.
Most of what people do on a web page is read. A site with plain colours and a simple layout can still feel polished if its text is easy to read, and a beautiful layout falls apart if the text is cramped, tiny or runs across the whole screen. Typography is the craft of making text readable and pleasant: picking fonts, sizes, spacing and line length. You do not need to be a designer. A handful of CSS properties and a few rules of thumb will get you most of the way.
font-family and fallback stacks
The font-family property names the font to use. Because you cannot be sure a font is installed on the reader's device, you give a list called a font stack. The browser walks the list from left to right and uses the first font it can find.
body {
font-family: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}Font names with spaces go in quotes. The last item should always be a generic family, a keyword that means "whatever the device has of this kind":
| Generic family | Looks like |
|---|---|
serif | Letters with small strokes at the ends, like a newspaper |
sans-serif | Clean letters without those strokes |
monospace | Every character the same width, for code |
system-ui | The operating system's own interface font |
The browser tries the first font. If the web font has downloaded, it uses it and stops.
A stack built from system fonts alone (system-ui, sans-serif) costs nothing to download and looks native on every device. It is a fine choice for apps and dashboards.
Web fonts
When you want a specific font, you load it from a file. There are two common ways.
A hosted font service, like Google Fonts, gives you a link element to put in your head and the font-family name to use. It is the quickest way to start.
Self-hosting means putting the font files in your project and describing them with @font-face:
@font-face {
font-family: "Inter";
src: url("/fonts/inter-var.woff2") format("woff2");
font-weight: 100 900;
font-display: swap;
}The font-family here is the name you invent for it; src points at the file. Use the WOFF2 format, which is compressed and supported by every modern browser. font-weight: 100 900 declares a variable font: one file that contains every weight, instead of a separate file per weight. font-display: swap tells the browser to show text in a fallback font straight away and swap in the web font when it arrives, so readers never stare at invisible text.
Every font file is a download. Two families (one for headings, one for body) and a few weights is plenty. More than that slows the page and rarely looks better.
Size and a type scale
Set font sizes in rem so they respect the reader's browser setting (see Colors and Units). Body text at 1rem (16px by default) is a good minimum; many sites use 1.125rem for long reading.
Instead of inventing sizes one by one, use a type scale: a short set of sizes where each step is the previous one multiplied by the same ratio. With a ratio of 1.25, you get roughly 1rem, 1.25rem, 1.563rem, 1.953rem and 2.441rem. Headings picked from a scale look related, the way notes in a chord do.
Preview
Try this: switch to a stronger ratio of 1.333 by setting the sizes to 3.157rem, 2.369rem, 1.777rem and 1.333rem. Notice the headings feel more dramatic. Then change system-ui, sans-serif to Georgia, serif and see how much the mood changes.
Weight, line height and letter spacing
font-weight takes a number from 100 (thin) to 900 (black). 400 is normal and 700 is bold. A font only displays weights it actually has; ask for 600 from a font that only has 400 and 700, and the browser picks the closest one.
line-height is the height of each line of text, and it has a big effect on readability. Body text reads well at around 1.5. Large headings look better tighter, around 1.1 to 1.2, because the gaps between big lines look huge. Write line height as a plain number with no unit. A unitless value is passed to children as a ratio and recalculated for each font size. A value like 24px would be inherited as a fixed 24px, which overlaps on any child with large text.
letter-spacing adjusts the space between characters. Use em so it scales with the size: a slight negative value like -0.02em tightens big headings, and a positive value like 0.08em helps small uppercase labels breathe.
text-align sets horizontal alignment: left, right, center, justify, or start and end, which follow the language direction. Keep long text left-aligned (or start). Centred paragraphs are hard to read because each line starts in a different place, and justify creates uneven gaps between words on the web.
What is the line height of the h2, in pixels?
article { font-size: 16px; line-height: 1.5; }
article h2 { font-size: 32px; }Line length
The length of a line matters as much as the size of the text. Very long lines make your eyes work hard to find the start of the next one; very short lines break the rhythm. Comfortable reading is roughly 45 to 75 characters per line.
CSS has a unit made for this: ch, the width of the "0" character in the current font. max-width: 65ch on your article gives about the right length automatically, and it adapts if the font size changes.
Preview
Try this: the paragraph runs across the whole preview. Change max-width: none to max-width: 40ch, then 65ch. Then change line-height: 1.6 to 1.1 and feel how much harder it is to read.
Better line breaks: text-wrap
Two newer values of text-wrap improve how lines break:
text-wrap: balanceevens out the length of lines in short blocks such as headings, so you do not get one word alone on the last line of a title. Browsers only balance a few lines at a time, so use it on headings, not paragraphs.text-wrap: prettyasks the browser to avoid a single short word on the last line of a paragraph, at a small performance cost.
h1, h2, h3 { text-wrap: balance; }
p { text-wrap: pretty; }Both are progressive enhancements: browsers that do not understand them just wrap normally. balance works in all current major browsers. Support for pretty is newer and still uneven, so check MDN or Can I Use for current support.
Putting it together
Here is a small recipe card that uses everything so far: a system font stack, sizes in rem, a tight heading, a spaced-out uppercase label, a comfortable line height and a capped line length. Small details like these are what make text look designed rather than default.
Preview
Try this: set the label's letter-spacing to 0 and see how cramped small capitals look. Change the heading's font-weight to 400, then 800. Remove text-wrap: balance and resize the window to compare the heading's line breaks.
Match each property to the job it does.
sans-serif?- font stack
- A list of fonts in font-family; the browser uses the first one available.
- generic family
- A keyword like serif, sans-serif or monospace that always maps to an installed font.
- @font-face
- A CSS rule that defines a font name and points to the font file to download.
- variable font
- A single font file that contains a continuous range of weights or other styles.
- type scale
- A set of font sizes where each step is the previous one times a fixed ratio.
- line height
- The height of each line of text; around 1.5 for body copy, written without a unit.
- ch
- A unit equal to the width of the 0 character, handy for setting line length.