Why is it so hard to have this:
DIV
with multiple lines of text The following JSFiddle shows a demo.
Questions:
.chrome
css) in JSFiddle Example:
.truncate {
border-width: 1px;
border-style: solid;
border-color: #aaaaaa;
padding: 10px;
margin-bottom: 10px;
width: 260px;
overflow: hidden;
display: block;
box-sizing: border-box;
-moz-box-sizing: border-box;
}
.truncate.ellipsis {
height: 50px;
text-overflow: ellipsis;
}
.truncate.ellipsis.chrome {
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
}
<div class="truncate">
No truncating at all, height adjusts to text: abcdefghijk abcdefghijk abcdefghijk abcdefghijk abcdefghijk abcdefghijk abcdefghijk abcdefghijk abcdefghijk abcdefghijk abcdefghijk abcdefghijk abcdefghijk
</div>
<div class="truncate ellipsis">
Truncating at 50px height: abcdefghijk abcdefghijk abcdefghijk abcdefghijk abcdefghijk abcdefghijk abcdefghijk abcdefghijk abcdefghijk abcdefghijk abcdefghijk abcdefghijk abcdefghijk
</div>
<div class="truncate ellipsis chrome">
Truncating at 50px height: abcdefghijk abcdefghijk abcdefghijk abcdefghijk abcdefghijk abcdefghijk abcdefghijk abcdefghijk abcdefghijk abcdefghijk abcdefghijk abcdefghijk abcdefghijk
</div>
If your text-overflow is not working you must ensure display, overflow and white-space CSS properties are set to proper values. Ellipsis works in the same way on all types of elements. But you need to use it with caution on Flex, CSS Grid or on a Table.
Inline overflow occurs when the text in a line overflows the available width without a breaking opportunity. To force overflow to occur and ellipses to be applied, the author must apply the nowrap value to the white-space property on the element, or wrap the content in a <NOBR> tag.
A text-overflow property in CSS is used to specify that some text has overflown and hidden from view. The white-space property must be set to nowrap and the overflow property must be set to hidden. The overflowing content can be clipped, display an ellipsis ('…'), or display a custom string.
There is currently no cross browser way to do this. That said, there are a few approches you could try:
:after
pseudo element and to absolutely position it on the bottom right corner of your DIVExample of that last one:
HTML:
<div class="ellipsis-div">
<p>Long text Cras justo odio, dapibus ac facilisis in, egestas eget quam. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit.<p>
</div>
CSS:
.ellipsis-div {
position: relative;
width: 150px;
height: 150px;
overflow: hidden;
}
.ellipsis-div:after {
position: absolute;
display: block;
content: "...";
bottom: 0;
right: 0;
background-color: white;
}
You will need to tweak the position of the :after
element depending on your DIV's line-height, etc...
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