My goal is to present a paragraph on the webpage that will be dynamically updated based on user actions. I want to restrict the content to a specified number of lines and if the user tries to exceed this limit, the beginning of the paragraph should be truncated with an ellipse as an indicator.
For example, with a maximum of 2 lines: Line number 1, Line2
. If another line (line3) is added, it should display as: ...Line2. Line3
.
I have experimented with different approaches, such as setting the display property to -webkit-box and utilizing the -webkit-line-clamp feature to limit the content to 4 lines.
p {
display: -webkit-box;
max-width: 200px;
-webkit-line-clamp: 4;
-webkit-box-orient: vertical;
overflow: hidden;}
However, I encountered challenges in positioning the ellipse at the beginning of the text and truncating from the start rather than the end. The limitation to 4 lines also posed a constraint.
.ellipsis {
overflow: hidden;
width: 60px;
position: relative
direction: rtl;
margin-left: 15px;
white-space: nowrap;
}
.ellipsis:after {
position: absolute;
left: 0px;
content: "...";
}
Due to using multiline text, the white-space property set to nowrap was not applicable. Despite browsing through related questions on Stack Overflow, the specific requirement for my scenario remained distinct.
References to similar inquiries:
- I need an overflow to truncate from the left, with ellipses
- Text-overflow ellipsis on the left side
- Applying an ellipsis to multiline text