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.
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.
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.
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.
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! :)
width
set.<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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With