admin管理员组

文章数量:1434363

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

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

Share Improve this question edited Nov 11, 2013 at 16:18 Roy M J 6,9387 gold badges53 silver badges79 bronze badges asked Nov 11, 2013 at 10:51 Hai TienHai Tien 3,1178 gold badges39 silver badges64 bronze badges 3
  • try this stackoverflow./questions/11417544/… – Vinod Louis Commented Nov 11, 2013 at 10:56
  • 1 There's no need to put it in jquery , unless if you have a button that say make it shorten – Drixson Oseña Commented Nov 11, 2013 at 10:57
  • Thanks all support me. – Hai Tien Commented Nov 11, 2013 at 11:07
Add a ment  | 

6 Answers 6

Reset to default 6

Add following styles to .box:

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

Working sample: http://jsfiddle/qLzK7/

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

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

Try this:-

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

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/9yatw/

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

    });
});

本文标签: javascriptHow to create shorten textStack Overflow