49

I'm using bootstrap 3 tables, when i put large text in the table it gets wrapped on several lines, but i want it to be truncated with three dots at the end in a responsive way without messing up the layout of the table (i found some solutions but with unpleasant effects).

Is that possible ? how ?

PS: any solution is welcome, but i would like it to be just HTML/CSS if it's possible.

0

4 Answers 4

62

You need to use table-layout:fixed in order for CSS ellipsis to work on the table cells.

.table {
  table-layout:fixed;
}

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

demo: http://bootply.com/9njmoY2CmS

3
  • 13
    i already try this; the problem is that the table layout will not be nice, all the column will have the same width, which i found unfair, i even mentioned it on my post!
    – Xsmael
    Commented May 2, 2014 at 15:48
  • 11
    bootstrap used to allocate width to each column depending on his content in a very nice way, and i want to keep this effect as much as possible
    – Xsmael
    Commented May 2, 2014 at 15:56
  • 10
    I second Xsmael's point here. Fixed layout is not a fix for this.
    – CptAJ
    Commented Jul 29, 2015 at 2:40
36

I did it this way (you need to add a class text to <td> and put the text between a <span>:

HTML

<td class="text"><span>looooooong teeeeeeeeext</span></td>

SASS

.table td.text {
    max-width: 177px;
    span {
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        display: inline-block;
        max-width: 100%;
    }
}

CSS equivalent

.table td.text {
    max-width: 177px;
}
.table td.text span {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    display: inline-block;
    max-width: 100%;
}

And it will still be mobile responsive (forget it with layout=fixed) and will keep the original behaviour.

PS: Of course 177px is a custom size (put whatever you need).

6
  • I had to switch to display: block to remove a small bit of whitespace that this added to the bottom of the table row. See stackoverflow.com/questions/45588761/…
    – Dan Mason
    Commented Aug 9, 2017 at 11:31
  • Is this solution possible without defining a max width, but with taking the rest of the available space? Commented Mar 9, 2019 at 9:50
  • @JelmerBrands If you mean with cols in "auto" (width), I'm afraid not, otherwise the browser wouldn't know when the text should be truncated. I'm not a CSS expert (I'm mainly a dev) but I think I'm not wrong with this. On the other hand, if you fixed the wdth of X - 1 cols (like 3 of 4), it may be possible I guess, but I think container (table maybe) should have a specific width somewhere. I can't help you with that, sorry. Commented Mar 10, 2019 at 15:29
  • @LucianoFantuzzi Logically thinking, the browser knows how much space it needs for column A to fit everything inside the column. Considering the width of the screen he knows how much space he has left for column B and truncates the text accordingly if the text in column B exceeds that width. Thanks anyway. Hopefully a CSS export hops along and can answer this. Commented Mar 12, 2019 at 13:28
  • @JelmerBrands if you are success with this, please let us know how you did it. The browser knows about widths of all cols and arrange the space depending on the content. The browser doesn't know what's the limit to show "..." unless you set the width of the parent container. So, if you set 3 of 4 col widths, then I guess it will be able to strip the content if a parent container was set with a fixed width. Ie: (100 table width, col1=25, col2=25, col3=25, col4=not-specified, so 100-25*3=25, and the browser knows the last col width). Commented Mar 12, 2019 at 13:51
2

This is what I achieved, but had to set width, and it cannot be percentual.

.trunc{
  width:250px; 
  white-space: nowrap; 
  overflow: hidden; 
  text-overflow: ellipsis;
}
table tr td {
padding: 5px
}
table tr td {
background: salmon
}
table tr td:first-child {
background: lightsalmon
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<table>
      
      <tr>
        <td>Quisque dignissim ante in tincidunt gravida. Maecenas lectus turpis</td>
      <td>
         <div class="trunc">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
         </div>
    </td>
        </tr>
      </table>

or this: http://collaboradev.com/2015/03/28/responsive-css-truncate-and-ellipsis/

2

I'm using bootstrap.
I used css parameters.

.table {
  table-layout:fixed;
}

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

and bootstrap grid system parameters, like this.

<th class="col-sm-2">Name</th>

<td class="col-sm-2">hoge</td>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.