← Back to HTML & CSS, page 3

html-css-0060 · Easy fresher

How do you truncate long text with an ellipsis on one line or on several lines?

For one line use white-space: nowrap, overflow: hidden and text-overflow: ellipsis. For several lines use -webkit-line-clamp with display: -webkit-box and overflow: hidden. The full text stays in the DOM and only the display is cut.

Full answer

For one line, three properties work together: white-space: nowrap keeps the text on one line, overflow: hidden clips what does not fit, and text-overflow: ellipsis replaces the clipped end with three dots. The element needs a width limit, such as a fixed width, a percentage or a grid or flex cell that can shrink.

For several lines, the common method uses display: -webkit-box, -webkit-box-orient: vertical, -webkit-line-clamp: 2 and overflow: hidden. Despite the prefix, it is the method most sites use. A standard line-clamp property exists, but it is newer, so check the support table before relying on it alone.

The full text is still in the page and still read by screen readers and found by search. Only the display is cut. If users must be able to read the rest, give them a way: a link, a "Read more" button, or an expandable area.

The first paragraph stays on one line and ends in an ellipsis, so its scroll width is larger than its visible width. The second shows two lines of 20px each, so it is 40px tall, even though the text needs more lines.

Inside a flex item, remember to add min-width: 0 to the item so it is allowed to shrink. Without it the text pushes the container wider instead of being clipped.

html
<style>
  body { margin: 0; font: 16px/20px sans-serif; }
  .one {
    width: 200px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .two {
    width: 200px;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    overflow: hidden;
  }
</style>
<p class="one" id="one">Introduction to Data Structures and Algorithms, Lecture Notes for Semester Two</p>
<p class="two" id="two">Introduction to Data Structures and Algorithms, Lecture Notes for Semester Two, including worked examples and practice questions for every chapter.</p>

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

  • Single line needs nowrap, overflow hidden and text-overflow ellipsis.
  • Multi-line uses -webkit-line-clamp with display: -webkit-box and a vertical box orientation.
  • The element needs a width limit for clipping to happen.
  • The full text remains available to screen readers and search.
  • Flex items need min-width: 0 to be clipped.

Common mistakes

  • Forgetting white-space: nowrap for the one-line version, so the text wraps and no ellipsis appears.
  • Truncating important information, such as a price or a date, with no way to read the rest.
  • Applying text-overflow to a flex item without min-width: 0, so the item grows instead of clipping.

Follow-ups

  • Why does text-overflow: ellipsis do nothing on a flex item sometimes?
  • How would you let a user expand a clamped paragraph?

Tips

  • Write the three-line single-line recipe from memory, then add the line-clamp version.
  • Say that truncation changes only what is seen. Screen readers and search still get the full text.