Truncate text with an ellipsis (single and multi-line)
Clip long text to one line or N lines with an ellipsis using text-overflow and line-clamp.
- CSS
- GreenSnippets
- MIT
.truncate {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.clamp-3 {
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 3;
line-clamp: 3;
overflow: hidden;
}
.flex-child {
min-width: 0;
}How it works
Single line needs three properties together. For several lines use line-clamp, which is now supported in current browsers (the -webkit- prefixed form below also works in older ones). Truncated text hides information, so put the full text in a title attribute or make it expandable.
Inside a flex child, add min-width: 0 or the element will refuse to shrink below its content width.
This snippet has been reviewed but was not executed by an automated test. Test it in your own environment before relying on it.
Related snippets
- CSS
CSS-only scroll-snap carousel
A horizontally scrolling gallery that snaps to each card, with no JavaScript.
- CSS
Header, sidebar, main and footer layout with grid
A full-height page layout with a sticky footer using grid-template-areas.
- CSS
Center anything with grid
Center an element horizontally and vertically using two lines of CSS.
- CSS
Keep media at a fixed aspect ratio
Reserve space for videos and images with aspect-ratio so the page does not jump while loading.

