text-overflow: ellipsis needs overflow hidden, nowrap and a width limit, and flex children need min-width 0. Also learn how to clamp text to several lines with an ellipsis.
The short answer
#text-overflow: ellipsis does nothing on its own. The text must be on one line, the box must hide its overflow, and the box must have a width it cannot grow past. Inside a flex container you usually also need min-width: 0.
_10.truncate {_10 overflow: hidden;_10 white-space: nowrap;_10 text-overflow: ellipsis;_10}
Why it happens
#text-overflow only describes what to draw when inline content overflows its box and that overflow is hidden. If any part of that is missing, there is nothing for it to act on:
- Text wraps. By default, long text wraps onto a new line, so it never overflows horizontally.
white-space: nowrapkeeps it on one line. - Overflow is visible. With the default
overflow: visible, the text simply spills out. It needsoverflow: hidden(orclip). - The box grows to fit. An inline element such as a
spanhas no width of its own, and a flex item will refuse to shrink smaller than its content. The text then never overflows, because the box keeps getting wider.
The flex case catches most people. Flex items have a default min-width: auto, which means "never shrink below the width of my content". A long unbroken line of text therefore pushes the item, and often the whole layout, wider instead of being truncated.
How to fix it
#Single line on a block element
Apply all three properties to an element that is block or inline-block and has a constrained width:
_10.title {_10 display: block;_10 max-width: 20rem;_10 overflow: hidden;_10 white-space: nowrap;_10 text-overflow: ellipsis;_10}
If the element is a span or a, it is inline by default, so add display: block or display: inline-block.
Inside a flex container
Add min-width: 0 to the flex item that holds the text, so it is allowed to shrink:
_12.row {_12 display: flex;_12 gap: 1rem;_12}_12_12.row__text {_12 flex: 1;_12 min-width: 0; /* let the item shrink below its content width */_12 overflow: hidden;_12 white-space: nowrap;_12 text-overflow: ellipsis;_12}
If the text is nested deeper, every flex item between the container and the text may need min-width: 0. The same applies to grid items, where minmax(0, 1fr) in the track definition does the same job:
_10.grid {_10 display: grid;_10 grid-template-columns: 3rem minmax(0, 1fr);_10}
In a table cell
Table cells grow to fit their content. Give the table a fixed layout and the cell a width:
_10table {_10 table-layout: fixed;_10 width: 100%;_10}_10_10td.name {_10 overflow: hidden;_10 white-space: nowrap;_10 text-overflow: ellipsis;_10}
Clamp to several lines
For two or three lines followed by an ellipsis, use line clamping. The prefixed form is the one that works across all current browsers:
_10.excerpt {_10 display: -webkit-box;_10 -webkit-box-orient: vertical;_10 -webkit-line-clamp: 3;_10 overflow: hidden;_10}
Do not add white-space: nowrap here, because the text needs to wrap to fill the lines. Despite the -webkit- prefix, this is specified for compatibility and is supported in Chrome, Edge, Firefox and Safari.
Common pitfalls
#- Applying it to the parent instead of the text element. The properties go on the box that directly contains the overflowing text.
- Missing
min-width: 0in nested flex layouts. One forgotten level is enough to stop truncation. - Padding on the clamped element. With line clamping, bottom padding can reveal part of the next line. Put the padding on a wrapper instead.
- Hiding important information. Truncated text is still in the DOM and read by screen readers, but sighted users cannot see it. Add a
titleattribute or a way to expand the content if the full text matters. - Expecting it to work with
overflow: auto****. Scrollable overflow does not show an ellipsis in a useful way. Usehiddenorclip.
Related reading
#Was this resource helpful?