Skip to content
Tutorials Center
My learning

Topics

Lesson 44 of 139 ยท Text and Fonts

Wrapping, Overflow and Hyphens

Control how text breaks with white-space, overflow-wrap, word-break and hyphens, truncate with ellipsis, clamp to a few lines, and balance headings with text-wrap.

Browsers wrap text automatically: when a line is full, the next word moves down. Most of the time that's exactly what you want. But real content is messy. A long URL breaks out of a narrow card, a product name needs to stay on one line, a heading wraps with one lonely word on its last line, and a German compound word refuses to fit anywhere.

CSS gives you precise control over all of these. In this lesson you'll learn how whitespace is handled, how to break long words safely, how to add hyphens, how to cut text off with an ellipsis or after a few lines, and how the newer text-wrap values make headings and paragraphs look more professional.

How whitespace is handled: white-space

In HTML, multiple spaces and line breaks in your source collapse into a single space, and lines wrap wherever there is room. The white-space property changes those two rules:

Value Spaces and tabs Line breaks in source Wraps automatically
normal collapse collapse yes
nowrap collapse collapse no
pre kept kept no
pre-wrap kept kept yes
pre-line collapse kept yes
break-spaces kept (and can wrap) kept yes

The ones you'll reach for most often:

  • nowrap keeps short UI text on one line: buttons, badges, table headers, prices.
  • pre-wrap shows user-written text (comments, messages) with their line breaks, while still wrapping long lines.
  • pre-line keeps line breaks but tidies up extra spaces. Handy for addresses or poems typed into a textarea.
white-space values

The same text, with extra spaces and a line break, renders six ways. nowrap and pre run straight out of the yellow box.

white-space is now defined as a shorthand for two newer properties: white-space-collapse (whether spaces collapse) and text-wrap-mode (wrap or nowrap). You can keep using white-space; it's supported everywhere.

Breaking long words

Wrapping happens at "soft wrap opportunities", mostly spaces and hyphens. A very long word or URL has none, so it overflows its box. Two properties fix that, in different ways.

overflow-wrap: anywhere / break-word

overflow-wrap lets the browser break inside a word only if that word can't fit on a line by itself. Normal words wrap as usual.

CSS
.comment {
  overflow-wrap: anywhere;
}
  • overflow-wrap: break-word breaks long words when needed, but doesn't consider those breaks when measuring the element's minimum size.
  • overflow-wrap: anywhere does the same and does count them, so a flex or grid item can shrink properly instead of being pushed wide by a long URL.

For user-generated text, overflow-wrap: anywhere is the safest default.

word-break

word-break is blunter:

  • word-break: break-all allows breaks between any two characters, even in short words that would fit on the next line. It's useful for long strings like hashes or IDs, but it makes normal text harder to read.
  • word-break: keep-all prevents breaks inside words in Chinese, Japanese and Korean text, where the browser otherwise breaks between any characters.

You may see word-break: break-word in old code; it's deprecated and acts like overflow-wrap: anywhere with word-break: normal.

Taming a long URL

The default card lets the URL escape. overflow-wrap: anywhere breaks only the URL; break-all also chops ordinary words at the edge of each line.

Hyphens

For long words in normal prose, hyphenation is gentler than a hard break. The hyphens property controls it:

  • manual (default): break only where you placed a soft hyphen, ­, in the HTML.
  • auto: the browser adds hyphens using a dictionary for the content's language.
  • none: never hyphenate, even at ­.

hyphens: auto only works if the page declares its language, such as <html lang="en"> (see Global Attributes). Dictionary coverage differs between browsers and languages, so treat it as an enhancement.

CSS
.article p {
  hyphens: auto;
}

Hyphenation shines in narrow columns and justified text, and in languages with long compound words like German or Finnish.

Truncating with an ellipsis

To show a single line of text and cut off the rest with "โ€ฆ", you need three properties together:

CSS
.truncate {
  white-space: nowrap;       /* keep it on one line */
  overflow: hidden;          /* hide what doesn't fit */
  text-overflow: ellipsis;   /* show โ€ฆ at the cut */
}

text-overflow does nothing on its own: the text must overflow (it can't wrap) and the overflow must be clipped. The element also needs a limited width, from a fixed width, max-width or its container. You learned the overflow part in Overflow.

Truncated text is hidden from sighted users. Make sure the full text is available somewhere: on a detail page, in an expanded view or, at least, in a title attribute. Don't truncate content people need to read in full.

Clamping to several lines

Sometimes you want two or three lines, then an ellipsis: think of card descriptions or article previews. The widely supported way uses line-clamp with legacy -webkit- prefixed properties, which work in every current browser:

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

It looks odd, but it's the standard, interoperable pattern. An unprefixed line-clamp property is being specified; until it's widely available, keep the prefixed version.

Ellipsis and line clamping

The heading stops at one line, the description at two. Change -webkit-line-clamp to 3, or widen max-width, to see the effect move.

Better line breaks with text-wrap

The text-wrap property (or its longhand, text-wrap-style) lets the browser choose line breaks more carefully:

  • balance evens out the line lengths of short, multi-line text. A heading that wraps as "a long line" plus "one word" becomes two lines of similar length. Browsers limit it to a few lines (around 4โ€“6), so use it for headings, captions and pull quotes, not paragraphs.
  • pretty uses a slower algorithm that mainly avoids a single short word alone on the last line (an "orphan"), and may improve breaks elsewhere. It's meant for body text.
  • wrap (default) and nowrap are the classic behaviors.
CSS
h1, h2, h3 { text-wrap: balance; }
p          { text-wrap: pretty; }

balance is supported in all current browsers. pretty works in Chromium-based browsers and Safari; elsewhere it simply falls back to normal wrapping, so it's safe to add today.

text-wrap: balance

The first heading leaves a short last line; the balanced version shares the words evenly. Edit the text and the width to compare different cases.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. You want to display a user's comment exactly with its line breaks, while still wrapping long lines. Which value fits?

    pre-wrap keeps spaces and line breaks and still wraps at the edge of the box. pre would not wrap.
  2. 2. A long URL overflows a narrow card. Which property breaks only the URL and leaves normal words alone?

    overflow-wrap breaks inside a word only when it can't fit on a line by itself. break-all breaks any word at the line edge.
  3. 3. text-overflow: ellipsis has no effect. Which is the most likely cause?

    An ellipsis needs text that overflows on one line (white-space: nowrap) and clipped overflow (overflow: hidden).
  4. 4. What does hyphens: auto need to work well?

    Automatic hyphenation uses a dictionary for the content's language, which the browser learns from lang.
  5. 5. Where should you use text-wrap: balance?

    Balancing is limited to a few lines and is designed for headings, captions and similar short text. pretty is meant for paragraphs.

Summary

  • white-space decides whether spaces collapse, whether source line breaks are kept, and whether lines wrap.
  • overflow-wrap: anywhere breaks only words that don't fit; word-break: break-all breaks anywhere.
  • hyphens: auto adds dictionary-based hyphens when the language is declared with lang.
  • A one-line ellipsis needs white-space: nowrap, overflow: hidden and text-overflow: ellipsis.
  • Multi-line clamping uses display: -webkit-box, -webkit-box-orient: vertical, -webkit-line-clamp and overflow: hidden.
  • text-wrap: balance evens out headings; text-wrap: pretty avoids orphans in paragraphs.

Next: Text Shadows, for depth, glows and better legibility on images.