I made an image for this question to make it easier to understand.
Is it possible to create an ellipsis on a <div>
with a fixed width and multiple lines?
I’ve tried some jQuery plugins out here and there, but cannot find the one I’m looking for. Any recommendation? Ideas?
EDIT: Came across Shave which is JS plugin that does multi line text truncation based on a given max-height really well. It uses binary search to find the optimum break point. Definitely worth investigating.
ORIGINAL ANSWER:
I had to come up with a vanilla JS solution for this problem. In the case that I had worked on, I had to fit a long product name into limited width and over two lines; truncated by ellipsis if needed.
I used answers from various SO posts to cook up something that fit my needs. The strategy is as follows:
Code sample:
PS:
PPS: Just realised this is very similar to the approach as suggested by @DanMan and @st.never. Checkout the code snippets for an implementation example.
I wrote an angular component that solves the problem. It splits a given text into span elements. After rendering, it removes all overflowing elements and places the ellipsis right after the last visible element.
Usage example:
Stackblitz demo: https://stackblitz.com/edit/angular-wfdqtd
The component:
Just a quick basic idea.
I was testing with the following markup:
And CSS:
Applying this jQuery will accomplish the desired result:
It repeatedly tries to remove the last word of the text until it reaches the desired size. Because of the overflow: hidden; the process remains invisible and even with JS turned off the result remains 'visually correct' (without the "..." of course).
If you combine this with a sensible truncation on the server-side (that leaves only a small overhead) then it will run quicker :).
Again, this is not a complete solution, just an idea.
UPDATE: Added a jsFiddle Demo.
Here's a vanilla JavaScript solution you can use in a pinch:
You can play around with it in the codepen below. Try changing the font size in the CSS panel and make a minor edit in the HTML panel (e.g., add an extra space somewhere) to update the results. Regardless of the font size, the middle paragraph should always be truncated to the number of lines in the second parameter passed to limitLines().
Codepen: http://codepen.io/thdoan/pen/BoXbEK
Here I made another library with faster algorithm. Please check:
https://github.com/i-ahmed-biz/fast-ellipsis
To install using bower:
To install using npm:
Hope you enjoy!
Not an exact answer to the question, but I came across this page when trying to do very similar, but wanting to add a link to "view more" rather than just a straightforward ellipsis. This is a jQuery function that will add a "more" link to text that is overflowing a container. Personally I'm using this with Bootstrap, but of course it will work without.
To use, put your text in a container as follows:
When the following jQuery function is added, any of the divs that are larger than the adjustheight value will be truncated and have a "More" link added.
Based on this, but updated: http://shakenandstirredweb.com/240/jquery-moreless-text