How can I truncate long texts in a table using CSS?

前端 未结 4 609
余生分开走
余生分开走 2020-12-30 23:05

Can anyone give a CSS example of how can I create a table where long texts are truncated to texts ending with \"...\"?

Here is the example: http://jsfiddle.net/NpABe

4条回答
  •  生来不讨喜
    2020-12-30 23:45

    2020 update - Full CSS working version

    For me the proposed answer wasn't doing the trick...

    After some research, the only way to do it without specifying a minimum width is the following:

    .clamp {
      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 1;
    }
    

    With this method, you don't have to add an other div.

    The html structure should look like something like this:

    
      
        Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
      
    
    

    Also here is the can i use, https://caniuse.com/#search=line-clamp, it's supported by all of them (beside iE as usual)... For iE you can just specify a max-height and overflow-y:hidden it should do the trick.

    And here is a simple working example, https://jsfiddle.net/d8r4yckm/ (loaded bootstrap for "ease on the eyes" only)

提交回复
热议问题