CSS text-overflow Ellipsis: How to Truncate Text
CSS text-overflow: ellipsis truncates overflowing text with "…", but only with overflow and white-space set. The recipe, flexbox fixes, and accessibility.
The CSS text-overflow property controls what the browser shows when inline text overflows its container. Set to ellipsis, it replaces the clipped end of the text with ”…”, which is the standard way to truncate a long filename, email address, or table cell to a single line. The property does nothing on its own, though: it only takes effect when the element also clips its overflow and prevents the text from wrapping. Most “why isn’t my ellipsis working” questions come down to one of those missing pieces.
The three-line recipe
Single-line truncation needs three declarations working together, plus a constrained width:
.truncate {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
Each one has a job:
white-space: nowrapstops the text from wrapping onto a second line. Without it, the text simply wraps and never overflows horizontally.overflow: hiddenclips the content that extends past the box.text-overflowonly applies when the overflow is clipped, sovisible(the default) disables it. Any value other thanvisibleworks, includingclipandauto.text-overflow: ellipsistells the browser to render ”…” at the point where the text is cut off, instead of slicing a glyph in half.
The element also needs a width the text can exceed. A block-level element gets one from its parent automatically. An inline element like a <span> does not, because inline boxes grow to fit their content. Give it display: block or display: inline-block with a width or max-width.
If any of this feels unfamiliar, our guide to CSS overflow covers how clipping and scroll containers work.
Values of text-overflow
| Value | Effect |
|---|---|
clip | Default. Text is cut at the edge of the box, possibly mid-character. |
ellipsis | Clipped text is replaced with ”…” (U+2026). |
"string" | A custom string replaces the clipped text. Browser support is limited; Firefox has supported it for years, but don’t rely on it elsewhere. |
The specification also allows two values — one for each end of the line — but in practice only the end side is commonly used, and support for the two-value form is uneven.
Why it breaks inside flexbox
The most common failure: a truncated label inside a flex row refuses to shrink, pushing a neighboring button off-screen.
<div class="row">
<span class="name truncate">a-very-long-file-name-that-keeps-going.tar.gz</span>
<button>Download</button>
</div>
.row { display: flex; gap: 0.5rem; }
Flex items default to min-width: auto, which for most items means they cannot become narrower than their content’s minimum size. With white-space: nowrap, that minimum is the full length of the text, so the item never shrinks and never overflows.
The fix is to let the item shrink below its content size:
.name {
flex: 1 1 auto;
min-width: 0;
}
min-width: 0 (or setting overflow: hidden directly on the flex item, which also resets the automatic minimum) allows the item to shrink, at which point the text overflows and the ellipsis appears. If the truncated text is nested several levels deep, every flex item between the row and the text may need min-width: 0. The article on flex-grow, flex-shrink, and flex-basis explains the sizing algorithm behind this.
The same automatic-minimum behavior applies to grid items. In a grid, a column defined as 1fr resolves to minmax(auto, 1fr), so a long unwrappable string can blow out the track. Use minmax(0, 1fr) for the column, or min-width: 0 on the item.
Truncating table cells
Table cells size themselves to their content by default, so text-overflow appears to do nothing in a <td>. Two approaches work:
- Set
table-layout: fixedon the table and give columns explicit widths. Cells then respect those widths, and the truncation recipe works inside them. - Put the text inside a block element inside the cell, and give that element a
max-width.
Multi-line truncation is a different property
text-overflow is strictly single-line: it only affects text that overflows in the inline direction. To clamp a paragraph to three lines with an ellipsis at the end, you need line-clamp (still most widely used as the prefixed -webkit-line-clamp together with display: -webkit-box and -webkit-box-orient: vertical). That technique has its own quirks, which our guide to CSS line-clamp walks through.
If the goal is better-looking wrapped text rather than cut-off text, consider text-wrap: balance for headings instead of truncation.
Direction and logical layout
The ellipsis is placed at the end edge of the line, which depends on the text direction. In a right-to-left language, the ”…” appears on the left. This aligns with how CSS logical properties think about start and end rather than left and right, and it means a truncation utility class works across languages without changes.
A related trick: setting direction: rtl on left-to-right text to truncate the beginning of a string (useful for file paths, where the end matters most) can work visually, but it can reorder punctuation and confuse screen readers. Prefer trimming the string in JavaScript or using a middle-truncation approach for those cases.
Accessibility considerations
Truncated text is still present in the DOM. Screen readers announce the full string, and copying the text copies all of it — which is generally what you want. The cost falls on sighted users, who cannot see what was hidden.
Good practice:
- Expose the full text. Show it in a tooltip, an expandable row, or a detail view. A
titleattribute is a common fallback, but it is not reachable by keyboard or touch, so don’t rely on it alone. - Don’t truncate critical information. Prices, error messages, and the distinguishing part of a name (for example, the version suffix on a filename) should stay visible.
- Watch zoom and text resizing. Truncation that looks fine at default size can hide most of a label when a user enlarges text. Test at 200% zoom.
These points fit into broader web accessibility basics, where the guiding rule is that hiding information visually needs an alternate path to reach it.
A reusable utility
Most design systems ship a single class for this:
.truncate {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
min-width: 0;
}
Including min-width: 0 in the utility saves a debugging session every time it lands inside a flex or grid container, and it has no effect elsewhere.
The takeaway
text-overflow: ellipsis only works when the text cannot wrap (white-space: nowrap), the overflow is clipped (overflow: hidden), and the element has a width the text can exceed. Inside flex and grid layouts, add min-width: 0 so the item is allowed to shrink. For multiple lines, reach for line-clamp instead, and always give users a way to see the full text you have hidden.
Tagged
Keep reading
Takina · · 4 min read HTML details and summary Elements Explained
The HTML details and summary elements build native disclosure widgets and accordions with no JavaScript. How they work, how to style them, and pitfalls.
Takina · · 4 min read CSS pointer-events Explained: Click-Through and Its Traps
CSS pointer-events: none makes an element ignore mouse, touch, and pen input so clicks pass through. How it works, good uses, and why it doesn't disable.
Takina · · 4 min read CSS @starting-style Explained: Entry Transitions in CSS
CSS @starting-style defines an element's before-state on first render, so transitions can animate elements as they appear, even from display: none.