Sub topic 13
Vertical Rhythm and Paragraph Spacing
This chapter assumes you can already set line-height as a unitless ratio and limit the measure with max-width in ch. It also assumes you know rem and em units. Here you will take the line height you chose for body text and use it as the unit for all vertical space around paragraphs.
What vertical rhythm is
Vertical rhythm is the idea that the vertical distances on a page repeat in a predictable pattern. Leading sets the distance between lines inside a paragraph. Vertical rhythm extends that distance to the gaps between paragraphs, headings, lists, and images.
The reader gets two benefits. The page looks ordered, because related gaps match. The gaps also tell the reader how things are grouped. A gap inside a paragraph is small, a gap between paragraphs is bigger, and a gap before a heading is bigger still.
Strict rhythm matters less than consistent rhythm. Browsers do not give you a true baseline grid, and images, captions, and fallback fonts will drift off any grid you set. The practical goal is a small set of spacing values that all derive from one number: the height of one line of body text.
Find your base unit
Start with the body text. Multiply the font size by the line-height ratio. With a font size of 1rem and a line-height of 1.5, one line is 1.5rem tall. That value is your base unit.
p {
font-size: 1rem;
line-height: 1.5;
margin-block: 0 1.5rem;
}The bottom margin of 1.5rem equals one line. A reader sees a blank line between paragraphs, and the text still looks like one column.
margin-block is a logical property. In a horizontal writing mode such as English, margin-block: 0 1.5rem means zero top margin and 1.5rem bottom margin. Using only the bottom margin keeps the rule simple. Every paragraph owns the space after it, and none owns the space before it.
Let the browser do the arithmetic with lh
You can avoid repeating 1.5rem by using the lh unit. It equals the computed line height of the element it is used on. If you later change the line height, the margin follows it.
p {
font-size: 1rem;
line-height: 1.5;
margin-block: 0 1lh;
}The rlh unit is similar but uses the line height of the root element. Both units are in current evergreen browsers, but check your browser support targets first. If you must support older browsers, write the value in rem and update it by hand, or store it in a custom property.
:root {
--line: 1.5rem;
}
body {
font-size: 1rem;
line-height: 1.5;
}
p {
margin-block: 0 var(--line);
}The custom property is a good fallback. It gives you one place to change the base unit, and later rules can use fractions of it with calc(var(--line) * 2).
How big should the paragraph gap be?
The gap has to be large enough to read as a break and small enough to keep the text in one column. These are the common choices.
| Gap size | Effect | When to use it |
|---|---|---|
| Less than 0.5 line | Hard to tell apart from ordinary line spacing | Rarely a good choice |
| 0.5 to 0.75 line | Compact, clearly separated | Dense interfaces, documentation, captions |
| 1 line | Standard, matches the rhythm | Articles and most body text |
| More than 1.5 lines | Paragraphs look disconnected | Short pull text or slides |
If you want a compact page, remember that a paragraph gap smaller than the line height still needs to be visible. A gap of one-half line works when the leading is generous. It fails when the leading is tight, because the two gaps become too similar.
Space or indent, but not both
Printed books mark a new paragraph with a first-line indent and no extra space. Web pages usually use space between paragraphs. Pick one. Using both gives two signals for the same event and makes the text look unfinished.
.book p {
margin: 0;
text-indent: 1.5em;
}
.book p:first-child,
.book h2 + p {
text-indent: 0;
}The indent is measured in em so it scales with the font size. A common range is 1em to 1.5em. The first paragraph after a heading is not indented, because the heading already marks the start of a section.
Margin collapsing
Adjacent vertical margins between block siblings collapse. The larger margin wins, and the two are not added together. Suppose a paragraph has margin-bottom: 1.5rem and the next heading has margin-top: 3rem. The space between them is 3rem, not 4.5rem.
This is helpful for rhythm, because you can give headings an upper margin and paragraphs a lower margin without doubling the gap. It does not apply everywhere:
- Margins do not collapse inside flex or grid containers. In those containers, use
gapto set the spacing between children. - A parent with padding, a border, or its own formatting context stops the margins of its first or last child from collapsing through it.
- Margins on floated and absolutely positioned elements do not collapse.
If a gap looks larger than you expect in a flex or grid layout, check whether margins that would have collapsed are now adding up.
Headings and other sizes
Large type breaks the one-unit rule, because a heading's line height is not 1.5rem. To keep the page in step, choose a line height for the heading that is a multiple of a smaller grid step. A half-unit step of 0.75rem works well.
h2 {
font-size: 1.75rem;
line-height: 2.25rem;
margin-block: 3rem 0.75rem;
}One heading line is 2.25rem, which is three half-units. The top margin of 3rem is four half-units, and the bottom margin of 0.75rem is one. A single-line heading therefore occupies a whole number of half-units, and the paragraph that follows lands on the same grid as the paragraphs above.
The heading's gap is deliberately uneven: more above than below. This ties the heading to the text it introduces. Equal space on both sides makes a heading look as if it belongs to neither section.
A length such as 2.25rem is safe here because the heading is a leaf element. Do not set a length line-height on a container such as body, since every descendant inherits the same fixed height regardless of its font size. Inherited unitless ratios are the safer default, as the earlier chapters showed.
Where rhythm breaks, and what to do
A few elements will not fit your base unit without compromise.
- Captions and small print. At
0.875remwith a ratio of 1.4, one line is1.225rem, which is not a grid multiple. Accept the odd height and restore the grid with the margin that follows, for example by rounding the bottom margin to1.5rem. - Images. Their heights are arbitrary. Add a bottom margin that puts the next paragraph back on a unit boundary, or accept the drift for photographs.
- Code blocks and tables. Their padding and borders add extra height. Give them margins in whole units and do not worry about the internal lines.
- Fallback fonts. If a fallback font has a different size, the ratio still holds, because
line-height: 1.5scales with the font.
Forcing the grid on every element costs more than it earns. Fix the elements that show up on every page, such as paragraphs, headings, and lists, and let the rest drift.
Spacing and the WCAG text-spacing check
WCAG 2.1 success criterion 1.4.12 (Text Spacing) requires that content survives user overrides: line height of 1.5 times the font size, paragraph spacing of 2 times, letter spacing of 0.12 times, and word spacing of 0.16 times. You can test your layout by applying those values.
p {
line-height: 1.5 !important;
letter-spacing: 0.12em !important;
word-spacing: 0.16em !important;
}
p + p {
margin-top: 2em !important;
}The paragraph spacing in this test, 2em, is bigger than your one-line gap. That is intentional, because the test checks that nothing breaks when the user asks for more room. To pass, your rhythm must not depend on fixed heights. Avoid height on text containers, and prefer min-height, margins, and padding in relative units.
Common errors
- Setting both top and bottom margins on paragraphs. In a normal block flow they collapse, but in a flex or grid parent they add up and double the gap.
- Setting a paragraph gap smaller than the visible leading, so paragraphs blend together.
- Using pixel values for margins while the text is in
rem. When the user changes the default font size, the line grows but the gap does not. - Adding both an indent and a gap.
- Treating a missed grid by a few pixels as a bug. Perfect baseline alignment is not a realistic target on the web.
Exercise
A paragraph has font-size 1.125rem and line-height 1.6. What bottom margin gives a gap of exactly one line, and how can you write it without calculating?
Show answer
One line is 1.125rem x 1.6 = 1.8rem, so margin-block: 0 1.8rem gives a one-line gap. Writing margin-block: 0 1lh gives the same result without calculating, and it stays correct if the line height changes.
Exercise
An h2 has line-height 2.25rem and margin-block 3rem 0.75rem. Body text uses a 1.5rem line and a 0.75rem half-unit. How many half-units does a one-line heading plus its margins occupy, and why does that keep the following paragraph on the grid?
Show answer
The heading line is 3 half-units (2.25rem), the top margin is 4 (3rem), and the bottom margin is 1 (0.75rem), for a total of 8 half-units, which is 6rem. That is a whole number of half-units, so the paragraph after it starts on the same grid as the paragraphs above. If the heading wraps to two lines, it adds another 3 half-units, which still keeps the grid intact.