I have a webpage with an elastic layout that changes its width if the browser window is resized.
In this layout there are headlines (h2
) that will have a variable length (actually being headlines from blogposts that I don't have control over). Currently - if they are wider than the window - they are broken into two lines.
Is there an elegant, tested (cross-browser) solution - for example with jQuery - that shortens the innerHTML of that headline tag and adds "..." if the text would be too wide to fit into one line at the current screen/container width?
I couldn't find a script that worked exactly as I wanted it so did my own for jQuery - quite a few options to set with more on their way :)
https://github.com/rmorse/AutoEllipsis
Here's another JavaScript solution. Works very good and very fast.
https://github.com/dobiatowski/jQuery.FastEllipsis
Tested on Chrome, FF, IE on Windows and Mac.
There's actually a pretty straightforward way to do this in CSS exploiting the fact that IE extends this with non-standards and FF supports
:after
You can also do this in JS if you wish by inspecting the scrollWidth of the target and comparing it to it's parents width, but imho this is less robust.
Edit: this is apparently more developed than I thought. CSS3 support may soon exist, and some imperfect extensions are available for you to try.
That last one is good reading.
I rewrote Alex's function to use to the MooTools library. I changed it a bit to word jump rather than add the ellipsis in the middle of a word.
DO THE ELLIPSIS USING ONLY CSS
*This code works on most current browsers. If you experience any problem with Opera and IE (which probably you won't), add these in the style:
* This feature is part of CSS3. Its complete syntax is:
you can do this much easier with css only, for example : sass mode
and you have ellipsis ;)