How can I show dots (“…”) in a span with hidden overflow?

后端 未结 8 1353

My CSS:

#content_right_head span
{
  display:inline-block;
  width:180px;
  overflow:hidden !important;
}

Now it\'s showi

8条回答
  •  清歌不尽
    2020-12-02 04:32

    If you are using text-overflow:ellipsis, the browser will show the contents whatever possible within that container. But if you want to specifiy the number of letters before the dots or strip some contents and add dots, you can use the below function.

    function add3Dots(string, limit)
    {
      var dots = "...";
      if(string.length > limit)
      {
        // you can also use substr instead of substring
        string = string.substring(0,limit) + dots;
      }
     
        return string;
    }
    

    call like

    add3Dots("Hello World",9);
    

    outputs

    Hello Wor...
    

    See it in action here

    function add3Dots(string, limit)
    {
      var dots = "...";
      if(string.length > limit)
      {
        // you can also use substr instead of substring
        string = string.substring(0,limit) + dots;
      }
    
        return string;
    }
    
    
    
    console.log(add3Dots("Hello, how are you doing today?", 10));

提交回复
热议问题