Categories: Css3

Truncate String with Ellipsis

Share

Overflow with text is always a big issue in a website, especially in a programmatic environment. Use Ellipsis to truncate string.

We can provide a solution by adding an CSS property overflow: hidden setting to the span, but you can see that the text looked unnaturally cut off.

The way to make text overflow elegant is with ellipses.

The CSS behind creating ellipses is very simple, combining width, wrapping, overflow, and text-overflow:

span{
display:inline-block;
width:200px;
white-space: nowrap;
overflow:hidden ;
text-overflow: ellipsis;
}

Here is total example:

HTML:

<span>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed cursus pretium metus vitae aliquet. Sed facilisis enim nibh, eget maximus erat dignissim sit amet. In hac habitasse platea dictumst. In nec nunc eu diam pulvinar lacinia nec et arcu. Sed ut enim nunc. Nam lobortis nibh tincidunt nunc scelerisque venenatis</span>

CSS:

span{
display:inline-block;
width:200px;
white-space: nowrap;
overflow:hidden ;
text-overflow: ellipsis;
}

Result will be:

Lorem ipsum dolor sit amet,...

But Firefox doesn’t currently support text-overflow:ellipsis property. There’s one another solution for Firefox provided by Dojo Toolkit: dojox.html.ellipsis.

This resource uses an iFrame shim to create ellipsis.

dojo.require("dojox.html.ellipsis");

After requiring the JavaScript resource, place a dojoxEllipsis node within the page as below:

<span class="dojoxEllpsis">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed cursus pretium metus vitae aliquet. Sed facilisis enim nibh, eget maximus erat dignissim sit amet. In hac habitasse platea dictumst. In nec nunc eu diam pulvinar lacinia nec et arcu. Sed ut enim nunc. Nam lobortis nibh tincidunt nunc scelerisque venenatis</span>
Published by
CSS Designer
Tags: css

Recent Posts

10 Reasons Why Technology is Good for Education

Educational technology has been very helpful in the era of ever-advancing technology. It opens millions…

3 hours ago

Chat GPT Login: Step-by-Step Guide for Beginners

Have you ever thought of a world where asking the Internet for information feels like…

7 hours ago

The Sharp-Looking Guy: 5 Essential Tips for Men to Become Sharp

We've gotten so used to seeing men streetwear joggers, ripped jeans, and sleeveless shirts. Hair…

2 days ago

How to Use Your Wedding Jewellery In Unique Ways At Festivals

When it comes to festivals, the options for wedding jewellery are endless. You can go…

2 days ago

5 Tips On Window Cleaning

Whether it concerns your home or an office building, the state of a property’s windows…

2 days ago

Sustainable Business Practices: A Win-Win Strategy

You know that running an environmentally sustainable business is the right thing to do. But…

2 days ago