How can I show dots ("...") in a span with hidden overflow?
To make text in a <span>
element magically transform into "..." when it spills over the allowed width, invoke the magical spell - text-overflow: ellipsis;
. This spell will take care of the clumsy part (overflow: hidden;
) that tries to break out, and by saying the incantation white-space: nowrap;
, the text will not dare to split onto the next line.
CSS:
HTML:
Output: This content is too...
Playing nice with dynamic text
We love surprises, don't we? But our magic prefers predictability over changes. Hence, the width
in .span-ellipsis
needs to be set considering the longest string or parent container's width.
Breaking the single line curse
The basic spell only works for a single line. Want to enchant multiple lines? Fret not. Just conjure the line-clamp
spell, but be careful, older browsers might not like it. If your audience is a group of old school muggles, you'll have to resort to other magical artifacts.
Taming the content with JavaScript
When CSS magic fails, JavaScript comes to the rescue. With a touch of add3Dots
function, we can custom tailor our content and append the ellipsis manually.
JavaScript spells provide a sea of possibilities. Dive in if you dare!
Sweeter spells with jQuery
Are you a budding wizard who wants to manage overflowing text but shorten the spell-cast time? The magical artifact called jQuery Ellipsis plugin can do the hard work for you. Just remember, with great power comes great responsibility, the plugin should be used wisely and sparingly.
Example:
Expert wizard tips
- Responsive design: Make your spell responsive. Use
vw
for width instead of fixed units likepx
. - Accessibility: For visually impaired audience who rely on screen readers, use
aria-label
to deliver the full content. - Hover effects: Consider using a
title
attribute or a hover tooltip to reveal the full content magically on mouse hover.
Was this article helpful?