← Tous les articles

CSS: truncate text with an ellipsis on one or several lines (text-overflow, line-clamp)

How to cut a title or excerpt cleanly with "…" in CSS: the single-line recipe, multi-line truncation with line-clamp, and the cases where it silently fails (flexbox, tables, padding).

CSS: truncate text with an ellipsis on one or several lines (text-overflow, line-clamp)

Key points

  • On a single line you need three properties together: white-space: nowrap, overflow: hidden and text-overflow: ellipsis, on an element with a constrained width.
  • For several lines, -webkit-line-clamp with display: -webkit-box is still the reliable method across current browsers.
  • Inside a flex container add min-width: 0; in a table, table-layout: fixed. And make sure the full text is still reachable somewhere.

Article cards whose titles run to one, two or four lines, so the grid no longer lines up. A never-ending file name that pushes a button off screen on mobile. Both problems come up on nearly every project we build, and the usual fix is to cut the text with an ellipsis. CSS can do it without any JavaScript, but each technique has preconditions, and when “it doesn’t work” it’s almost always one of those that’s missing. Here are the recipes we use, the traps we keep running into, and the limits worth knowing so you don’t end up hiding information people need.

Single line: three properties that only work together

The classic recipe is three lines long, and all three are required:

.card-title {
  white-space: nowrap;      /* keep the text on one line */
  overflow: hidden;         /* hide whatever overflows */
  text-overflow: ellipsis;  /* and replace it with "…" */
}

text-overflow on its own does nothing: it only describes how to render an overflow that already exists. Without white-space: nowrap, the text wraps and never overflows horizontally. Without overflow: hidden, it overflows but stays visible.

The element also needs a constrained width. A block element takes its parent’s width, which is usually enough. An inline element such as a <span> or <a> has no width of its own: switch it to display: block or inline-block with a max-width, or nothing happens.

Something people often forget: the ellipsis is purely visual. The full text is still in the HTML, read by screen readers, indexed by search engines and copied if the user selects it. That’s an advantage over cutting text on the server, but also a reason not to hide essential information this way.

Inside flex or grid: the min-width trap

This is by far the question we hear most: “I’ve added all three properties, the text still isn’t truncated and it breaks my layout”. Nine times out of ten, the element is a flex item. By default a flex item has a minimum width equal to its content (min-width: auto): it refuses to shrink below the length of its text, so there’s never any overflow to cut.

.file-row {
  display: flex;
  align-items: center;
  gap: .75rem;
}
.file-row .name {
  flex: 1;
  min-width: 0;             /* essential: otherwise the item refuses to shrink */
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.file-row .size { flex-shrink: 0; }

min-width: 0 lets the item shrink and the ellipsis appears. Meanwhile flex-shrink: 0 on the sibling stops it from being squashed instead. The same logic applies to grid: a 1fr track also has a content-based minimum, so you write minmax(0, 1fr) to let it shrink.

The culprit can sit one level up: if the text lives in a wrapper that is itself a flex item, that wrapper needs min-width: 0. In the browser inspector, walk up the parents and look for the one that’s wider than expected.

Several lines: line-clamp

To limit an excerpt to three lines, use line-clamp. The standard property spent a long time in specification, and the form that works in every current browser is still the prefixed one, which relies on the old box model:

.excerpt {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;    /* number of visible lines */
  line-clamp: 3;            /* standard property, for the future */
  overflow: hidden;
}

The three prefixed declarations go together: without display: -webkit-box and -webkit-box-orient: vertical, the line count is ignored. Adding the unprefixed line-clamp does no harm and will take over where it’s supported.

A few traps we’ve seen in production:

  • Padding reveals the next line. Text is clipped at the edge of the box, padding included, so with bottom padding the top of the fourth line peeks through. Put the padding on a parent wrapper.
  • The display gets overridden. A more specific rule setting the element to display: block or flex turns the clamp off with no visible error.
  • Several paragraphs inside. Clamping works best on an element that directly contains the text. With several child blocks, results vary, so apply it to a single paragraph instead.
  • Matching card heights. Three clamped lines aren’t the same height as one line of text. To align cards, combine with grid and subgrid, or reserve the height with min-height: calc(3 * 1lh) where the lh unit is supported.

Tables, links, and keeping truncated text accessible

In a table, cells grow to fit their content, so the ellipsis never triggers. You have to change the table’s width algorithm:

table.list { table-layout: fixed; width: 100%; }
table.list td.description {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

With table-layout: fixed, column widths come from the first row or the <col> elements rather than the content. Give short columns (date, amount) an explicit width and let the text column take the rest.

For the user, truncated text must still be reachable. Depending on context we use a link to the full page (article cards), a title attribute for file names in an admin interface (it doesn’t show on touch screens, so keep it for desktop tools), or a “Read more” button that removes the clamp:

<p class="excerpt" id="desc-42">…long text…</p>
<button type="button" class="read-more" aria-controls="desc-42" aria-expanded="false" hidden>
  Read more
</button>

<script>
document.querySelectorAll('.read-more').forEach((button) => {
  const text = document.getElementById(button.getAttribute('aria-controls'));

  // Only show the button if the text is actually cut off
  const check = () => {
    if (button.getAttribute('aria-expanded') === 'true') return;
    button.hidden = text.scrollHeight <= text.clientHeight + 1;
  };
  new ResizeObserver(check).observe(text);

  button.addEventListener('click', () => {
    const open = text.classList.toggle('excerpt--open');
    button.setAttribute('aria-expanded', String(open));
    button.textContent = open ? 'Show less' : 'Read more';
  });
});
</script>

<style>
.excerpt--open { -webkit-line-clamp: unset; line-clamp: none; }
</style>

The ResizeObserver hides the button when the text already fits in three lines, including after the window is resized: few things are more annoying than a “Read more” that reveals nothing. aria-expanded exposes the state to assistive technology.

Truncate in CSS or on the server?

CSS truncation adapts to the available space, which is ideal for responsive layouts: a title that fits on one line on a wide screen isn’t cut for nothing. On the other hand, the whole text is still sent to the browser. For a list of a hundred thousand-word excerpts, that’s wasted weight. In that case we first trim on the server to a sensible length, then let CSS handle the display. In WordPress:

<?php
// In the template: a server-side excerpt cut at 25 words
echo esc_html( wp_trim_words( get_the_excerpt(), 25, '…' ) );

Trim by words rather than characters so you don’t slice a word in half, and if you do trim characters in PHP, use mb_substr() rather than substr(), which can cut through a multi-byte character and leave an invalid symbol. The two approaches complement each other: the server limits volume, CSS handles presentation.

FAQ

Why isn’t text-overflow: ellipsis working?

Check in this order: are all three properties present, does the element have a constrained width (not a plain inline element), and if it sits in a flex or grid container, does it have min-width: 0? The browser inspector quickly shows which one is missing.

Can the ellipsis go in the middle, for example in a file name?

Not with CSS alone. You can split the name into two elements (a truncated start and an always-visible extension) inside a flex container, or compute the cut in JavaScript. The first option covers most needs.

Is line-clamp supported in every browser?

The prefixed -webkit-line-clamp, used with display: -webkit-box, works in current browsers, Firefox included. Add the standard property alongside it for the future.

Layouts that break with real content, titles that are too long or a mobile view that’s hard to tame? We design and build interfaces that hold up with real-world content, on WordPress and in your web applications.

Partager LinkedIn Facebook X WhatsApp
</>

webtestudio

Agence web : sites WordPress sur mesure, thèmes premium et logiciels SaaS.

Parler de votre projet →