'How to spread text into multi-line by the height of the div and add three dots in the end?
I have a long text, and I want to display it along with his parent div. I give him height and width. I styled the text with
text-overflow: ellipsis;
overflow: hidden;
white-space: nowrap;
But, it's only on one line.
I add codepen to demonstrate it : https://codepen.io/roee030/pen/bGYYjEr
.root {
width: 283px;
font-size: 20px;
height: 283px;
background-color: coral;
}
.text {
text-overflow: ellipsis;
overflow: hidden;
white-space: nowrap;
}
<div class="root">
<div class="title">
asdasdasdasdasd
</div>
<div class="text">
There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum,
you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary
of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.
</div>
</div>
Solution 1:[1]
The
text-overflowproperty doesn't force an overflow to occur. To maketext-overflowits container you have to set other CSS properties: overflow and white-space.
~ MDN
So in short, the behavior you are experiencing is standard with text-overflow: ellipsis;. white-space: nowrap; is required for ellipsis to take effect. (which is also putting the text on one line).
So let's look at workarounds:
You can nest your text in the <span> element with hidden and overflow styles. If you have a contained (fixed-height) on your parent element, you can specify a line-clamp.
The line-clamp property truncates text at a specific number of lines.
So for example: If you have a parent with a div that has a fixed height of 283px you can specify -webkit-line-clamp: 13; which produces 13 lines, corresponding with the height of the parent.
However, keep in mind that the line-clamp property works well with:
.root {
width: 283px;
font-size: 20px;
height: 283px;
background-color: coral;
}
span {
-webkit-line-clamp: 13;
display: -webkit-box;
line-height: 1;
-webkit-box-orient: vertical;
overflow: hidden;
text-overflow: ellipsis;
}
<div class="root">
<div class="title">
asdasdasdasdasd
</div>
<div class="text"><span>There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.<span>
</div>
</div>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Kameron |
