32
2 Simple ways you can truncate text using CSS
As part of this blog lets see two ways in which you can truncate a text using CSS
.truncate-ellipsis {
width: 350px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
display: block;
}
With text-overflow , ellipsis can be applied to single line of text, provided the following conditions are met. [ For truncating after multiple lines, keep reading 😉 ].
Did you know that you can reverse the direction of the truncation using the CSS direction property?
direction: rtl; //show from right to left
The direction property will truncate the text in the start of the line and show the end of the paragraph instead.

.truncate-line-clamp {
display: -webkit-box;
-webkit-line-clamp: 4;
-webkit-box-orient: vertical;
width: 250px;
overflow: hidden;
}
With line-clamp text can be truncated after multiple lines, whats even more interesting is you can truncate it by specifying the line number where you want to truncate it.
eg: -webkit-line-clamp: 3; will truncate start truncating the text from the third line.
eg: -webkit-line-clamp: 3; will truncate start truncating the text from the third line.
Below are the list of conditions which should be met in order to make this work.
Browser Compatability: webkit-line-clamp at the moment is not supported in IE.
For detailed information refer: caniuse.com
For detailed information refer: caniuse.com
The below codepen will show you a live preview of the above two methods will look.
https://codepen.io/kpattalam/pen/jOBXvyg
https://codepen.io/kpattalam/pen/jOBXvyg
32