In CSS, why does text-overflow: ellipsis on its own not truncate a long line, and what else must be declared for it to work?
answer
- it is a rendering rule, not a truncation rule
- you must clip before you can style the clip
- wrapped text never overflows sideways
- shrink-to-fit boxes never exceed themselves
- block containers only
basics
~20 stext-overflow only styles text that the box is already clipping, so it needs overflow set to something other than visible, white-space: nowrap so the text stays on one line, and a width the line can actually exceed.
solid answer
~40 s`text-overflow: ellipsis` does not truncate anything — it only decides how *already clipped* inline content is rendered at the clip edge. So three things have to be true first. The box must clip: `overflow: hidden` (or `clip`, or a scrolling value). The text must overflow horizontally rather than wrap, which normally means `white-space: nowrap`, because wrapped text simply grows the box taller instead of running past the edge. And the box needs a constraint the line can exceed — an explicit width, a `max-width`, or a width imposed by its layout context; an auto-width block in normal flow already has one, but a shrink-to-fit box grows to the text instead. `text-overflow` also applies to block containers, so an inline element needs `display: inline-block` or `block` first.
code
css · 6 lines.cell-title {
max-inline-size: 24ch;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}go deeper
Memorise the trio — overflow: hidden, white-space: nowrap, text-overflow: ellipsis — and be able to say that the first two do the clipping while the third only styles the edge.
Explain why each ingredient is needed, including the width constraint, and diagnose the shrink-to-fit case where a box grows to its text and therefore never overflows.
Discuss the consequences on a real UI: truncated text stays selectable and readable by assistive technology, when a tooltip or full-value reveal is required, and the tradeoffs of the prefixed line-clamp technique.
Set the policy for truncation across a design system — which surfaces may truncate at all, how titles and user-generated strings degrade at small widths, and whether truncation ever happens server-side instead.
## What the property actually does `text-overflow` is easy to misread as "truncate the text". It is not a truncation property. Its job is purely cosmetic: when inline content is *already being clipped* by the element's overflow, `text-overflow` decides what appears at the clip edge. `clip` (the initial value) chops the glyph mid-stroke; `ellipsis` renders a `…` in place of the last glyphs that fit; a string value renders that string instead. Everything else — deciding that text overflows at all — is the job of other properties. That is why a lone `text-overflow: ellipsis` reliably does nothing. ## The three prerequisites ```css .truncate { overflow: hidden; /* something must clip */ white-space: nowrap; /* the line must not wrap */ text-overflow: ellipsis; max-inline-size: 20ch; /* the line must be able to exceed the box */ } ``` **1. The box must clip.** With the default `overflow: visible` nothing is clipped, so there is no clip edge to draw an ellipsis at. `hidden` is the usual choice; `clip` and the scrolling values also clip, though putting an ellipsis on a scrollable box is odd — the user can scroll to the text the ellipsis claims is missing. **2. The line must not wrap.** With normal white-space handling, a long sentence wraps into more lines and the box grows in the block direction instead of overflowing in the inline direction. There is nothing sticking past the inline edge, so nothing gets clipped. `white-space: nowrap` forces a single line. (`overflow-wrap` and `word-break` push the other way — they *encourage* breaking — so setting them alongside `nowrap` is a contradiction that trips people up.) **3. The box must be narrower than the line.** A block-level box in normal flow fills its containing block's inline size, so it already has a constraint. A box that shrinks to fit its content — a float, an absolutely positioned box, an `inline-block` with no width, a table cell in some configurations — grows to the text and never overflows. That is the case where everything *looks* right and the ellipsis still refuses to appear: the answer is a `width` or `max-width` (or `max-inline-size`). ## Where it applies `text-overflow` applies to block containers, so a bare `<span>` will not truncate; give it `display: inline-block` (or `block`) so it has a block container's inline layout to clip. It affects inline content only — it will not put an ellipsis after a clipped block-level child. Because the clipped text is still in the box, it stays selectable and copyable, and assistive technology still exposes the full string. That is usually the behaviour you want, and it is a real advantage over truncating strings in advance: the ellipsis is a rendering decision that adapts to the available width, not a lossy edit to the content. ## Multi-line truncation There is no `text-overflow` answer for "three lines then an ellipsis" — the property is single-line by nature. The technique everyone uses is the line-clamp trio, which is prefixed but implemented across engines: ```css .clamp-3 { display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 3; overflow: hidden; } ``` All four declarations are required: the clamp only takes effect on a `-webkit-box` with vertical orientation, and without `overflow: hidden` nothing is clipped. Note that the box display type it depends on has its own quirks, so it is worth checking any layout you drop it into. ## Symptoms and fixes - Ellipsis missing, text wraps to several lines → `white-space: nowrap` is absent. - Ellipsis missing, text runs past the box → `overflow` is still `visible`. - Ellipsis missing, box just keeps getting wider → the box shrink-wraps its content; add a width or max-width. - Ellipsis appears but is chopped in half → you set `text-overflow: clip`, or you are looking at the `clip` fallback in a context where `ellipsis` does not apply. - Nothing works on an inline element → make it a block container first.
- Does text-overflow: ellipsis work on a plain inline element such as a span?No. `text-overflow` applies to block containers, so an inline box has nothing to clip inline content against. Give the element `display: inline-block` or `display: block` together with `overflow: hidden`, `white-space: nowrap` and a width constraint, and it truncates as expected.
- Is the clipped text still available to the user or to assistive technology?Yes — the text is fully present in the box, only visually clipped. It stays selectable and copyable, and assistive technology reads the whole string, which is why CSS truncation is preferable to shortening the string in advance. If the visual truncation itself is meaningful, pair it with a way to reveal the full value.
- How would you truncate after three lines rather than one?`text-overflow` is single-line only. The cross-engine technique is the prefixed line-clamp trio: `display: -webkit-box`, `-webkit-box-orient: vertical`, `-webkit-line-clamp: 3`, plus `overflow: hidden`. All four are needed — the clamp only applies inside that box display type, and without the overflow declaration nothing is clipped.
saying these in an interview costs you the question
- Thinks text-overflow alone shortens the string
- Forgets white-space: nowrap and blames the browser
- Assumes it works without any width constraint
- Applies it to an inline span and expects truncation
- Believes the clipped text is removed from the page