Is there a solution to add ellipsis on last line inside a div with a fluid height (20%)?
I found the -webkit-line-clamp
function in CSS, but in my case the line number will be depending on window size.
p {
width:100%;
height:20%;
background:red;
position:absolute;
}
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla sed dui felis. Vivamus vitae pharetra nisl, eget fringilla elit. Ut nec est sapien. Aliquam dignissim velit sed nunc imperdiet cursus. Proin arcu diam, tempus ac vehicula a, dictum quis nibh. Maecenas vitae quam ac mi venenatis vulputate. Suspendisse fermentum suscipit eros, ac ultricies leo sagittis quis. Nunc sollicitudin lorem eget eros eleifend facilisis. Quisque bibendum sem at bibendum suscipit. Nam id tellus mi. Mauris vestibulum, eros ac ultrices lacinia, justo est faucibus ipsum, sed sollicitudin sapien odio sed est. In massa ipsum, bibendum quis lorem et, volutpat ultricies nisi. Maecenas scelerisque sodales ipsum a hendreritLorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla sed dui felis. Vivamus vitae pharetra nisl, eget fringilla elit. Ut nec est sapien. Aliquam dignissim velit sed nunc imperdiet cursus. Proin arcu diam, tempus ac vehicula a, dictum quis nibh. Maecenas vitae quam ac mi venenatis vulputate. Suspendisse fermentum suscipit eros, ac ultricies leo sagittis quis. Nunc sollicitudin lorem eget eros eleifend facilisis. Quisque bibendum sem at bibendum suscipit. Nam id tellus mi. Mauris vestibulum, eros ac ultrices lacinia, justo est faucibus ipsum, sed sollicitudin sapien odio sed est. In massa ipsum, bibendum quis lorem et, volutpat ultricies nisi. Maecenas scelerisque sodales ipsum a hendrerit.</p>
I have this JSFiddle to illustrate the issue. https://jsfiddle.net/96knodm6/
If you want to apply ellipsis (...) to a single line of text, CSS makes that somewhat easy with the
text-overflow
property. It's still a bit tricky (due to all the requirements – see below), buttext-overflow
makes it possible and reliable.If, however, you want to use ellipsis on multiline text – as would be the case here – then don't expect to have any fun. CSS has no standard method for doing this, and the workarounds are hit and miss.
Ellipsis for Single Line Text
With
text-overflow
, ellipsis can be applied to a single line of text. The following CSS requirements must be met:width
,max-width
orflex-basis
white-space: nowrap
overflow
with value other thanvisible
display: block
orinline-block
(or the functional equivalent, such as a flex item).So this will work:
jsFiddle version
BUT, try removing the
width
, or letting theoverflow
default tovisible
, or removingwhite-space: nowrap
, or using something other than a block container element, AND, ellipsis fails miserably.One big takeaway here:
text-overflow: ellipsis
has no effect on multiline text. (Thewhite-space: nowrap
requirement alone eliminates that possibility.)jsFiddle version
Ellipsis for Multiline Text
Because CSS has no property for ellipsis on multiline text, various workarounds have been created. Several of these methods can be found here:
Please check this css for ellipsis to multi-line text
To bad CSS doesn't support cross-browser multiline clamping, only webkit seems to be pushing it.
You could try and use a simple Javascript ellipsis library like Ellipsity on github the source code is very clean and small so if you do need to make any additional changes it should be quite easy.
https://github.com/Xela101/Ellipsity
After looking all over the internet and trying a lot of these options, the only way to make sure that it is covered correctly with support for i.e is through javascript, i created a loop function to go over post items that require multi line truncation.
*note i used Jquery, and requires your post__items class to have a fixed max-height.
This man have the best solution. Only css:
I have just been playing around a little bit with this concept. Basically, if you are ok with potentially having a pixel or so cut off from your last character, here is a pure css and html solution:
The way this works is by absolutely positioning a div below the viewable region of a viewport. We want the div to offset up into the visible region as our content grows. If the content grows too much, our div will offset too high, so upper bound the height our content can grow.
HTML:
CSS:
I have tested this in Chrome, FF, Safari, and IE 11.
You can check it out here: http://codepen.io/puopg/pen/vKWJwK
You might even be able to alleviate the abrupt cut off of the character with some CSS magic.
EDIT: I guess one thing that this imposes is word-break: break-all since otherwise the content would not extend to the very end of the viewport. :(