Lesson 13 of 23
In progress
Colour and typography that read well
Pick colours with enough contrast, build a font stack that loads without shifting, and set line height and measure for comfortable reading.
Text is most of the web. Two decisions carry most of the reading experience: the contrast between text and background, and the length and spacing of each line.
What you will learn
- Write colour values in hex, rgb and hsl.
- Set a type scale with rem, unitless line height and a readable measure.
- Load a webfont without a flash of invisible text.
Three ways to write the same colour
:root {
--ink: #1f2430;
--brand: rgb(122 75 210);
--brand-hover: hsl(266 58% 36%);
}
Hex is compact. rgb() with space-separated values is readable and supports alpha as rgb(0 0 0 / 40%). hsl() makes lighten and darken adjustments intuitive: change the third number.
Whichever you use, check the contrast. Dark grey on white is usually safe; mid-grey on a pastel background often is not. Body text needs 4.5:1, large text 3:1.
Do not rely on colour alone. Links in body text need an underline or another non-colour cue; error states need text and an icon.
A font stack that survives failure
body {
font-family: "Inter", system-ui, -apple-system, "Segoe UI", sans-serif;
font-size: 1rem;
line-height: 1.6;
color: #1f2430;
background: #ffffff;
}
The first font is the preferred one. system-ui uses the operating system’s interface font, which costs nothing and looks native. The final sans-serif is the guaranteed fallback.
If you self-host a webfont, declare it with @font-face, font-display: swap and a matching size-adjust on a fallback. Without those, text is either invisible while loading or shifts when the font arrives, and both hurt reading and Core Web Vitals.
Sizes and line height
Use rem for font sizes so the page respects the reader’s browser settings. px ignores them, which is an accessibility problem for anyone who increases the default text size.
A unitless line-height multiplies the element’s font size, so a heading and a paragraph can share the same value and each gets proportional spacing. line-height: 24px looks fine at 16px and cramped at 24px.
A simple scale is enough: 1rem for body, 1.25rem for an h3, 1.5rem for h2, 2rem for h1, with clamp() for the largest sizes. Too many sizes makes a page look unsettled.
Measure and rhythm
Line length is measured in characters, roughly 60 to 75 for comfortable reading. In CSS, max-width: 65ch on a text container is a direct way to set it; ch is the width of a zero in the current font.
Editable. Runs in your browser.
<article class="prose">
<h1>Readable by default</h1>
<p>A line that runs the full width of a wide monitor is tiring to track.
Limiting the measure keeps the eye moving comfortably from the end of
one line to the start of the next.</p>
<p>Space between paragraphs, rather than first-line indents, is the web
convention and survives narrow screens.</p>
</article>
Press Run to see the output.
margin-inline: auto centres the block without a wrapper. clamp() lets the heading grow with the viewport but stops before it becomes absurd.
Weight, alignment and emphasis
Use two weights at most: one for body, one for headings. A page with five weights looks noisy and loads more font files.
Left-align body text. Centred paragraphs are hard to read beyond a line or two, and justified text creates rivers of whitespace on the web because browsers do not hyphenate by default.
For emphasis, strong and em already carry meaning; style them rather than adding new classes. Avoid all-caps body text, and if a design calls for it on a label, add letter-spacing and keep it short.
Setting font-size in px on html overrides the reader’s default and breaks zoom-based preferences in some browsers. Set the root size in rem or leave it alone, and size everything else relative to it.
Style an article with a serif body font, a sans-serif heading, a 65-character measure, 1.7 line height and one accent colour for links. Check that the link is distinguishable without colour alone.
Show solution
Editable. Runs in your browser.
<article class="article">
<h1>Typography is structure</h1>
<p>Body text in a serif face, headings in a sans-serif face, and a
<a href="#">link with a visible underline</a> so colour is not the only cue.</p>
</article>
Press Run to see the output.
The measure is set once on the container, and the heading family overrides only the font. The link keeps its underline, which is the reliable non-colour signal.
Recap
- Hex, rgb and hsl describe the same colours; contrast is what makes them usable.
- A font stack with
system-uiand a generic fallback works before any webfont arrives. - Size in
rem, use unitless line height, and cap the measure near 65 characters.
Next, you will learn the box model, which explains almost every spacing surprise in CSS.
How many fonts should a page load?
Is a dark mode just inverted colours?
Progress is saved in this browser only. No account, no email.