Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement single-line ellipsis with CSS

Tags:

css

I want to be able to add three dots and maintain a text in a single line in a responsive design.

So for example:

I have a link with a link inside a container element (e.g. <span>). If the text is long, it will shown in two lines one a small screen:

This is a very long text and it wraps because it's very long 

I want that text to be shown like this:

This is a very long text... 

text-overflow: ellipsis; works if you have set a width to the container, but in responsive design and on my web app it's not specified obviously.

Here's the HTML:

<div class="itm-title">   <a href="/friendly-url-sample/">This is a very long sentence and I don't want it to wrap, I want three dots</a> </div> 

Is there a solution in CSS or jQuery that can solve this? thanks.

like image 652
Idan Shechter Avatar asked Dec 15 '15 16:12

Idan Shechter


People also ask

How do I make one line text in CSS?

If you want to limit the text length to one line, you can clip the line, display an ellipsis or a custom string. All these can be done with the CSS text-overflow property, which determines how the overflowed content must be signalled to the user.

How do I truncate a line in CSS?

Single-line Truncate Single lines you need to set the text-overflow property value to ellipsis. If your elements wrapping div does not have an explicit height you also need to add white-space: nowrap; ,which you are best to use in most use cases anyhow.

How do you overflow text in CSS?

A text-overflow property in CSS is used to specify that some text has overflown and hidden from view. The white-space property must be set to nowrap and the overflow property must be set to hidden. The overflowing content can be clipped, display an ellipsis ('…'), or display a custom string.


1 Answers

You actually don't need width to be "set" here. All the elements in the responsive design have their width. You can just do it around with the following rules:

white-space: nowrap; overflow: hidden; text-overflow: ellipsis; 

Comment: This doesn't work with anchor:

a {    display: block;    white-space: nowrap;    overflow: hidden;    text-overflow: ellipsis;  }
<a href="#">Pages you view in incognito tabs won’t stick around in your browser’s history, cookie store, or search history after you’ve closed all of your incognito tabs. Any files you download or bookmarks you create will be kept. Learn more about incognito browsing.</a>

It works! :)

  • No width set.
  • Using <a> tag.

Updated with OP's Code

.itm-title {    width: 150px;  }  a {    display: block;    white-space: nowrap;    overflow: hidden;    text-overflow: ellipsis;  }
<div class="itm-title">    <a href="/friendly-url-sample/">This is a very long sentence and I don't want it to wrap, I want three dots</a>  </div>

Result: Works!

like image 123
Praveen Kumar Purushothaman Avatar answered Sep 23 '22 03:09

Praveen Kumar Purushothaman