CSS Reference ยท Typography
The text-overflow property
Reference for the CSS text-overflow property: truncate overflowing lines with an ellipsis or clip, the three required companion declarations, flex and table pitfalls.
The text-overflow property decides what happens at the end of a line of text that does not fit its box. With clip (the default) the text is simply cut off. With ellipsis the browser replaces the cut part with an ellipsis character so readers can tell something is missing.
The property only changes how the overflow is shown. It does not create the overflow, and it does not hide anything by itself. For the classic single-line truncation you always need three declarations working together.
.title { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
Try it
The third box shows the most common mistake: text-overflow: ellipsis has no effect while overflow is visible.
Syntax
text-overflow: [ clip | ellipsis | <string> ]{1,2}
- Initial value:
clip - Applies to: block containers
- Inherited: no
- Animatable: no
- Computed value: as specified
Values
| Value | Description |
|---|---|
clip |
Cuts the text at the edge of the box. A glyph can be sliced in half. |
ellipsis |
Replaces the hidden end of the line with an ellipsis character (โฆ). |
<string> |
A custom string shown instead of the hidden text. Browser support is limited, so check a compatibility table before using it. |
| two values | The first applies to the left end of the line, the second to the right end (the line-end and line-start in other writing directions). |
Usage notes
- Three ingredients. Truncation needs
overflowset to something other thanvisible,white-space: nowrap(otherwise the text wraps and does not overflow horizontally), and a constrained width. - Only whole inline overflow.
text-overflowworks on a single line. To cut text after several lines, use the line-clamp technique described in Wrapping, Overflow and Hyphens instead. - Flex and grid items. A flex item has
min-width: auto, so it refuses to shrink below its text. Addmin-width: 0(oroverflow: hidden) to the item so the ellipsis can appear. - Table cells. Cells size to their content. Use
table-layout: fixedor give the cell amax-widthbefore truncation works. - Accessibility. The full text is still in the DOM, so screen readers read all of it. Sighted users lose the hidden part, so offer a
title, a tooltip, or a way to expand when the text matters. - Scrolling boxes. With
overflow: autoorscroll, the ellipsis shows while text is clipped and disappears as the user scrolls it into view.
Browser support
clip and ellipsis work in every current browser; custom <string> values and the two-value form are less consistent, so test them.
Related
overflowโ clipping and scrollingwhite-spaceโ prevents wrapping withnowraptext-wrapโ controls how lines break- Lesson: Wrapping, Overflow and Hyphens
- Lesson: Overflow