Note #04 ·
CSS text ellipsis not working: flex, grid, tables and more lines
The three declarations are rarely what is missing. Usually the box never gets narrower than the text, and the layout around it is the reason.
The usual recipe for an ellipsis is three declarations on one element:
.name {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
When the three dots still do not appear, the three declarations are rarely what is missing. text-overflow only paints the end of text that the box is already cutting off. If the box grows to fit the text, nothing is cut off and there is nothing to paint. Almost every broken ellipsis comes down to that: something lets the box get as wide as its content. Below are the places it happens, in the order people tend to run into them.
The element is inline
The CSS Overflow spec applies text-overflow to block containers. A span, an a or a strong is inline by default, so it has no width of its own to clip against, and overflow: hidden on it does nothing. The text runs on past the edge of whatever holds it.
Give the element a box: display: block when it sits on its own line, display: inline-block with a max-width when it sits in a sentence. A link in a navigation bar is the common case, and so is a label inside a button.
Nothing limits the width
A block element is as wide as its container, so it clips. An element that shrinks to fit its content does not, because white-space: nowrap makes that content one long line. That covers floats, absolutely positioned boxes without a right or a width, width: fit-content, and an inline-block with no max-width. Each of them gets wider until the line fits, and the ellipsis never has a reason to show.
The fix is any limit on the width: a width, a max-width in any unit, or a container that is itself limited.
Inside a flex row
This is the case that sends most people searching. A row with an avatar, a name and a button, the name set to truncate, and on a narrow screen the whole row overflows instead.
Flex items have an automatic minimum size. For an item whose overflow is visible, min-width: auto works out to its min-content width, and the min-content width of text that cannot wrap is the whole line. So the item refuses to shrink below the full name, and the row overflows.
When the ellipsis rule sits on the flex item itself, it fixes itself. overflow: hidden makes the automatic minimum zero, so the item can shrink, and the ellipsis appears. The trouble starts one level deeper: a flex item that holds a name and a role on two lines, each set to truncate. The item still has overflow: visible, so its minimum is still the longest line inside it. Give that item min-width: 0:
.row { display: flex; gap: 12px; }
/* lets the item shrink */
.row .text { min-width: 0; }
.row .name {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
Nested flex rows need it at every level. Each flex item between the row that has the space and the text that should truncate keeps its own automatic minimum, and any one of them left at auto blocks the rest. In a column layout the same thing happens with min-height.
Drag the corner of the box below to make it narrower. The first row has the three declarations on a name inside a flex item. The second is the same markup with min-width: 0 on that item. The third shows a filename cut in the middle, which is covered further down.
Margarethe Fontaine-Oyelaran
flex item left at min-width: auto
Margarethe Fontaine-Oyelaran
same item with min-width: 0
quarterly-report-final-FINAL-reviewed-v2.pdf
Inside a grid track
Grid items have the same automatic minimum, and 1fr does not help, because 1fr on its own means minmax(auto, 1fr). The track will not go below the min-content width of its widest item, and a nowrap line sets that width. Write the minimum out:
.layout {
display: grid;
grid-template-columns: 240px minmax(0, 1fr);
}
A min-width: 0 on the grid item does the same job from the other side. Pick one and use it everywhere, so the next person does not find both.
In a table cell
Tables are laid out by their content by default. With the automatic table layout a cell with nowrap text pushes its column as wide as the text, and max-width on a td is not reliably honored. The dependable fix is to stop the table sizing itself from content:
table { table-layout: fixed; width: 100%; }
td.path {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
With table-layout: fixed the column widths come from the table's width and the first row, so set widths on the columns that should not shrink and leave the truncating column to take what remains. The alternative is to put the three declarations on a div inside the cell and give that div a fixed or maximum width, which keeps the automatic layout for every other column.
More than one line
text-overflow handles one line and nothing else. For two or three lines and then the dots, use line clamping:
.excerpt {
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 3;
overflow: hidden;
}
The prefixed names are the ones that work in Chrome, Safari and Firefox, which added -webkit-line-clamp in version 68 for compatibility with existing sites. All four declarations are needed, and the clamped element should not have bottom padding, because the next line shows through it. The line clamp element has a working demo. Do not combine the clamp with white-space: nowrap: that leaves one line to clamp, and the single-line recipe already handles that.
Cutting in the middle
CSS puts the ellipsis at the end of the line, never in the middle. For filenames that loses the part people need, since quarterly-report… says nothing about whether it is the PDF or the spreadsheet. The two-value form text-overflow: ellipsis ellipsis, which clips both ends, only works in Firefox, and it still cannot keep the extension.
Splitting the name into two elements gets close without script. The first part truncates and the extension refuses to shrink:
<p class="file">
<span class="base">quarterly-report-v2</span>
<span class="ext">.pdf</span>
</p>
.file { display: flex; min-width: 0; }
.file .base {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
.file .ext { flex: none; }
The server or the template has to split the string, which is one lastIndexOf('.'). A true middle cut that keeps a few characters at both ends of an arbitrary string needs JavaScript that measures the text.
Right to left, and where the dots go
The ellipsis goes on the end edge of the line, and the end edge follows direction. In Arabic or Hebrew text it appears on the left. A path or a URL that should lose its beginning rather than its end can borrow that with direction: rtl, but the punctuation at the edges of the string can move. Test it with real paths before relying on it.
What the reader loses
The ellipsis is drawn, not written. The full text stays in the page, so copy, find in page and screen readers all get the whole name. Sighted mouse users get nothing unless you give it to them, and a title attribute only helps people with a mouse who wait for the tooltip. Where the cut-off part matters, such as two files whose names differ only at the end, show the full text somewhere: on focus, in a details row, or in a column wide enough to hold it.
Questions
Why is text-overflow: ellipsis not working?
The box is not narrower than the text, so there is nothing to cut off. Check four things: the element is a block or inline-block, something limits its width, it has overflow: hidden and white-space: nowrap, and in a flex or grid layout the item holding it has min-width: 0.
How do I add three dots to overflowing text in CSS?
Use overflow: hidden, white-space: nowrap and text-overflow: ellipsis on an element with a limited width. The dots are drawn at the end of the line where the text is cut.
Why does the ellipsis not work inside a flex item?
Flex items cannot shrink below their content width by default, because min-width is auto. Set min-width: 0 on the flex item that holds the text, at every nested level, and the ellipsis appears.
How do I truncate text to two or three lines with an ellipsis?
text-overflow only handles one line. For several, use display: -webkit-box, -webkit-box-orient: vertical, -webkit-line-clamp with the number of lines, and overflow: hidden. It works in Chrome, Safari and Firefox.
How do I make ellipsis work in a table cell?
Set table-layout: fixed and a width on the table, then put the three ellipsis declarations on the cell. With the default automatic layout, the column grows to fit the text instead.