← Back to HTML & CSS, page 7

html-css-0138 · Medium mid

How do text-wrap, overflow-wrap and hyphens control how text breaks into lines?

text-wrap: balance evens out lines in short headings, pretty improves paragraph breaks, overflow-wrap breaks long strings, and hyphens: auto adds hyphens using the language dictionary. text-wrap is newer, and older browsers just use normal breaks.

Full answer

Different properties solve different line-breaking problems.

  • text-wrap: balance asks the browser to make the lines of a short block about equal in length. It is meant for headings and captions, up to a few lines, because it is costly for long text. A heading is then not left with one lonely word on the last line.
  • text-wrap: pretty asks for better line breaks in paragraphs, for example avoiding a very short last line. It is a hint, and the exact result depends on the browser.
  • overflow-wrap: break-word (or anywhere) lets a very long word break so it does not overflow the box. It matters for URLs and user-generated text. The difference between the two is in how min-content size is computed: anywhere lets the long word count as small in the intrinsic size, which helps inside flex and grid items, while break-word does not.
  • word-break: break-all breaks anywhere, even in normal words. It is rarely wanted for English text.
  • hyphens: auto lets the browser add hyphens at correct places in a word. It needs a correct lang attribute, because the dictionary depends on the language. hyphens: manual uses only the soft hyphens (­) that you add yourself.
  • white-space: nowrap stops all wrapping, and text-overflow: ellipsis can then cut the line.

text-wrap is a newer property. Check the support table. Browsers that do not know it simply use the normal breaks, which is an acceptable fallback.

The two headings have the same text and the same width. The balanced one has lines that are closer in length, so its last line is wider than the plain one. The long address wraps inside its 120px box. The hyphenated word may break with a hyphen where the browser has a dictionary for English.

Hyphenation depends on the browser and the language data, so test it on your target browsers.

html
<style>
  .box { width: 220px; font: 20px/1.3 sans-serif; background: #eef; margin: 8px 0; }
  .balance { text-wrap: balance; }
  .url { width: 120px; overflow-wrap: anywhere; background: #fee; }
  .hy { width: 90px; hyphens: auto; }
</style>
<h2 class="box" id="plain" style="margin:0">Write clean HTML and style it with simple CSS rules</h2>
<h2 class="box balance" id="bal" style="margin:0">Write clean HTML and style it with simple CSS rules</h2>
<div class="url" id="u">https://example.com/a/very/long/address/without/breaks</div>
<p class="box hy" id="h" lang="en">Internationalization</p>

Checked in Chrome 156 only. The balanced heading had a last line much wider than the plain one. Hyphenation depends on the browser and was only checked as a computed value. Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • balance is for headings and short blocks.
  • pretty is a hint for better paragraph breaks.
  • overflow-wrap handles long URLs and words.
  • hyphens: auto needs a correct lang.
  • Check support for text-wrap.

Common mistakes

  • Using word-break: break-all for normal text.
  • Using text-wrap: balance on a long paragraph.
  • Using hyphens: auto without a lang attribute.

Follow-ups

  • What is the difference between overflow-wrap: anywhere and break-word?
  • Why does hyphens: auto need lang?

Tips

  • Match each property to its problem: lonely last word, long URL, long word hyphenation.
  • Say that balance is for short blocks only.