Truncate Text in CSS: text-overflow: ellipsis and line-clamp

By Marko Denic
Contents
  1. Single-line ellipsis
  2. Multi-line ellipsis with line-clamp
  3. When the ellipsis doesn’t show
  4. Inside flexbox: add min-width: 0
  5. Inside CSS Grid: use minmax(0, 1fr)
  6. Inside table cells
  7. On inline elements
  8. Part of the next line shows with line-clamp
  9. More truncation tricks
  10. Show the full text on hover
  11. A "Read more" toggle
  12. FAQ

To cut text off with an ellipsis (…) when it doesn’t fit on one line, you need three properties together:

Single-line ellipsis

white-space: nowrap keeps the text on one line, overflow: hidden hides what doesn’t fit, and text-overflow: ellipsis shows the “…” where it’s cut. The element also needs a width limit: a set width, a max-width, or a parent that constrains it.

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

Multi-line ellipsis with line-clamp

To show a fixed number of lines and end the last one with “…”, use -webkit-line-clamp. Despite the prefix, it works in every current browser, Firefox included. It needs display: -webkit-box and -webkit-box-orient: vertical alongside it.

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

Demo:

Single line: this sentence is far too long to fit in the box, so it ends with an ellipsis.

Two lines: line-clamp keeps the first two lines of this paragraph and cuts off everything after them with an ellipsis, however long the text is and however wide the screen.

PDF quarterly-report-final-version-2-really-final.pdf 2.4 MB

Make the window narrower: all three truncate, and the file icon and size never get squeezed.

When the ellipsis doesn’t show

The three-line rule is easy. Making it work inside real layouts is where people get stuck.

Inside flexbox: add min-width: 0

The most common problem. A flex item won’t shrink below its content’s width by default (min-width: auto), so the text never overflows, it just pushes the layout wider. Add min-width: 0 to the flex item that should truncate. More on how items shrink in the flexbox guide.

css
.row {
  display: flex;
}

.row .title {
  min-width: 0;            /* the fix */
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

Inside CSS Grid: use minmax(0, 1fr)

Same cause, different fix: a 1fr column can’t shrink below its content. Define the column as minmax(0, 1fr), as explained in the CSS Grid guide.

css
.layout {
  display: grid;
  grid-template-columns: 200px minmax(0, 1fr);
}

Inside table cells

Table columns grow to fit their content. Give the table table-layout: fixed and a width, or put a max-width on the cell.

css
table {
  table-layout: fixed;
  width: 100%;
}

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

On inline elements

text-overflow only works on block containers. On a <span> or <a>, add display: block or inline-block with a max-width.

css
a.url {
  display: inline-block;
  max-width: 300px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  vertical-align: bottom;  /* keeps it on the text baseline */
}

Part of the next line shows with line-clamp

overflow: hidden clips at the padding edge, so any bottom padding on the clamped element reveals the top of the next line. Move the padding to a wrapper, or remove the vertical padding from the clamped element.

css
/* Shows a sliver of line 4 */
.excerpt { padding: 16px; -webkit-line-clamp: 3; /* ... */ }

/* Fixed: padding on the parent */
.card { padding: 16px; }
.card .excerpt { padding: 0; -webkit-line-clamp: 3; /* ... */ }

More truncation tricks

Show the full text on hover

Truncated text is still in the HTML, so screen readers and search engines get all of it. Sighted users don’t, so offer a way to see it: a title attribute, a tooltip, or an expanded state.

html
<span class="truncate" title="quarterly-report-final-version-2-really-final.pdf">
  quarterly-report-final-version-2-really-final.pdf
</span>

A "Read more" toggle

Remove the clamp when a class is added, and toggle that class from a button.

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

.excerpt.is-expanded {
  -webkit-line-clamp: unset;
}

FAQ

Why is text-overflow: ellipsis not working?

One of the three properties is missing (white-space: nowrap, overflow: hidden, text-overflow: ellipsis), the element has no width limit, it’s an inline element, or it’s a flex item without min-width: 0.

Can I add an ellipsis after multiple lines?

Yes, with -webkit-line-clamp plus display: -webkit-box, -webkit-box-orient: vertical and overflow: hidden. It’s supported in all modern browsers.

Can I truncate text in the middle?

Not with CSS; text-overflow only cuts at the end. For “start…end.pdf” style truncation, split the text into two spans and truncate only the first one, or do it in JavaScript.

Does truncated text hurt SEO?

No. The full text is in the HTML, so search engines read all of it. Only the visible rendering is cut.

Related: the original ellipsis and line-clamp tips, CSS Flexbox, CSS Grid and hide the scrollbar in CSS.