Explain Codes LogoExplain Codes Logo

Overflow:hidden dots at the end

css
responsive-design
best-practices
css-pseudo-elements
Anton ShumikhinbyAnton Shumikhin·Jan 24, 2025
TLDR

Here's a short and sweet way to apply CSS text-overflow: ellipsis for trailing dots on overflowed text:

.ellipsis { overflow: hidden; /* Overflow? Sorry, I don't do that... */ white-space: nowrap; /* Text stays in its lane! */ text-overflow: ellipsis; /* Magic dots when text misbehaves! */ }
<div class="ellipsis" style="width: 100px;"> Long text here will show dots when cut off. </div>

The constraining factor here is the width of the container. If it's crossed, the ellipsis function occurs due to the overflow.

Understanding the ellipsis properties

CSS property text-overflow: ellipsis teaming up with overflow: hidden and white-space: nowrap is what makes the dots happen. The white space is on guard, preventing any line breaks while 'overflow: hidden' chops off extra text. Together, they create an environment that spawns ellipsis when the set width is breached.

Multi-lining and adapting to screens

For multiline overflow truncation, here's a combo using -webkit-line-clamp along with -webkit-box-orient: vertical;:

.multiline-ellipsis { display: -webkit-box; -webkit-line-clamp: 3; /* Count 'em! One. Two. Three lines... */ -webkit-box-orient: vertical; overflow: hidden; text-overflow: ellipsis; }

On a side note, the property -webkit-line-clamp performs best in webkit based browsers. Mixing it up with responsive design, using max-width instead of width adjusts the container size along with the ellipsis based on screen sizes.

Hover: the grand reveal

How about creating a sneak peak effect on hover? Return the white-space and text-overflow properties to normal, and unhide the overflow:

.ellipsis:hover { white-space: normal; /* Party's over, everyone free to break lines! */ text-overflow: clip; /* Regular rules now, no ellipsis! */ overflow: visible; /* Hide-n-seek, time to seek! */ }

A look that's neat yet doesn't fail to inform.

The effect of display properties on ellipsis

The display property plays a role in how text-overflow: ellipsis; works. For inline elements, make sure to set the display to inline-block:

.inline-ellipsis { display: inline-block; /* Art of staying in line */ /* Everything else same as .ellipsis class */ }

With block elements, you're good with just display: block;.

When implementing text-overflow: ellipsis, always check browser compatibility on platforms like Can I use.... Cater for localization by considering how direction: rtl; might behave with right-to-left languages.

Accessibility and search engine indexing

While 'ellipsis' is neat, keep accessibility in mind. Screen readers should access the full content, and search engines index what is visible. So, keep crucial keywords and information accessible.

Advanced text truncation techniques

For complex layouts, use CSS pseudo-elements or flexbox. Pseudo-elements can add visual cues for truncated text. Flexbox on the other hand can handle text overflow in a flex container differently.