Is there a way to customize the alignment in a bootstrap table so that all content is left-aligned and the table utilizes only the required space

My goal is to create a table layout as shown below:

[ header  ] [ header 2 ] [ header 3      ]
----------------------------------------------------------------------100%
 cell here   and longer   and even longer
----------------------------------------------------------------------100%
 another     row          here
----------------------------------------------------------------------100%

In the example above, I am aiming for a table with a width of 100%, allowing the striped table rows (i.e.

<table class="table table-striped">
) to span the entire width. However, I need all content to be left-aligned and occupy only the necessary space. I have experimented with different CSS styles such as:

table.something thead th { width: auto !important }
table.something thead th { white-space: nowrap; width: 1%; }
table.something tbody td { white-space: nowrap; width: 1%; }

This customization is required by a specific design specification that has to be adhered to. Otherwise, I would simply stick with the standard Bootstrap table formatting.

Answer №1

To ensure that the table and its cells adjust to fit the content, set the table-layout to auto. Then, designate the width of the last column as 100% to compress the other columns. To prevent text wrapping in other columns, use white-space: nowrap.

HTML

<table class="table table-striped">
    <thead>
        <tr>
            <th>[header]</th>
            <th>[header 2]</th>
            <th>[header 3]</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>cell here</td>
            <td>longer cell here</td>
            <td>longer longer and even longer cell here</td>
        </tr>
        <tr>
            <td>cell</td>
            <td>longer cell</td>
            <td>longer longer and even longer cell</td>
        </tr>
        <tr>
            <td>cell here here</td>
            <td>longer cell here here</td>
            <td>longer longer and even longer cell here here</td>
        </tr>
    </tbody>
</table>


CSS

.table {
    table-layout: auto;
}

.table td, .table th {
    white-space: nowrap;
}

.table thead th:last-child {
    width: 100%;
}


Screenshot

https://i.sstatic.net/CN2Jf.png


See Demo: https://jsfiddle.net/davidliang2008/bd7g1mxp/20/

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

I'm looking to use JavaScript to dynamically generate multiple tabs based on the selected option in a dropdown menu

I'm reaching out with this question because my search for a clear answer or method has come up empty. Here's what I need help with: I've set up a dropdown titled 'Number of Chassis'. Depending on the selection made in this dropdown ...

Could the reason behind the malfunctioning of the bootstrap tabs be due to my CSS files?

I'm looking to implement Bootstrap tabs in the sidebar of my website, but I'm running into an issue where the tabs aren't functioning properly within the sidebar-parsel-liste class. If I place the tab tags outside of this class, everything w ...

Execute the executable file on the server and display the output on the client side

Is it feasible to execute an executable file on the web server and display the image on the client side using various technologies like PHP, JS, or HTML? I am interested in achieving this to avoid sharing the exe file directly with the client but still al ...

What is the best way to assign a class to objects with a count greater than a

Can someone assist with editing a foreach loop to add a class to divs only if the number of divs is greater than 3, without affecting those with fewer divs? @if(!empty($property->testimonials)) @foreach($property->testimonials as $testimonial) ...

What are some ways to customize the appearance of Stripe credit card form fields?

I've successfully implemented stripe using this HTML code and Bootstrap 4. <div class="form-control" id="card-element"></div> As a result, I get the displayed output: https://i.sstatic.net/Aw653.png However, I am loo ...

What could be causing my absolutely positioned div to be hidden from view?

I'm working on designing a slideout slideshow with three images and content that looks like papers hidden in folders, complete with a handle at the top or bottom of each folder displaying its name. Here's an example of what I'm envisioning: ...

Is it possible to implement counters in CSS with Jquery?

I am trying to implement a specific style when an if statement is executed, but Jquery is encountering syntax issues and generating errors. Does anyone have a solution for escaping these errors so that I can still apply the desired styling? The problemati ...

Active Arrow Link

I'm currently working on customizing the appearance of my WordPress site's categories sidebar by adding an arrow to the left side of the active link. After tweaking the CSS to achieve the desired behavior and making some color adjustments for te ...

What are the steps to integrating databases with HTML5?

I currently have SPA applications developed with angularjs, but I am in need of creating an installer that allows users to work offline using a secure local database. I experimented with IndexedDB and converted my SPA into a Chrome extension, however, I di ...

My PHP script is not functioning correctly with Ajax

I am currently working with HTML5, PHP, and JavaScript. My goal is to implement Ajax in order to display the sizes of a selected product when an option is chosen from #productoSeleccionado. However, I believe that there may be an issue with my code as the ...

Why doesn't the <img> with the class "w-100" fit inside the parent flex div if the <img> is smaller than the parent div in Bootstrap 4?

I've been racking my brain over this issue, but I just can't seem to find a solution. So here's the scenario: Adding an image with a width of 100px to a desktop site, and applying the w-100 class should make it stretch to match the width of ...

The width of my root container does not display at a full 100% in mobile dimensions

After creating a simple React.js project, I set up the folder structure using `npx create-react-app`. Additionally, I added some styling with `* { margin: 0; padding: 0; box-sizing: border-box }`, and specified background colors for the body and #root elem ...

Angular pop-up message not displaying title or content

I have integrated the toaster directive into my AngularJS web project. Following the documentation, I have declared the container on my view as shown below. <toaster-container toaster-options="{'time-out': 3000, 'close-button':true ...

``Passing a variable to a Jquery function - A step-by-step guide

As a beginner in JQuery, I am exploring various functions available online, one of which enables the display of a tooltip when hovering over an HTML element. This is the specific function: <script> $(document).ready(function() { $('#my-tool ...

The icons within the <i> tag are failing to appear on my Ionic webpage

Trying to incorporate the tag icon in the HTML, but encountering issues with displaying the icon. Here is the code snippet I am using to display the icon, but unfortunately it's not appearing: <i class="fas fa-plus"></i> Intere ...

Creating a slider with a large central image and just three slides that automatically transition

I am trying to create a slider that includes: 3 different images 3 different text captions A button that opens a modal dialog similar to Bootstrap, with each slide's button displaying unique text in the modal The slider should automatically slide Fo ...

Problem with stacking order in Internet Explorer 7

Our current issue involves a z-index problem with a div that should be positioned above another specific div. The div in question, named (house-over-banner), is set to absolute positioning, while the one below it is relative. Interestingly, everything dis ...

Looking to incorporate a pre-checked checkbox using Angular Material

I'm facing an issue with the following code snippet: <ng-container matColumnDef="Estado"> <th mat-header-cell *matHeaderCellDef>Estado</th> <td mat-cell *matCellDef=""> <mat-checkbox *ngIf ...

I'm attempting to make the button hover color transparent, but unfortunately, it's not having any effect

Check out the code snippet below. I'm attempting to adjust the button hover color to transparent. The div class is "button_container" while the button class is "btn". I made changes to the color initially, but it doesn't seem to be working. Any a ...

Oops! An error occurred while trying to load the myApp module. The module 'ui.bootstrap' is missing and causing the failure

When using Firefox, I encountered the following error: SyntaxError: syntax error xml2json.js:1 SyntaxError: syntax error ui-bootstrap-tpls-0.13.0.js:1 Error: [$injector:modulerr] Failed to instantiate module myApp due to: [$injector:modulerr] Failed to in ...