There is a "div" in my webpage that has fixed width and height.
Following css only works with single line text:
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
How can I apply ellipsis on multiline text on the text inside that div using pure css with cross browser compatibility?
You could solve it using some after pseudo classes. As
text-overflow: ellipsis
doesn't render the same cross browser we are adding ellipsis using the content attribute that you can provide to the:after
class. When we are settingwhite-space: nowrap
to thep
we need to add thediv:after
"hack" to ensure that the text is clipped where the padding sets in.HTML:
CSS
JSFiddle
Edit
I can see that I might have misread your question a bit. My code will fix Cross-browser ellipsis rendering but not for multi-line text. Check out this post for more answers on your specific topic: Limit text length to n lines using CSS: Stack Overflow
To bad CSS doesn't support cross-browser multiline clamping, only WebKit seems to be pushing it. Any other hacky solutions don't really seem worth it at the moment because even they have their own issues.
I know how you want pure CSS and probably have your own Javascript alternative options but 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
I'm really wanting a pure CSS solution to this too to speed things up and make everything look more pretty without the need of external dependencies.
Try this example:
http://codepen.io/martinwolf/pen/qlFdp
or go for dotdotdot.js
This workaround will require a wrapping element and has a small caveat of covering the very end of your text if it exactly fills your content box, but it works well enough in fluid situations until a better css property (like line-clamp) is widely implemented.
Works best with
text-align:justified
, but not necessary.https://codepen.io/freer4/pen/prKLPy