Hai Tien
Hai Tien

Reputation: 3117

How to create shorten text?

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

Upvotes: 1

Views: 230

Answers (6)

arclite
arclite

Reputation: 528

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);
        }

    });
});

Upvotes: 1

radha
radha

Reputation: 782

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/

Upvotes: 1

Sujata Chanda
Sujata Chanda

Reputation: 3395

Try this:-

.box{
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}

Upvotes: 2

Jacques Snyman
Jacques Snyman

Reputation: 4290

.box a 
{
    color:#000;
    text-overflow: ellipsis;
}

Upvotes: 3

Krzysztof
Krzysztof

Reputation: 16140

Add following styles to .box:

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

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

Upvotes: 6

Bhojendra Rauniyar
Bhojendra Rauniyar

Reputation: 85545

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

Upvotes: 3

Related Questions