I publish this because I find that my solution is less complicated than the popular one, which uses pseudo-elements and float behavior. I recently had to create a solution that will work in IE7, so pseudo-elements were not an option in the first place.
The method includes 4 elements:
- Block level container that defines the maximum height of the content
- Built-in wrapper for text content
- Ellipse contained within an embedded shell
- The "fill" element is also inside the inline shell, which overlaps the ellipsis when the text content does not exceed the size of the container.
As in previous solutions only for CSS, the technique requires a solid colored background or background image with a fixed position for the content: the ellipsis should hide parts of the text, and the fill should hide the ellipsis. You can make a spectacular gradient effect so that the text disappears into ellipsis, but I will leave this cosmetic detail to my discretion.
HTML structure
<p class="clamped clamped-2"> <span class="text"> Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. <span class="ellipsis"> … </span> <span class="fill"></span> </span> </p>
CSS
body { background: #fff; line-height: 1.5; } .clamped { overflow: hidden; position: relative; } .clamped-2 { max-height: 3em; } .clamped .ellipsis { background: #fff; bottom: 0; position: absolute; right: 0; } .clamped .fill { background: #fff; height: 100%; position: absolute; width: 100%; }
Here's a fiddle demonstrating this : resize the browser or resize the text to see that it is shifting from ellipsis to non-ellipsis.
In addition to the arbitrary elegance factor, I believe that this is more productive than the popular solution, because it does not rely on floats (which require a lot of repainting) - absolute positioning is much easier to calculate, dependencies when calculating the layout.
Barney Jun 19 '13 at 9:14 a.m. 2013-06-19 09:14
source share