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?
A more flexible jQuery plugin enabling you to keep a element after the ellipsis (for example a "read-more" button) and update onWindowResize. It also works around text with markup:
http://dotdotdot.frebsite.nl
I've got a solution working in FF3, Safari and IE6+ with single and multiline text
jquery.ellipsis.js
Just like @acSlater I couldn't find something for what I needed so I rolled my own. Sharing in case anyone else can use:
Method: Usage: Code and Demo Link: https://gist.github.com/cemerson/10368014I made a really cool jQuery plugin for handling all varieties of ellipsis of text is one called ThreeDots @ http://tpgblog.com/threedots
It's much more flexible than the CSS approaches, and supports much more advanced, customizable behaviors and interactions.
Enjoy.
Pure CSS Multi-line Ellipsis for text content:
Please checkout the snippet for a live example.
Well, one simple solution, that doesn't quite add the "...", but does prevent the <h2> from breaking into two lines would be to add this bit of css:
I gave it some more thought, and I came up with this solution, you have to wrap the textual contents of your h2 tag with another tag (e.g. a span) (or alternatively wrap the h2s with something that has the given height) and then you can use this sort of javascript to filter out the unneeded words: