Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add dots/ellipsis on div/span element overflow without using jquery

Need to implement functionality similar to what dotdotdot jQuery plugin does but cannot use javascript frameworks (like jquery or ext).

Is there any easy way to add the dots to the content of div or span element if content takes more space then element should??? (similar to what css overflow: ellipsis setting does)

Can't use ellipsis beacause it doesn't work with many lines when height is limited.

Thank you :)

like image 960
Bohdan Avatar asked Nov 16 '11 13:11

Bohdan


People also ask

How can I show dots in a span with hidden overflow?

So by default short text with dots is shown and by clicking long text appears. Clicking again hides that long text and shows short one again. Quite easy thing to do: just add / remove class with text-overflow:ellipsis.

How do you add an ellipsis to a span tag?

To add an ellipsis in the HTML <span> element having the CSS overflow property set to “hidden”, you need to add the text-overflow property. Use its “ellipsis” value, which will add dots at the end of the content within the <span>.

How do you add an ellipsis to a div in CSS?

To clip at the transition between characters you can specify text-overflow as an empty string, if that is supported in your target browsers: text-overflow: ''; . This keyword value will display an ellipsis ( '…' , U+2026 HORIZONTAL ELLIPSIS ) to represent clipped text.

How do I use text overflow ellipsis in a div?

The overflowing content can be clipped, display an ellipsis ('…'), or display a custom string. Syntax: text-overflow: clip|string|ellipsis|initial|inherit; Property Values: All the properties are described well with the example below.


4 Answers

Why not using the CSS property text-overflow? It works great as long as you define a width in your tag.

Class in CSS:

.clipped {
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
    }
    
<div class="clipped" style="width: 100px;" title="This is a long text">This is a long text<div>

You can also add the text to the title attribute, so the user can see the whole text when hovering over the element.

like image 180
martinstoeckli Avatar answered Sep 23 '22 00:09

martinstoeckli


Works for any number of lines and any width without any javascript - and is responsive. Simply set your max-height to a multiple of your line height: i.e. (22px line height) * (max 3 lines of text) = (max height 66px).

https://codepen.io/freer4/pen/prKLPy

html, body, p { margin: 0; padding: 0; font-family: sans-serif;line-height:22px;}

.ellipsis{
  overflow:hidden;
  margin-bottom:1em;
  position:relative;
}

.ellipsis:before {
  content: "\02026";  
  position: absolute;
  bottom: 0; 
  right:0;
  width: 1.8em; 
  height:22px;
  margin-left: -1.8em;
  padding-right: 5px;
  text-align: right;
  background-size: 100% 100%;
  background: linear-gradient(to right, rgba(255, 255, 255, 0), white 40%, white);
  z-index:2;
}
.ellipsis::after{
  content:"";
  position:relative;
  display:block;
  float:right;
  background:#FFF;
  width:3em;
  height:22px;
  margin-top:-22px;
  z-index:3;
}

/*For testing*/
.ellipsis{
  max-width:500px;
  text-align:justify;
}
.ellipsis-3{
  max-height:66px;
}

.ellipsis-5{
  max-height:110px;
}
<div class="ellipsis ellipsis-3">
  <p>Here we can have a great many lines of text and it works as we expect it to. Here we can have a great many lines of text and it works as we expect it to. Here we can have a great many lines of text and it works as we expect it to. Here we can have a great many lines of text and it works as we expect it to.</p>  
</div>

<div class="ellipsis ellipsis-5">
  <p>The number of lines shown is easily controlled by setting the max-height of the .ellipsis element. The downsides are the requirement of a wrapping element, and that if the text is precisely as long as your number of lines, you'll get a white area covering the very trailing end of your text. You've been warned. This is just some pushing text to make the element longer. See the ellipsis? Yay.</p>  
</div>
like image 38
Randy Hall Avatar answered Sep 23 '22 00:09

Randy Hall


You could try:

text-overflow: ellipsis;
-o-text-overflow: ellipsis;

This will only work if your elements are not dynamically sized. They will have to have a width set or some other mechanism to keep them from growing to allow more content.

like image 21
arb Avatar answered Sep 27 '22 00:09

arb


My solution to my problem can seem a little awkward, but it works for me:)

I used a little of CSS:

word-wrap: break-word;

and Javascript:

var spans = document.getElementsByTagName("span");
for (var i in spans) {
    var span = spans[i];
    if (/*some condition to filter spans*/) { // just 
        if (navigator.appName == 'Microsoft Internet Explorer') {
            span.parentNode.style.display ='inline-block';
        }
        if (span.parentNode.clientHeight > 50 ) {
            span.innerHTML = span.innerHTML.substr(0, 26) + ' ...';
        }
    }
}
like image 1
Bohdan Avatar answered Sep 27 '22 00:09

Bohdan