1

I have HTML like :

<div class="box">
   <a href="#">Stackoverflow is very useful site and I love it</a>
</div>

and CSS like :

.box {
    width:230px;
    height:50px;
    line-height:50px;
    background:#eee;
}

.box a {
    color:#000;
}

I want create shorten text of link. If it itself overflow the box class, then take it by "...". How can I do that with css or jquery?

Check jsfiddle here

Roy M J
  • 6,926
  • 7
  • 51
  • 78
Hai Tien
  • 2,929
  • 7
  • 36
  • 55

6 Answers6

6

Add following styles to .box:

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

Working sample: http://jsfiddle.net/qLzK7/

Krzysztof
  • 15,900
  • 2
  • 46
  • 76
3

You need to set text-overflow: ellipsis for that purpose see this link for more info

Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
3
.box a 
{
    color:#000;
    text-overflow: ellipsis;
}
Jacques Snyman
  • 4,115
  • 1
  • 29
  • 49
2

Try this:-

.box{
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
Sujata Chanda
  • 3,355
  • 2
  • 21
  • 35
1

try this,

 .box{
width:230px; 
height:50px;
line-height:50px
;background:#eee; 
overflow:hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
.box a{color:#000}

http://jsfiddle.net/9yatw/

radha
  • 782
  • 4
  • 8
1

Try this:

$(document).ready(function() {
    var showChar = 37;
    var ellipsestext = "...";
    $('.box').each(function() {
        var content = $(this).find("a").html();
        if(content.length > showChar) {
         var a = content.substr(0, showChar);
         var b = content.substr(showChar-1, content.length - showChar);
         var html = a + ' ' + ellipsestext;
            $(this).find("a").html(html);
        }

    });
});
arclite
  • 528
  • 1
  • 6
  • 23