Hai pattern tùy theo số dòng:
Single line:
css
.truncate {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}Multi-line (ví dụ 3 dòng):
css
.clamp {
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 3;
overflow: hidden;
}Mặc dù mang prefix -webkit-, line-clamp đã được hỗ trợ rộng rãi trên tất cả browsers hiện đại (Chrome 6+, Firefox 68+, Safari 5+).
There are two approaches depending on the number of lines.
- For single line, combine three properties:
white-space: nowrap(prevents text from wrapping),overflow: hidden(hides overflow), andtext-overflow: ellipsis(adds '...'). - For multi-line (e.g., limit to 3 lines):
display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 3; overflow: hidden. - Despite the
-webkit-prefix,line-clampis now widely supported across all modern browsers.