Sub topic 11
Keeping line length readable with max-w-prose and max-w-* utilities
This chapter assumes you know the CSS box model, can read Tailwind class lists, and have used w-* and max-w-* utilities. Here you will apply them to limit how wide a block of text can grow.
Why line length matters
Readers lose their place when a line holds too many characters. Typography guides usually recommend about 45 to 75 characters per line, and 60 to 70 is a common target. A paragraph that stretches across a wide monitor can run past 150 characters per line, which is tiring to read.
The fix is to cap the width of the text container, not to change the font size. The cap is a maximum, so on a narrow phone screen the text still shrinks to fit.
The max-w-* utilities
max-w-* sets the CSS max-width property. Two groups are useful for text.
| Class | CSS | Rough use |
|---|---|---|
| `max-w-prose` | `max-width: 65ch` | Body text |
| `max-w-md` | `max-width: 28rem` | Narrow column, forms |
| `max-w-xl` | `max-width: 36rem` | Medium column |
| `max-w-3xl` | `max-width: 48rem` | Wide column |
| `max-w-none` | `max-width: none` | Removes a cap |
The ch unit equals the width of the "0" character in the element's font. So 65ch is about 65 characters wide, and it scales when the font size changes. A rem cap such as max-w-xl stays at 36rem no matter what font size the text uses, so the character count per line changes with the text size.
Centering the capped block
A block with a maximum width sits at the left edge of its parent. Add mx-auto to center it.
<article class="mx-auto max-w-prose px-4">
<h1 class="text-3xl font-bold">Reading comfortably</h1>
<p class="mt-4 text-base/7 text-gray-700">
This paragraph never grows wider than about 65 characters per line,
even on a very wide screen. On a phone it fills the available width.
</p>
</article>The px-4 keeps text away from the screen edge when the viewport is narrower than the cap. Without it, the text touches the edge on small screens.
Why max-w and not w
w-prose does not exist in the default theme. Even if you set a fixed width, the block would overflow a narrow screen. max-w-* only applies when the parent is wider than the limit, so it works at every screen size without extra breakpoints.
Capping text inside a wider layout
Often only the text needs a cap, while images or cards in the same section use the full width. Put the cap on the text elements, not on the section.
<section class="mx-auto max-w-5xl px-4">
<img src="hero.jpg" alt="Mountain lake" class="w-full" />
<p class="mt-6 max-w-prose text-lg/8">
The image spans the section, but this paragraph keeps a readable measure.
</p>
</section>Here the paragraph is left-aligned inside the wider section. Add mx-auto to the paragraph if you want it centered.
Common mistakes
- Putting
max-w-proseon an element with its own narrowermax-w-*class. The last matching rule in the generated CSS wins, which is not always the one written last in the HTML. - Using
max-w-proseon a flex or grid child and expecting it to shrink the column. The track size comes from the container, so cap the content inside the child instead. - Forgetting
mx-auto, which leaves a gap on the right of the page and looks off-center. - Setting
max-w-proseon text with a very large font. The cap is inch, so it adapts, but a heading withtext-5xlgets a much wider box than body text.
Exercise
A paragraph with the classes max-w-prose sits in a full-width page, but it hugs the left edge. Which class do you add to center it, and why does the paragraph still shrink correctly on a 320px phone?
Show answer
Add mx-auto, which sets the left and right margins to auto and centers the capped block. max-w-prose only limits the width to 65ch when the parent is wider. On a 320px screen the parent is narrower than 65ch, so the paragraph fills the parent and the cap has no effect.
Exercise
Write the classes for an article that is centered, never wider than 65 characters, and has 1rem of horizontal padding.
Show answer
mx-auto max-w-prose px-4