Text Wrapping and Truncation

Intermediate · 5 min

How the browser breaks lines, how to handle long words and overflowing text with overflow-wrap and ellipsis, and what text-wrap balance and pretty change.

Before thisFluid Typography with clamp()

Kevin Powell · 5 min

Text Wrapping and Truncation

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Kevin PowellReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Browsers break lines only at allowed break opportunities, normally spaces and hyphens. A long word or URL has none, so it overflows its box.
  • `overflow-wrap: break-word` breaks an overlong word only after the layout is calculated, so it does not shrink a flex or grid item's minimum width. `overflow-wrap: anywhere` counts the break opportunities in that calculation and does.
  • A single-line ellipsis needs three declarations together: `white-space: nowrap; overflow: hidden; text-overflow: ellipsis;`.
  • Multi-line truncation uses the prefixed `display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: N;` pattern, which all major browsers support. Unprefixed `line-clamp` is not yet usable everywhere.
  • `text-wrap: balance` evens out the lines of short blocks such as headings (limited to about 6 lines in Chromium and 10 in Firefox). `text-wrap: pretty` improves paragraph endings but is not in Firefox.

Text is allowed to overflow

Boxes in CSS are happy to be too small for their content. Text normally avoids the problem by wrapping at spaces. A string with no spaces, such as a long URL, an email address or a German compound noun, has nowhere to wrap, so it runs out of its box and keeps going. As the video’s description puts it, overflowing text is “actually a feature of CSS”, and the fix depends on the situation.

Controlling where a line may break

white-space sets whether text wraps at all. The default normal wraps; nowrap keeps one line; pre and pre-wrap preserve whitespace as written.

For long unbreakable strings there are two properties with similar names and different jobs.

overflow-wrap says what to do with a word that does not fit its line:

  • normal: do nothing and overflow.
  • break-word: break the word at an arbitrary point, but only if it would otherwise overflow.
  • anywhere: the same breaking, but the possible break points are counted when the browser calculates the box’s minimum content width.

That last difference matters in flexbox and grid, where items refuse to shrink below their min-content width. I put the same 34-letter word in two 50px-wide flex containers. The item with overflow-wrap: break-word stayed 215px wide and overflowed; the one with anywhere shrank to 50px and wrapped. If you use break-word in a flex or grid child and still see overflow, switch to anywhere or add min-width: 0 to the child.

word-break is blunter: break-all allows breaks between any two letters even when a space is available, which suits CJK text and little else. keep-all forbids breaks inside CJK words.

hyphens: auto inserts real hyphens at dictionary break points, but only if the element or page has a lang attribute and the browser has a dictionary for it. It is a good companion to justified text and narrow columns. Support depends on the platform, so don’t rely on it as the only safeguard.

Truncating with an ellipsis

Sometimes cutting the text is the point: a table cell, a file name, a card title.

.title {
  white-space: nowrap;      /* stay on one line */
  overflow: hidden;         /* clip what spills */
  text-overflow: ellipsis;  /* show … at the cut */
}

All three are needed. text-overflow only decorates text that is already clipped, and clipping needs both a one-line layout and an overflow value other than visible. I checked that the 100px box with a long sentence ends up with scrollWidth greater than clientWidth, which is the condition under which the ellipsis appears. A truncated title should usually be reachable some other way, such as a title attribute or a link to the full text, because a screen reader reads the whole string but sighted users cannot.

More than one line

text-overflow only handles one line. For two or three lines, the long-standing technique uses a prefixed line-clamp:

.excerpt {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
  overflow: hidden;
}

A box with a line-height of 20px and -webkit-line-clamp: 2 measured 40px high in Chromium. MDN’s compatibility data lists the prefixed form in Chrome (since 6), Firefox (68) and Safari (5). A plain line-clamp property exists in the specification, but in that data it is still behind a flag or in preview, and CSS.supports("line-clamp", "3") returned false in the Chromium I tested, so use the prefixed form.

Better line breaks: text-wrap

text-wrap is the newer property for how lines are broken, with these useful values:

  • balance: spreads the words so all lines are about the same length. Short blocks only. MDN says browsers limit it: six lines or fewer in Chromium, ten or fewer in Firefox. It is supported in Chrome 130 and Firefox 124 and Safari 17.5 for the longhand form.
  • pretty: has the browser favor better breaks in paragraphs, such as avoiding a lone short word on the last line. Chromium and Safari support it; Firefox does not, which is fine because it is an enhancement.
  • nowrap: shorthand for no wrapping.
h1, h2, h3, blockquote { text-wrap: balance; }
p { text-wrap: pretty; }

I measured the effect on a 300px-wide heading in Chromium: without balance the two lines were 278px and 92px wide, and with it they were 182px and 188px. Unsupported browsers fall back to normal wrapping, so these are safe to apply broadly. Don’t use balance on long text; beyond the line limit it does nothing and costs nothing, but adds no value either.

What the video covers

Kevin Powell’s “4 ways to deal with overflowing text” is about 4.5 minutes and is from November 2022. By its chapters: why this happens (0:50), overflow-x (1:20), break-word (1:40), min-content and fit-content (2:21), and container queries (3:07). It does not cover ellipsis truncation, line-clamp or text-wrap; those parts of this lesson are my own, tested in Chromium and checked against MDN. The runner-up is a 2023 two-minute clip on text-wrap: balance.

Where this fits

Wrapping and truncation are about the edges of a text box, which loops back to overflow and, for flex and grid items, to the Flexbox and Grid groups. The last typography lesson, writing modes and logical properties, covers text that does not flow left to right. For line spacing inside wrapped text, see text spacing and readability.

Check yourself

  1. 01A flex item containing a long URL pushes the layout wider than the screen, even with `overflow-wrap: break-word`. What change fixes it?

    Switch to `overflow-wrap: anywhere` (or add `min-width: 0` to the item). `break-word` does not reduce the item's min-content size, so the flex item will not shrink below the URL's full width, whereas `anywhere` does. I measured an item at 215px with `break-word` and 50px with `anywhere` in a 50px container.

  2. 02You set `text-overflow: ellipsis` and nothing shows. What is likely missing?

    The text needs to actually overflow on one line. `text-overflow` works only on a block that clips (`overflow` other than `visible`) and, for a single line, `white-space: nowrap` so the text does not just wrap. The standard recipe is all three together.

  3. 03How would you show a card description as at most 3 lines with an ellipsis?

    `display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 3; overflow: hidden;`. The prefix is still the supported way in all major browsers. I verified that two lines at a 20px line height give a 40px box.

  4. 04Where does `text-wrap: balance` help, and why not use it on every paragraph?

    It helps short blocks like headings, captions and pull quotes by evening out line lengths, avoiding a one-word last line. It is costly to compute, so browsers only apply it up to a few lines (six in Chromium, ten in Firefox); beyond that it silently does nothing, which makes it unsuitable for long paragraphs.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark