Bootstrap 3 truncate long text inside rows of a table in a responsive way

106,502

Solution 1

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).

Solution 2

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

Solution 3

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/

Solution 4

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>
Share:
106,502

Related videos on Youtube

Xsmael
Author by

Xsmael

Updated on July 09, 2022

Comments

  • Xsmael
    Xsmael almost 2 years

    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.

  • Xsmael
    Xsmael about 10 years
    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
    Xsmael about 10 years
    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
  • CptAJ
    CptAJ almost 9 years
    I second Xsmael's point here. Fixed layout is not a fix for this.
  • Purgatory
    Purgatory almost 7 years
    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/…
  • Jelmer Brands
    Jelmer Brands about 5 years
    Is this solution possible without defining a max width, but with taking the rest of the available space?
  • Luciano Fantuzzi
    Luciano Fantuzzi about 5 years
    @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.
  • Jelmer Brands
    Jelmer Brands about 5 years
    @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.
  • Luciano Fantuzzi
    Luciano Fantuzzi about 5 years
    @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).
  • Luciano Fantuzzi
    Luciano Fantuzzi about 5 years
    @JelmerBrands For us it's easy to see a space in a column and say "it must strip from here, it's obvious", but the browser is doing maths with containers widths, content length and screen space, so if no one says "this is the width, so stop and strip from there", it might assume you want, let's say, an horizontal scrolled table (just an example, it's more complicated than that). This is my modest understanding on this.