Favorites

Using text-overflow and line-clamp Properties

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
text-overflow line-clamp

Why trim text with CSS?

Layouts often need text to fit a fixed space: a card title that must stay on one line, or a summary that shouldn't be longer than three lines. This matters most on sites managed through a CMS (content management system), where editors can type any amount of text and the design still needs to look neat and line up.

CSS can trim text to fit its container, whatever the number of characters, and add an ellipsis (…) to show that something has been cut off. Two properties do the job: text-overflow for a single line, and line-clamp for several.

Trimming one line with text-overflow

Let's build it up step by step. Here's a paragraph in a container with a limited width. By default, the text simply wraps onto new lines:

Results

CSS gives you several ways to deal with text that is too long for its container. You can let it wrap onto new lines, hide whatever doesn’t fit, or neatly trim it with an ellipsis so readers know there’s more. Trimming is especially handy for card titles, product descriptions and news summaries, where every item needs to line up neatly, however much text an editor types in.

Starting point: long text wraps inside the container.

Step 1: stop the text wrapping

white-space: nowrap keeps all the text on one line. Now it runs straight past the edge of its box:

Results

CSS gives you several ways to deal with text that is too long for its container. You can let it wrap onto new lines, hide whatever doesn’t fit, or neatly trim it with an ellipsis so readers know there’s more. Trimming is especially handy for card titles, product descriptions and news summaries, where every item needs to line up neatly, however much text an editor types in.

white-space: nowrap keeps the text on a single line.

Step 2: hide the overflow

overflow: hidden cuts off anything that doesn't fit inside the box:

Results

CSS gives you several ways to deal with text that is too long for its container. You can let it wrap onto new lines, hide whatever doesn’t fit, or neatly trim it with an ellipsis so readers know there’s more. Trimming is especially handy for card titles, product descriptions and news summaries, where every item needs to line up neatly, however much text an editor types in.

overflow: hidden hides the text that doesn’t fit.

Step 3: add the ellipsis

Finally, text-overflow: ellipsis replaces the last few characters that fit with "…", so readers can see the text continues:

Results

CSS gives you several ways to deal with text that is too long for its container. You can let it wrap onto new lines, hide whatever doesn’t fit, or neatly trim it with an ellipsis so readers know there’s more. Trimming is especially handy for card titles, product descriptions and news summaries, where every item needs to line up neatly, however much text an editor types in.

text-overflow: ellipsis adds "…" where the text is cut off.

Here's the complete code. All three lines are needed: text-overflow does nothing on its own.

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

Trimming several lines with line-clamp

For longer text, such as a summary on a card, you usually want to allow a few lines before trimming. That's what line-clamp does: it limits text to a set number of lines and adds an ellipsis at the end of the last one.

The starting point, a paragraph that wraps onto many lines:

Results

CSS gives you several ways to deal with text that is too long for its container. You can let it wrap onto new lines, hide whatever doesn’t fit, or neatly trim it with an ellipsis so readers know there’s more. Trimming is especially handy for card titles, product descriptions and news summaries, where every item needs to line up neatly, however much text an editor types in. It keeps layouts tidy and predictable, which matters most on sites where the content changes often and nobody can check every page by hand.

Starting point: text wraps onto many lines.

And here it is clamped to three lines:

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

CSS gives you several ways to deal with text that is too long for its container. You can let it wrap onto new lines, hide whatever doesn’t fit, or neatly trim it with an ellipsis so readers know there’s more. Trimming is especially handy for card titles, product descriptions and news summaries, where every item needs to line up neatly, however much text an editor types in. It keeps layouts tidy and predictable, which matters most on sites where the content changes often and nobody can check every page by hand.

The text is limited to three lines, ending with an ellipsis.

Why the strange -webkit- properties?

Line clamping started life as a WebKit-only feature, which is why it needs the -webkit- vendor prefix and the old display: -webkit-box layout mode to work. It became so popular that every major browser, including Firefox, now supports this exact combination, and the specification promises it will keep working.

The standard, unprefixed line-clamp property is being added to browsers but isn't supported everywhere yet. Including both versions, as above, means the prefixed version works today while browsers that understand the standard one can use it.

Correction: the original version of this post said line-clamp was only an "Editor's Draft" and needed the prefix in all browsers. Things have moved on: the prefixed version is now reliably supported everywhere, and the unprefixed property is on its way.

The standard property can do more

The unprefixed line-clamp property will also let you choose what appears at the cut-off point, such as no ellipsis at all or your own text:

.summary {
  line-clamp: 3 "… (continued)";
}

Support for these extra options is still limited, so treat them as a bonus for now.

Common gotchas

Trimming inside flexbox and grid

The most common surprise: text-overflow doesn't seem to work inside a flex or grid layout. Flex and grid items won't shrink smaller than their content by default. So if the truncated text sits inside a flex item, such as a title inside a card's text area, the long text stretches that item instead of being trimmed. Adding min-width: 0 to the flex item fixes it:

<div class="card">          <!-- display: flex -->
  <img class="card-icon" ...>
  <div class="card-body">   <!-- the flex item -->
    <h3 class="truncate">A very long card title...</h3>
  </div>
</div>
.card { display: flex; }

.card-body {
  min-width: 0;  /* lets the flex item shrink below its content's width */
}

.truncate {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
Results

Without min-width: 0, this long title gets chopped off with no ellipsis at all

With min-width: 0, this long title is trimmed neatly with an ellipsis

The element needs a width limit

Truncation only happens when text hits the edge of its box. If the element is free to grow, such as an inline element or a table cell with automatic widths, there's nothing to trim against. Give it a width or max-width, or use table-layout: fixed on tables.

Padding on clamped text

With line-clamp, bottom padding on the same element can reveal the top of the hidden fourth line. Put the padding on a wrapper element instead, or keep it to the sides.

Adding a "Read more" toggle

Trimmed text works best when readers can still get to the full version. A checkbox and its label can toggle the clamp on and off, with no JavaScript:

<div class="expandable">
  <input type="checkbox" id="more-1" aria-label="Show the full text">
  <p class="clamp">Long text...</p>
  <label for="more-1" aria-hidden="true"></label>
</div>
.expandable label::after {
  content: "Read more";
}
.expandable input:checked ~ .clamp {
  -webkit-line-clamp: unset;
  line-clamp: none;
}
.expandable input:checked ~ label::after {
  content: "Show less";
}
Results

CSS gives you several ways to deal with text that is too long for its container. You can let it wrap onto new lines, hide whatever doesn’t fit, or neatly trim it with an ellipsis so readers know there’s more. Trimming is especially handy for card titles, product descriptions and news summaries, where every item needs to line up neatly, however much text an editor types in. It keeps layouts tidy and predictable, which matters most on sites where the content changes often and nobody can check every page by hand.

The checkbox is visually hidden but still reachable with the keyboard. When it's ticked, the ~ combinator removes the clamp from the paragraph that follows, and the label's text changes.

Tip: often the simplest "Read more" is just a link to the full article. Use an expand toggle when the full text belongs on the same page, such as a long product description.

Accessibility tips

  • Screen readers still read everything. Truncation is purely visual, so people using screen readers (software that reads pages aloud) hear the full text. That's good, but it means sighted readers are the ones missing out.
  • Give everyone a way to see the full text. Link to the full page, add an expand toggle, or show the full text elsewhere. A title attribute shows a tooltip on hover, but it doesn't help keyboard or touch users, so don't rely on it alone.
  • Don't trim essential information. Prices, dates, error messages and button labels should always be shown in full.
  • Test with larger text. Visitors who enlarge their text will see fewer words in the same space, so check that trimmed content still makes sense.

Wrapping up

For a single line, combine white-space: nowrap, overflow: hidden and text-overflow: ellipsis. For several lines, use the -webkit-line-clamp combination alongside the standard line-clamp property. Remember min-width: 0 inside flex layouts, and always give readers a way to see the full text.

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee