I have a long text that I want to display inside a div with a fixed width and height.
I have tried using the CSS properties:
text-overflow: ellipsis;
overflow: hidden;
white-space: nowrap;
but this only displays the text on a single line.
I want to display the text on multiple lines that fit within the height of the div and add an ellipsis at the end if the text exceeds the available space. I have created a CodePen to demonstrate the issue: https://codepen.io/roee030/pen/bGYYjEr
Here is the CSS code I am using:
.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>
I would appreciate any help or suggestions on how to achieve this. Thank you.
The
text-overflow
property doesn't force an overflow to occur. To maketext-overflow
its 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.
~ CSS-tricks
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:
display: -webkit-box;
-webkit-box-orient: vertical;
.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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With