← All writing
Craft · · 2 min

text-wrap: balance and pretty

One is for headings, one is for paragraphs, and using the wrong one costs you performance for nothing.

CSS Typography Small Print

A heading that wraps to two lines with one word on the second is the single most common piece of ugliness on a website, and for years the fix was a non-breaking space typed in by hand, which then survived a copy edit by nobody.

wrap.css
h1, h2, h3 {
  text-wrap: balance;
}
CSS

balance evens the lines out so a heading breaks somewhere sensible. It is capped (browsers stop at a handful of lines, around four in practice) because balancing is expensive and the layout cost on a long paragraph would be real.

wrap.css
p {
  text-wrap: pretty;
}
CSS

pretty is the one for body text. It does not even the lines; it looks ahead far enough to avoid a single-word last line and some of the worse breaks. It is cheap enough to put on every paragraph.

So the rule is just: balance for things with two or three lines, pretty for things with ten. Putting balance on body copy is the mistake to avoid. It fails quietly by doing nothing on anything past the line cap.

Both degrade to normal wrapping in a browser that does not know them, which makes this about the lowest-risk thing you can add to a stylesheet. I put the two rules in the base layer of a project now and never think about them again.

Read similar posts
2 min

word-break and overflow-wrap

Two properties that sound like synonyms, do different things, and get reached for in the wrong order roughly every time a long URL breaks out of a card.

2 min

light-dark()

Two color values in one declaration, picked by the color scheme, which removes most of the reason a theme needed a second block of custom properties at all.