Sub topic 10
Measure in CSS: max-width in ch
This chapter assumes you know what the measure is (the length of a line of text) and the usual target of about 45 to 75 characters for body text. It also assumes you can write basic HTML and CSS selectors and know the difference between px, rem, and em. Here you will turn the measure into a CSS rule and avoid the mistakes that make that rule behave differently from what you expected.
What the ch unit measures
The ch unit is a CSS length. One ch equals the advance width of the "0" (zero) glyph in the element's font. The advance width is the horizontal space the glyph takes, including its side spacing. The browser reads that width from the font in use at the element and the element's font size.
Because ch is tied to a character, it matches the way people describe measure. Guidelines say "about 65 characters per line", and max-width: 65ch states the same thing. A pixel value does not. 600px gives a different number of characters when the font size, the typeface, or the user's default size changes. A ch value changes with all three.
Here is the basic rule from the plan:
body {
font-size: 1rem;
line-height: 1.5;
}
p {
max-width: 65ch;
}Each p can now grow up to 65 zero-widths and no further. If the viewport is narrower, the paragraph shrinks with it, because max-width is only a ceiling.
ch is an approximation of characters
In most proportional fonts, the digit zero is wider than the average lowercase letter. Letters such as i, l, t, and r are much narrower. So a line limited to 65ch usually holds more than 65 characters of running text. A figure in the low 70s or higher is common, and it varies by typeface.
This does not make ch the wrong tool. It means you should treat the number as a setting to tune, not an exact count. Two practical consequences follow:
- If you want lines near 66 actual characters, a value of 55ch to 60ch may be closer in some typefaces. Check by looking at the rendered text.
- If you change typeface, recheck the measure. The same
65chgives different real line lengths in a narrow font and a wide one.
In a monospaced font, every glyph has the same width, so ch is exact. That is rare for body text.
Where ch gets its value
A ch value is resolved against the font of the element where the declaration is written. It is not resolved against the text that will end up inside. This is the most common source of surprises.
Consider this markup and CSS:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Measure demo</title>
<style>
body {
font-family: Georgia, serif;
font-size: 1rem;
line-height: 1.5;
}
.article {
max-width: 65ch;
margin-inline: auto;
padding-inline: 1rem;
}
.article .lead {
font-size: 1.5rem;
}
</style>
</head>
<body>
<main class="article">
<p class="lead">A lead paragraph with a larger type size.</p>
<p>Body text uses the container's font size, so its line length matches the 65ch limit on the container.</p>
</main>
</body>
</html>The 65ch sits on .article, whose font size is 1rem. The container is therefore about 65 zero-widths of the 1rem font wide. The .lead paragraph is set at 1.5rem, but it does not get a wider box. It fits inside the same container, so it holds fewer characters per line, roughly two thirds as many. That may be fine for a lead, which is often short. It matters when you expected the lead to have the same measure as the body.
To give an element its own measure, put the max-width on that element:
.article .lead {
font-size: 1.5rem;
max-width: 45ch;
}Now ch resolves against the 1.5rem font of .lead, so the limit is 45 zero-widths of the larger size.
Setting measure per component
Different kinds of text want different measures. The second example from the plan shows this together with leading:
body {
line-height: 1.5;
}
.article p {
max-width: 70ch;
line-height: 1.6;
}
.caption {
max-width: 45ch;
line-height: 1.4;
}The article paragraphs get a longer line and slightly more leading. The caption gets a shorter line and slightly less leading. Both values move together in the same rule. Keep the pairing in one place so that changing a measure reminds you to check the leading.
Container or paragraph?
You can limit the measure on the container or on each text element. Each choice has a trade-off.
| Approach | Good for | Watch out for |
|---|---|---|
| `max-width` on a wrapper such as `.article` | Simple pages where everything inside should share one column | Headings, figures, and tables are also limited, and `ch` uses the wrapper's font size |
| `max-width` on `p`, `li`, and similar elements | Pages where images, code blocks, and tables should be wider than the text | Each element needs the rule, and a paragraph inside a narrow parent is limited by the parent first |
| A custom property such as `--measure` | Reusing one value across many rules | Needs a fallback if you support very old browsers |
A custom property keeps the number in one place:
:root {
--measure: 65ch;
}
.prose > * {
max-width: var(--measure);
}
.prose > .wide {
max-width: none;
}The .prose > * selector limits every direct child of the prose container. The .wide class opts a child, such as a figure or a table, out of the limit.
Interaction with box sizing and padding
A max-width limits the content box by default. With box-sizing: content-box, which is the browser default, padding and borders are added to that width. A max-width: 65ch with padding-inline: 1rem produces a box that is 65ch plus 2rem wide, but the text still has 65ch of room.
Many sites set box-sizing: border-box for every element. Then the max-width includes the padding, so the text area is narrower than 65ch by the padding amount. A container with max-width: 65ch and 2rem of horizontal padding gives text lines of about 65ch minus 2rem.
Neither is wrong, but you should know which one applies. If you measure lines and they come out shorter than expected on a border-box page, padding is the likely cause. In that case, increase the max-width or move the padding to a parent element.
Centering the column
A max-width alone leaves the column against the left edge of its parent. In a wide viewport, this can leave a large empty area on the right. To center the column, use automatic inline margins:
.article {
max-width: 65ch;
margin-inline: auto;
padding-inline: 1rem;
}Here margin-inline: auto splits the remaining space between left and right. The padding-inline keeps text away from the screen edge on narrow screens, where the container is no longer limited by max-width. Whether to center depends on the design. Left-aligned columns next to a sidebar are common and work well.
Common errors
- Using
widthinstead ofmax-width. Withwidth: 65ch, the box stays 65ch wide even on a narrow phone, which causes horizontal scrolling. Usemax-widthso the box can shrink. - Using
emfor the measure. Oneemis the font size, which is wider than one character.65emis roughly twice as long as65chand gives a line of well over 100 characters. - Setting
chon an element with a different font from its text. Thechresolves from the element where it is written. If a child uses a different font family, the line length changes. - Forgetting that fallback fonts change the width. If a web font has not loaded, the fallback font's zero may be a different width. The column shifts slightly when the real font arrives. Matching the fallback's metrics reduces the shift.
- Limiting only paragraphs and forgetting lists. List items, block quotes, and definition lists are body text too. Include them in the same rule.
Checking the result
Open the page in a browser and inspect a paragraph. Count the characters in a few full lines. Choose lines in the middle of the paragraph, since the last line is short and the first may be indented. Lines should fall in the range you chose, for example 60 to 75. If most lines are far outside that range, adjust the ch value and look again.
A quick way to count in the browser console is to take a sample line and read its length:
const line = "Paste one full rendered line of text here, copied from the page.";
console.log(line.length);This gives the character count of the line you pasted, including spaces.
Exercise
A page sets .article { max-width: 65ch; } with the font size at 1rem. A paragraph inside has font-size: 1.25rem, and you want its line to be limited to 65 of its own zero-widths. What change would you make, and why does the original rule not do it?
Show answer
Put the limit on the paragraph itself, for example .article p { font-size: 1.25rem; max-width: 65ch; }. The ch value in the original rule resolves against the font of .article, which is 1rem, so the container is 65 zero-widths of the smaller size. The larger paragraph fits fewer characters on each line. Declaring max-width on the paragraph makes ch resolve against its 1.25rem font.
Exercise
A page uses * { box-sizing: border-box; } and .article { max-width: 65ch; padding-inline: 2rem; }. Measured lines come out shorter than expected. What is the cause, and what are two ways to fix it?
Show answer
With border-box, the max-width includes the padding, so the text area is 65ch minus 4rem (2rem on each side). To fix it, raise the max-width by the padding amount, for example with calc(65ch + 4rem), or move the padding to a parent element so the 65ch box holds only text.