Sub topic 5
Body text: size, line height, and line length
This chapter assumes you know basic HTML elements, CSS selectors, and the font-size, font-weight, and color properties. It covers the three settings that decide whether a paragraph is comfortable to read: size, line height, and line length.
Why body text comes first
Body text is the text people spend the most time reading. Headings help readers find their place, but body text carries the content. If the body text is hard to read, a clear heading structure will not make up for it.
Three properties do most of the work:
| Property | What it controls | A sound starting point |
|---|---|---|
| `font-size` | How large each character is | `1rem` (usually 16px) |
| `line-height` | Vertical distance between lines | `1.5` for paragraphs |
| `max-width` | How long each line can get | About `65ch` |
Size: start from the user's default
Browsers ship with a default font size, usually 16px, and users can change it in their settings. If you set body text in rem units, your text follows that setting. If you set it in px, it may not.
body {
font-size: 1rem;
}Do not shrink body text below the default to fit more on the screen. Small text is the most common cause of tiring pages, and it gets worse on phones. Treat 1rem as the floor for paragraphs. Keep smaller sizes, such as 0.875rem, for secondary text like captions, which a later chapter covers.
Line height: space between lines
line-height sets the height of each line box. When it is too tight, the descenders of one line crowd the ascenders of the next, and the eye jumps to the wrong line. When it is too loose, lines look unrelated.
Write line-height as a unitless number. A unitless value is multiplied by each element's own font size.
body {
font-size: 1rem;
line-height: 1.5;
}If you write line-height: 1.5rem or 24px on body, children inherit that fixed length. A large heading inside would then get the same 24px line box, and its lines would overlap. The unitless 1.5 avoids this because each child recalculates it.
A good range for body text is 1.4 to 1.6. Headings, which are larger and wrap onto fewer lines, usually take a tighter value such as 1.2.
Line length: how far the eye travels
On a wide screen, a paragraph can stretch across 150 or more characters. At the end of each line, the reader must find the start of the next one, and long lines make that hard. Very short lines cause the opposite problem, with constant jumping back.
A common target is 45 to 75 characters per line. The ch unit helps here. One ch is the width of the 0 character in the current font, so 65ch gives roughly 65 characters per line.
p {
max-width: 65ch;
}Use max-width and not width. With max-width, the paragraph can still shrink on a narrow screen. A fixed width: 65ch would force sideways scrolling on a small phone.
Putting it together
This example combines the three settings. The container sets the line length once, so every paragraph inside it stays readable.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Body text demo</title>
<style>
body {
font-family: system-ui, sans-serif;
font-size: 1rem;
line-height: 1.5;
margin: 0;
padding: 1rem;
}
article {
max-width: 65ch;
margin-inline: auto;
}
p {
margin: 0 0 1em;
}
</style>
</head>
<body>
<article>
<h1>Planning a multi-day hike</h1>
<p>Start with footwear, then add layers. Weather in the hills can change within an hour, so pack for conditions that are worse than the forecast.</p>
<p>Carry more water than you expect to need. Plan refills around streams and huts, and check that your route actually passes them.</p>
</article>
</body>
</html>Open this file and resize the window. The text stays at a readable width on large screens and fills the screen on small ones. The 1em bottom margin on paragraphs separates them by one line of text, which is easier to scan than indented first lines.
Common errors
- Fixed line height in pixels on
body. Children inherit the fixed value, and large text overlaps. Use a unitless number. - Body text in
pxonly. Some users' size preferences are ignored. Userem. - Setting
widthand notmax-width. The paragraph cannot shrink and causes horizontal scrolling. - Limiting the width of every element with
ch. Apply it to text blocks. Images, tables, and code samples may need more room. - Light gray text for body copy. Low contrast cancels the effect of good spacing. The contrast chapter returns to this.
Exercise
A page sets body { font-size: 1rem; line-height: 24px; }. An h1 inside is set to 2rem and wraps onto two lines. What goes wrong, and what is the fix?
Show answer
Every child inherits the fixed 24px line height, so the two lines of the 32px heading sit only 24px apart and overlap. Change the value on body to the unitless 1.5. Each element then multiplies it by its own font size. You can also give headings a tighter unitless value such as 1.2.
Exercise
Your paragraphs run the full width of a 1600px window. Write a rule that limits them to about 65 characters but lets them shrink on a phone.
Show answer
p { max-width: 65ch; }. Using max-width and not width lets the paragraph get narrower than 65ch when the screen is small.