1.5 KiB
1.5 KiB
Truncate text multiline
If the text is longer than one line, it will be truncated for n lines and end with an ellipsis ….
HTML
<p class="truncate-text-multiline">
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut
labore et.
</p>
CSS
.truncate-text-multiline {
overflow: hidden;
text-overflow: ellipsis;
display: block;
display: -webkit-box;
height: 109.2px;
margin: 0 auto;
font-size: 26px;
line-height: 1.4;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;
width: 400px;
}
Demo
Explanation
overflow: hiddenprevents the text from overflowing its dimensions (for a block, 100% width and auto height).text-overflow: ellipsismakes it so that if the text exceeds its dimensions, it will end with an ellipsis.width: 400pxensures the element has a dimension, to know when to get ellipsisdisplay: -webkit-boxattribute that allows to work withline-clamp-webkit-line-clamp: 3number of lines to be truncated (in this case there will be only 3 visible lines)-webkit-box-orient: horizontalspecify that text will go verticallydisplay: blockfallback for unsupported browsersheight: 109.2pxfallback, calculated value for height, it equalsfont-size * line-height * numberOfLines(in this case26 * 1.4 * 3 = 109.2)
Browser support
⚠️ Requires prefix for full support.