Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap 4 text-truncate not working in a table cell

There are several questions on SE about this issue in Bootstrap 3, which have been solved by adding a custom class. Bootstrap 4 includes a text-truncate class to limit the display of text inside an element. We've used it in parts of the site without issue.

However, it doesn't work when applied to a table cell. Here's what we tried - in reality, there are multiple columns to the table but I've trimmed it down to one.

<table class="table table-striped table-bordered table-hover table-hover-cursor" id="tblData">
<thead class="thead-light">
<tr>
  <th scope="col" data-bind="tableSort: { arr: _data, propName: 'text()'}">Text</th>
</tr>
</thead>
<tbody data-bind="foreach: pagedData">
    <tr>
        <td data-bind="text: text()" class="text-truncate"></td>
    </tr>
</tbody>
</table>

There are various other question about this which suggest you need to put the text inside a span to have it work. But this doesn't work either.

<tr>
    <td>
      <span data-bind="text: text()" class="text-truncate"></span>
    </td>
</tr>

I've also tried moving the class to the td and having it on both the td and the span. None of it works.

Another common suggestion is to add the text class. Although that doesn't seem to be a default class in Bootstrap. This doesn't work either.

<tr>
    <td>
      <span data-bind="text: text()" class="text text-truncate"></span>
    </td>
</tr>

Again, moving or duplicating the class on the td doesn't make any difference. I wasn't sure if a size limitation might be needed so I tried this:

<tr>
    <td data-bind="text: text()" class="col-md-2 text-truncate"></td>
</tr>

But still the text is displayed in full with no elipsis.

In all cases I've check the element to make sure it's picking up the text-truncate class and that the styles are being applied, and they are.

This does work:

<tr>
    <td data-bind="text: text()" class="text-truncate" style="max-width: 200px"></td>
</tr>

But I'd prefer to stick to Bootstrap classes. What's the correct set of elements and classes to get this to work?

like image 309
Bob Tway Avatar asked Sep 19 '18 12:09

Bob Tway


People also ask

How do I truncate a paragraph in bootstrap?

Truncate long strings of text with an ellipsis. For longer content, you can add a . text-truncate class to truncate the text with an ellipsis.

What is truncate text?

TEXT TRUNCATION IS THE PROCESS OF shortening text content. If text is truncated, it is usually followed with 3 periods called an ellipsis. On webpages, there are several ways to shorten text content so that it fits within a certain designated area.

Which class adds zebra stripes to a table in bootstrap 4?

Which bootstrap classes is used to add a zebra stripe to the table ? Explanation: The table-stripped classes are used to create a striped table in bootstrap.

How to truncate text in a table using bootstrap?

To truncate the text in an HTML table, you first figure out what's the maximum width (in pixels) you can allow on small screens and then apply the text-truncate class along with max-width style like so: To truncate automatically, based on the available space, you need to restructure the whole thing into a Bootstrap grid. Then you can use this:

Why is my table not truncating text properly?

Despite setting the width and max width on the table to be 100%, one column does not truncate text properly. This problem typically occurs when the width of the container is not set. What classes should I be applying on Material table to truncate long text.

Is it possible to limit the display of text in Bootstrap?

Bootstrap 4 includes a text-truncate class to limit the display of text inside an element. We've used it in parts of the site without issue. However, it doesn't work when applied to a table cell.

How to create a table that is always responsive using bootstrap?

Bootstrap provides a responsive class .table-responsive that is always responsive in all breakpoints. However, you can use the breakpoints to make Bootstrap 4 tables responsive according to the specified breakpoint in a table as given below. You can create a table that is always responsive using Bootstrap 4.


1 Answers

AFAIK there isn't a Bootstrap class that will solve this if the table columns have fluid width. The simplest solution is to use:

.table {
    table-layout: fixed;
}

https://www.codeply.com/go/NysJfvWtst

like image 65
Zim Avatar answered Nov 03 '22 03:11

Zim