Skip to main content

CSS Text & Fonts

Text Wrapping

Written by Updated Text Wrapping exercises

Handle long words, truncation, balanced headings, and multi-line clamping without breaking layouts. The chapter covers long URLs, emergency breaks, language-aware hyphenation, balanced headings, ellipsis, and responsible multi-line clamping.

Your design was made with short, tidy text. Real text is a 60-character file name, a German label twice the length of the English one, or a URL with no spaces. CSS text wrapping decides what happens when that text arrives.

The safest order is: first let the text wrap so it never pushes its box wider. Only cut text off when the full version is still easy to reach. This lesson pairs well with CSS Overflow and Web Fonts & Typography.

Let long words break

Break long strings, balance headings

css

.message {
  overflow-wrap: anywhere;
  hyphens: auto;
}

.title {
  text-wrap: balance;
}

overflow-wrap: anywhere lets the browser break a word only when it cannot fit, which is exactly what long URLs and IDs need. hyphens: auto adds proper hyphens, but only if the page language is set, so make sure your <html> tag has a lang attribute. text-wrap: balance evens out the lines of a heading so you do not get one lonely word on the last line.

Cut text to one line with an ellipsis

Example

css

.file-name {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

All three lines are needed: nowrap keeps the text on one line, hidden cuts off what does not fit, and ellipsis shows the "…". Remember that the hidden part is gone for the reader. Use this for things like file names in a list, where the full name is shown somewhere else, such as on hover or on the next screen.

Show only the first few lines

Example

css

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

This keeps a card summary to three lines. The -webkit- names look old, but every modern browser supports them for this exact purpose.

Indent poems and addresses

text-indent with each-line indents the first line and every line after a line break you typed. It does not indent lines that wrapped on their own.

Example

css

.poem {
  white-space: pre-line;
  text-indent: 1.5em each-line;
}

CSS Text Wrap Quick Reference

What you wantCSS
Keep text on one linewhite-space: nowrap
Break a very long wordoverflow-wrap: anywhere
Keep line breaks from the sourcewhite-space: pre-line
Balance the lines of a headingtext-wrap: balance
Allow hyphenshyphens: auto
Break between any charactersword-break: break-all

Try it: wrap, nowrap and ellipsis

html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Example</title>
  <style>
    p { width: 180px; padding: 8px; border: 1px solid #94a3b8; }
    .wrap { overflow-wrap: anywhere; }
    .nowrap { white-space: nowrap; }
    .ellipsis { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  </style>
</head>
<body>
  <p class="wrap">Averyveryverylongwordwithoutanyspaces</p>
  <p class="nowrap">This line does not wrap and leaves its box.</p>
  <p class="ellipsis">This line is cut and ends with an ellipsis.</p>
</body>
</html>

overflow-wrap or word-break?

overflow-wrap: anywhere breaks a word only when the word does not fit, so normal sentences stay readable. word-break: break-all breaks any word that reaches the end of a line, even short ones, which looks broken in ordinary text. Start with overflow-wrap.

Why does text not wrap in a Flexbox item?

A flex item does not shrink below its content by default. Add min-width: 0 to the item so long text can wrap or be cut. See CSS Flex Items.

Text Overflow Properties

PropertyPurposeExample
overflow-wrapBreaks an otherwise unbreakable string when necessary.overflow-wrap: anywhere
word-breakControls where words may break.word-break: break-all
white-spaceControls collapsing and wrapping of white space.white-space: nowrap
text-overflowSignals clipped inline content.text-overflow: ellipsis
text-indent: each-lineIndents the first line and lines after forced breaks.text-indent: 1.5em each-line

Complete Example

Complete runnable example

html

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Text Wrapping example</title>
  <style>
    * { box-sizing: border-box; }
    body {
      margin: 0;
      padding: 2rem;
      font-family: system-ui, sans-serif;
      line-height: 1.5;
    }
    main { max-width: 70rem; margin-inline: auto; }
    article, section, aside, form, .card, .panel {
      padding: 1rem;
      border: 1px solid #cbd5e1;
      border-radius: 0.6rem;
    }

.message {
  overflow-wrap: anywhere;
  hyphens: auto;
}

.title {
  text-wrap: balance;
}

.file-name {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

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

.poem {
  white-space: pre-line;
  text-indent: 1.5em each-line;
}
  </style>
</head>
<body>
  <main><h1 class="title">A balanced heading for a narrow content card</h1><p class="message">https://example.com/a/very/long/unbroken/path/that/must/not/break/the/layout</p><p class="file-name">quarterly-financial-report-final-revised.pdf</p><p class="summary">This longer summary demonstrates a multi-line clamp while keeping the card height under control.</p></main>
</body>
</html>

Browser Support

Feature
Chrome
Edge
Firefox
Safari
overflow-wrap2318497
text-wrap: balance11411412117.5
text-indent: each-line14614612115

Versions show the first stable desktop release with unprefixed support. Data source: MDN Browser Compatibility Data 8.0.8. A partial-support note is included where it changes how the example behaves.

Before you ship

  • Test with the longest real text you can find, not "Lorem ipsum".
  • Never cut off error messages, prices or instructions. Use ellipsis and line clamps only for previews.
  • If text will not wrap inside a flex item, the fix is almost always min-width: 0 on that item.

Text Wrapping exercises

One line

Keep the text of the first box on one line.

The HTML your code works on
<style>
  .box { width: 160px; border: 1px solid black; }
</style>
<div class="box one">A short line that would wrap</div>
<div class="box two">Supercalifragilisticexpialidocious</div>
<h2 class="box three">A heading that runs over two lines</h2>

Result

Check your answer to see it here.