Sub topic 10
Measure in CSS: max-width in ch
This chapter covers how to set the measure (line length) of body text in CSS. It assumes you know basic HTML and CSS, understand font sizes, and have read the earlier chapters on why a comfortable line holds roughly 45 to 75 characters.
Why characters, not pixels
The measure is a count of characters per line, so the best CSS unit for it is one that tracks characters. Pixels do not. A 600px column holds about 60 characters at 16px but only about 40 at 24px. The text got bigger, and the column stayed the same.
The ch unit solves this. One ch equals the advance width of the "0" (zero) glyph in the element's current font. Setting max-width: 65ch gives a box about as wide as 65 zeros in that font and size. If the font size changes, the box changes with it.
The basic pattern
body {
font-size: 1rem;
line-height: 1.5;
}
p {
max-width: 65ch;
}Use max-width, not width. A max-width lets the paragraph shrink on a narrow screen and stops it from growing past the limit on a wide one. A fixed width: 65ch would overflow a small phone screen.
Adjusting the measure and the leading together
Longer lines need more leading, because the eye travels farther to find the start of the next line. Shorter lines need a little less. Set both on the same selector so they stay linked.
body {
line-height: 1.5;
}
.article p {
max-width: 70ch;
line-height: 1.6;
}
.caption {
max-width: 45ch;
line-height: 1.4;
}The article text gets a wider measure and slightly more leading. The caption is narrower and gets slightly less.
Common errors
- Reading
chas an exact count. The "0" glyph is usually wider than the average letter in running text. A65chbox often holds 70 to 80 real characters in a proportional font. Treatchas a close approximation and check the result by eye. - Setting the limit on the wrong element. If you put
max-widthonbodyor a wrapper, thechis measured in that element's font. A wrapper with a larger font than its paragraphs gives a wider line than you expect. Put the limit on the element that holds the text, or make sure the font sizes match. - Limiting everything. Applying
max-width: 65chto all elements also narrows images, tables, and code blocks. Target paragraphs and other prose elements. - Forgetting centering. A capped paragraph sits at the left edge of a wide container. Center the container, or use
margin-inline: auto, if the design needs it.
Capping a whole prose container
If many elements share the same limit, set it once on a wrapper. Give the wrapper the same font size as its children so ch means the same thing.
.prose {
font-size: 1rem;
line-height: 1.5;
max-width: 65ch;
margin-inline: auto;
padding-inline: 1rem;
}The padding keeps the text off the screen edge on small devices. In this pattern, remember that max-width applies to the content box by default, so padding adds to the total width. If you use box-sizing: border-box, the padding comes out of the 65ch instead.
Exercise
A paragraph has width: 65ch and the page is viewed on a 320px-wide phone with a 16px font. What problem can occur, and what single change fixes it?
Show answer
At 16px, 65ch is roughly 520px or more, which is wider than the 320px screen, so the text overflows and forces horizontal scrolling. Change width to max-width, so the paragraph can shrink to fit the screen and still stops at 65ch on wide screens.
Exercise
You set max-width: 65ch on a wrapper that has font-size: 1.5rem, but its paragraphs have font-size: 1rem. Are the lines about 65 characters long?
Show answer
No. The ch is measured in the wrapper's 1.5rem font, so the box is wider than the paragraphs' own 65ch. The lines are longer than intended. Match the font sizes, or set the max-width on the paragraphs.