Difficulty encountered with text truncation provided by Bootstrap 4 in a table cell

Many questions on SE address this particular issue in Bootstrap 3 and have found solutions by adding a custom class. In Bootstrap 4, a text-truncate class was introduced to restrict the display of text within an element, and we have successfully implemented it in various parts of our site.

However, we encountered a problem when applying the text-truncate class to a table cell. Here's what we attempted - although in reality, there are multiple columns in the table which I have condensed to one for demonstration purposes.

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

Several other suggestions mention enclosing the text in a span to achieve the desired effect, but this approach also proved unsuccessful.

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

We even experimented with moving the class to the td or using it on both the td and the span, but none of these methods yielded the desired result.

An oft-recommended suggestion is to include the text class, even though it is not a default Bootstrap class. However, this strategy did not work either.

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

Despite trying different combinations and variations of classes and elements, including attempts to set size limitations, such as:

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

The text continued to display in full without any ellipsis or truncation.

Ultimately, we resorted to explicitly setting a max-width style to achieve the desired effect:

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

However, we would prefer to adhere to using only Bootstrap classes. What combination of elements and classes should be used to resolve this issue?

Answer №1

As far as I know, there is no predefined Bootstrap class that can address this issue if the table columns have variable widths. An easy fix would be to apply the following CSS:

.table {
    table-layout: fixed;
}

https://www.example.com/code-example123

Answer №2

Try moving the class="text-truncate" from the <td> to a nested span that has its own custom class for better styling

<td>
  <span class="my-class text-truncate">Long text that needs truncating</span>
</td>
.my-class {
  display: inline-block;
  max-width: 10rem;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using HTML to place an image tag close to an input element

I'm experiencing a strange issue where adding an img tag alongside an input and a span causes the input and span to shift downwards. <div> <span>Credit Points</span> <input type="text" name="credit_points" value="2.25"/> ...

Steady Navigation Bar in Javascript without Bouncing

While experimenting with a fixed navigation bar, I've encountered an issue where the content below jumps up on the page when the navigation bar "fixes" itself to the top of the screen. You can check out the JSFiddle I've been working on for refer ...

Ways to determine the height of a row within a flexbox

Is it possible to obtain the height of each row within a flexbox container using JavaScript? For instance, if there are 3 rows in the container, can I retrieve the height of the second row specifically? ...

Displaying an image extracted from a database as a blob data type on a fresh PHP page

I have created a code that successfully retrieves an image from a database. However, I am facing a challenge with opening the image on a new page when the user clicks on it. I have attempted a few solutions but have been unsuccessful... Below is the code ...

Creating a sticky footer using Vue.js for both mobile and desktop display views

Looking for a solution to create a sticky footer that works on both desktop and mobile views without overlapping the main content? The current code either overlaps or doesn't stick to the bottom. Seeking a responsive approach. App.vue: <template& ...

Python (Selenium) - Guide on extracting and storing data from multiple pages into a single CSV file

I am facing a challenge with scraping data from a web page that contains a table spanning multiple pages. I am using Python with selenium for this task. The website in question is: The issue I am encountering is related to navigating through the pages of ...

Having difficulty aligning dynamic lists with their container on a horizontal navigation menu

I am trying to create an expanding list that drops relative to the parent's position. Right now, I have set the position to absolute, which lines up the lists incorrectly. When I change it to relative positioning, I get the desired result but there is ...

Safari experiencing a CSS issue not found in Chrome or Firefox

https://gist.github.com/2354116 When you check the link above in Chrome or Firefox, everything looks good. The divs at the bottom, including the headings and social icons, are centered within a container div without any issues. However, when viewed in Sa ...

The navigation bar components created with Bootstrap are experiencing issues with their active state functionality

I created a Navbar using Bootstrap code but I am facing an issue where the 'active' state is not being applied to the Navbar elements on my webpage. I have been attempting to add and remove the active class on the Navbar elements, but I have bee ...

How can you achieve a vertical list display for a label while preserving its inline-block properties?

I have chosen to use labels for my radio buttons because I enjoy the convenience of being able to click on the text to select the option. However, I want these labels to appear in a vertical list format. Currently, they are displaying as blocks, causing th ...

What is the best method to apply a background image in HTML that functions properly in Internet Explorer?

Having trouble getting this CSS code to work in Internet Explorer. Need a solution that is compatible with IE for setting the background. body { background: url("images/Login-BG.png") no-repeat center center fixed; -moz-background-size: cover; -webkit ...

Apply only the css style to the first adjacent div with a specific class name

Is it possible to apply a specific style to only the first div with the class "bla" and not the second? <div class="outer"> <div> ....(more div's, unknown how many) <div class="bla"> .... <div class="b ...

Guide to highlighting input field text using Angular

I've set up an angular page that includes an input field within its template. My goal is to highlight the text in the input field when a specific function is triggered. When I refer to "highlighting the text," I mean (check out the image below) https ...

Transitioning in Vue.js can be triggered by changing a value up or down

My current transition block component is set up like this: <div v-if="!surveyResultIsReady" class="vh-md-40 position-relative" > <transition name="custom-classes-transition" enter-active-class="animated slideInRight" ...

I am experiencing a problem with my Dynamic Table where it duplicates the titles every time a new entry

Attempting to generate dynamic tables using the Repeat Region feature in Dreamweaver CS5 is causing an issue where each new record entry also repeats the table titles when viewed in a browser. COMPANY REF DATE highbury 223333 2014- ...

Conceal the Angular Material toolbar (top navigation bar) automatically when scrolling downwards

In my Angular application, the main navigation consists of a standard toolbar positioned at the top of the page. My goal is to have this navigation bar smoothly scroll up with the user as they scroll down, and then reappear when they scroll back up. I at ...

Substitute placeholders in the HTML code with Angular syntax

Lately, I have been encountering some issues with AngularJS. I have defined multiple scopes like this: app.controller('mainController', function($scope) { $scope.siteURL = "my website url"; }); When using the {{siteURL}} variable in ...

Ways to enlarge the diameter of the inner circle

I've implemented some CSS with JQuery, as seen in this JSFiddle. However, I'm having trouble increasing the width and height of the green circle to be slightly larger than the gray circle (referred to as the main circle) which is positioned at a ...

Emulating form functionality: How to programmatically open a link using PHP

Do you want to provide package tracking on your website like DHL does? With DHL, users can track their packages by visiting a specific URL, such as . When a tracking number is entered, it opens a new window with the following format: SOMENUMBER1234 If you ...

Toggle class on element based on presence of class on another element

If I have 4 boxes and the user is required to choose one. I aim to assign an active class to the box that the user clicks on. However, if the user selects another box, I want to remove the class from the first clicked box and apply it to the newly clicked ...