Css ellipsis on second line
Quickly deploy ellipsis after the second line of text with CSS using a group of WebKit-specific properties: display: -webkit-box;
, -webkit-line-clamp: 2;
, and -webkit-box-orient: vertical;
. This combination generates a box confined to two lines with the excess text gracefully hidden and the ellipsis perfectly placed. However, as this is strictly WebKit-specific, it won't play nicely with all browsers. For an exact fit, ensure the max-height
equals twice your line-height.
Note: The max-height
in the .two-line-ellipsis
class should mirror your unique line-height
.
The cross-browser tools and tricks
While the WebKit method is a sleek sprinter, the marathon of cross-browser compatibility warrants a little more groundwork with complementary approaches or fallbacks. For instances where line-clamp
takes a rain check, we can call upon JavaScript tools or innovative CSS maneuvers to take up the baton.
JavaScript libraries: dotdotdot to the rescue
A trusty way to ensure cross-browser multiline ellipsis is by swooping in with a JavaScript library, like dotdotdot
. Integrating this plugin is as easy as nodding, offering a flexible solution that can contend with dynamic content, resizes, and even nested elements.
A CSS-only magic trick with pseudo-elements
A crafty CSS-only method involves pulling ::after
pseudo-element out of the hat, coupled with absolute
positioning to fake an ellipsis. It won't match the fluidity of WebKit methods or JavaScript but duty calls for static content.
A future max-lines
prophecy
While we do the heavy lifting today, keep an ear to the ground for W3C's potential max-lines
property. This could become a standard solution in the future. Until then, let's stick to the progressive enhancement ethos to offer all users a basic (but friendly) experience while enhancing the experience for those with modern browsers.
Nailing the truncation quirks
Managing text overflow comes with its own bag of quirks: localization, responsiveness, and the size of the box hosting our text. Let's unscrew these quirks one by one.
Counting the words in a world of languages
In the Babel of multilingual websites, an ellipsis could stretch or shrink. Make sure your layout stays robust to varying text lengths across languages for consistency.
Meeting the demands of fluid screens
In a responsive design playground, truncated text morphs differently at various breakpoints. It's critical to test your methods across devices, ensuring they don't call in sick unpredictably.
Anchors aweigh with proper truncating
When working with an anchor link with ellipsis, the truncation needs to be visibly anchored. The CSS remains roughly similar, but ensuring the anchor is block displayed crisply secures the text.
Was this article helpful?