Adjusting the width of columns in a table

Previously in Bootstrap 3, I was able to use col-sm-xx on the th tags within the thead to resize table columns as needed. However, this functionality is no longer available in Bootstrap 4. How can I achieve a similar effect in Bootstrap 4?

<thead>
<th class="col-sm-3">3 columns wide</th>
<th class="col-sm-5">5 columns wide</th>
<th class="col-sm-4">4 columns wide</th>
</thead>

When looking at the provided codeply example, it is clear that the sizing does not work properly, especially when adding data to the table. See how it behaves:

<div class="container" style="border: 1px solid black;">
    <table class="table table-bordered">
    <thead>
        <tr class="row">
            <th class="col-sm-3">3 columns wide</th>
            <th class="col-sm-5">5 columns wide</th>
            <th class="col-sm-4">4 columns wide</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>123</td>
            <td>456</td>
            <td>789</td>
        </tr>
    </tbody>
</table>
</div>

Answer №1

Latest Update in 2018

When creating a table, it is important to include the table class. This is because Bootstrap 4 tables require intentional opt-in, meaning the table class must be specifically added to the table.

For more information and examples, visit:

In Bootstrap 3.x, there was CSS code to reset table cells so they don't float...

table td[class*=col-], table th[class*=col-] {
    position: static;
    display: table-cell;
    float: none;
}

Although this feature isn't included in Bootstrap 4 alpha versions, it may be reinstated in the final release. By adding this CSS, all columns will utilize the specified widths in the thead.

To see a demo of Bootstrap 4 Alpha 2, click here: Bootstrap 4 Alpha 2 Demo


LATEST UPDATE (as of Bootstrap 4.0.0)

With Bootstrap 4 now using flexbox, table cells won't automatically adjust width when using col-*. To work around this issue, apply the d-inline-block class on the table cells to avoid the default column display:flex.

Another option in Bootstrap 4 is to utilize sizing utility classes for setting widths...

<thead>
     <tr>
           <th class="w-25">25</th>
           <th class="w-50">50</th>
           <th class="w-25">25</th>
     </tr>
</thead>

To view a demonstration of Bootstrap 4 Alpha 6, follow this link: Bootstrap 4 Alpha 6 Demo

Lastly, applying d-flex on table rows (tr), with col-* grid classes on columns (th,td) can be useful...

<table class="table table-bordered">
        <thead>
            <tr class="d-flex">
                <th class="col-3">25%</th>
                <th class="col-3">25%</th>
                <th class="col-6">50%</th>
            </tr>
        </thead>
        <tbody>
            <tr class="d-flex">
                <td class="col-sm-3">..</td>
                <td class="col-sm-3">..</td>
                <td class="col-sm-6">..</td>
            </tr>
        </tbody>
    </table>

For a live example of Bootstrap 4.0.0 stable version, check out: Bootstrap 4.0.0 (stable) Demo

Please note that changing TR to display:flex might have an impact on borders. Read more about it here

Answer №2

If you're looking for an alternative approach, you can utilize flex styling directly on the table row and then assign the col-classes to the table header or table data elements:

<table>
  <thead>
    <tr class="d-flex">
      <th class="col-3">Header with 3 columns width</th>
      <th class="col-sm-5">Header with 5 columns width</th>
      <th class="col-sm-4">Header with 4 columns width</th>
    </tr>
  </thead>
  <tbody>
    <tr class="d-flex">
      <td class="col-3">Content with 3 columns width</th>
      <td class="col-sm-5">Content with 5 columns width</th>
      <td class="col-sm-4">Content with 4 columns width</th>
    </tr>
  </tbody>
</table>

Answer №3

When using the d-flex class, everything works smoothly except for certain attributes like the vertical-align: middle property.

A helpful way I discovered to easily size columns is by utilizing the width attribute with percentage values specifically in the thead cells.

<table class="table">
    <thead>
        <tr>
            <th width="25%">25%</th>
            <th width="25%">25%</th>
            <th width="50%">50%</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>25%</td>
            <td>25%</td>
            <td>50%</td>
        </tr>
    </tbody>
</table>

Answer №4

Here is my custom solution that I developed for Bootstrap 4.1.1 before coming across a similar post by @florian_korner.

If you are using sass, simply add this code snippet at the end of your bootstrap includes. It resolves compatibility issues with Chrome, IE, and Edge without causing any problems in Firefox.

@mixin make-td-col($size, $columns: $grid-columns) {
    width: percentage($size / $columns);
}

@each $breakpoint in map-keys($grid-breakpoints) {
    $infix: breakpoint-infix($breakpoint, $grid-breakpoints);

    @for $i from 1 through $grid-columns {
        td.col#{$infix}-#{$i}, th.col#{$infix}-#{$i} {
            @include make-td-col($i, $grid-columns);
        }
    }
}

If you only need the compiled CSS utility, here it is:

[...]

Answer №5

Important Note: Please be aware that this answer contains information that may no longer be up-to-date. Bootstrap 4 has undergone changes since its beta release.

The class for defining table column size has been modified as shown below:

<th class="col-md-4">4 columns wide</th>

To:

<th class="col-4">4 columns wide</th>

Answer №6

To tackle this issue, I have come up with a solution that involves using Bootstrap 4 components:

<table class="table">
  <tbody>
    <tr class="d-flex">
      <th class="col-3" scope="row">Indicator:</th>
      <td class="col-9">Here goes the indicator</td>
    </tr>

    <tr class="d-flex">
      <th class="col-3" scope="row">Definition:</th>
      <td class="col-9">This is how I define it</td>
    </tr>

  </tbody>
</table>

Answer №7

Starting from Alpha 6, you have the ability to generate this unique sass file:

@each $breakpoint in map-keys($grid-breakpoints) {
  $infix: breakpoint-infix($breakpoint, $grid-breakpoints);

  col, th, td {
    @for $i from 1 through $grid-columns {
        &.col#{$infix}-#{$i} {
          flex: none;
          position: initial;
        }
    }

    @include media-breakpoint-up($breakpoint, $grid-breakpoints) {
      @for $i from 1 through $grid-columns {
        &.col#{$infix}-#{$i} {
          width: calc(100% / #{$grid-columns} * #{$i});
        }
      }
    }
  }
}

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

The image fails to reach full width and does not expand correctly when I zoom out

At the bottom of my website, I have a bar for displaying certain content. However, I am facing an issue where the bar is not extending to cover the entire screen width despite having code in place to do so. The HTML code is quite basic: <div class="bo ...

What could be the reason for the top alignment of only a portion of the cell

Here is the code snippet I am working with: <html> <head> <style> table.t_group { border: 2px solid black; margin: 0 auto 0 auto; } table.t_group > tbo ...

Trouble with implementing conditional styles in Next JS

<label className={`${darkModeActive ? 'text-brand-five' : 'text-brand-seven'} ${errors.invoiceDate ? 'text-[#EC5756]' : ''}`} htmlFor="invoiceDate"> Invoice Date </label> I am encountering a ...

How do you use CSS to replace an HTML "rules" attribute?

When publishing to HTML using the DITA Open Toolkit, certain inline table attributes are automatically applied such as frame="border" and rules="all". I am facing an issue where I need to override the "rules" attribute for table cells using CSS styles. Wh ...

Neatly incorporating a checkbox into a miniaturized image

I need help with the code snippet below, where I want each thumbnail image to have a checkbox overlay. I want the images to take up all the space in the table cell without any white space above. It is essential that the "left" and "right" divs are aligned ...

Please request user input in order to generate a multiplication chart

Is there a way to ensure that the program works properly so that when the user inputs a value, it is included in the multiplication table? <html> <head> <title> Multiplication Table </title> <style> body{ font-family: aria ...

JavaScript and CSS animations out of sync in terms of timing

I've been encountering some issues. I have an array containing 5 lines of text that change with a timer, but it seems that the css timer animation might not be synced properly or my @keyframes slidesdown setup could be incorrect. The delay between te ...

Preventing overlapping of a "position-fixed" element with variable size: Effective strategies

I am trying to keep a fixed element visible at the top of the page while also having content with varying heights. Here is the CSS code for the fixed element: div.topfloat { position: fixed; top: 0; width: 100%; } The issue I'm facing is ...

Tips for Resizing google reCAPTCHA

The problem's image is shown below: https://i.sstatic.net/ph2EX.jpg Below is the code snippet related to the issue: <div class="text-xs-center" id="DIVCATCHA" runat="server"> <asp:HiddenField runat="server" ID="hdnImageSelected" /> ...

HTML input field's placeholder text is truncated at the end

My HTML input form has a field with placeholder text that extends beyond the padding and gets cut off at the end. https://i.sstatic.net/9r46F.png I am unable to pinpoint the source of this issue. ...

Ways to manipulate a div tag with CSS even when it lacks a class or ID attribute

Hey there! I've got this snippet of HTML code that I'm working with: <div class="template-page-wrapper"> <div class="templemo-content-wrapper"> <div class="templatemo-content"> <div c ...

Adjusting the Height of the Video Jumbotron in Bootstrap

One of my main goals is to create a jumbotron with a video that plays, occupying 100% width and 70% height of the screen. However, I am facing an issue where the video follows the scrolling behavior when I don't want it to. Specifically, as I scroll d ...

Understanding the usage of jQuery transitionend to prevent interruptions in CSS animations

I found a similar thread on StackOverflow but I'm struggling to apply it in my current scenario. The Setup (Welcome Ideas) I've developed a jQuery slider that will have multiple instances on one page, each containing an unknown number of childr ...

"Explore the functionalities of Drupal's innovative Menu module: How sub-sub menus can take precedence over sub-menus

When visiting , I noticed that the Nice Menu module for Drupal is not displaying sub-sub menus properly. If you navigate to the first item in the menu and then select the first one in the sub-menu (Facilities->Spring->Calendar), you'll see that ...

How to Align Title in the Center of a Section Containing Multiple Images?

I have a situation where I am using a StyledHead section to display 10 images from an API. The images are wrapped in another section called StyledHeader, which also contains an h1 element. How can I center the h1 element both vertically and horizontally so ...

The stacking order of elements is not affected by the z-index property when using absolute positioning

I have developed a unique custom toggle switch component with the following structure: <template> <div> <label class="switch"> <input type="checkbox" :checked="value" @c ...

Isotope grid shatters when browser window is resized

My goal is to design a 3-column grid using Twitter Bootstrap and Isotope. However, when I resize the browser to force the layout into a single column mode, Isotope fails and leaves large spaces both vertically and horizontally. Regular Layout Issues in ...

Is it possible to customize the CSS styles of a jQuery UI modal dialog box?

Having trouble with my CSS styles not applying to a dialog modal added to my website using jQuery UI, even when using '!important'. I suspect that the predefined jQuery or UI CSS styles from a CDN link are preventing me from overriding them. The ...

Steps to include a horizontal divider in the footer section of an angular-material table

Is it possible to add a line between the last row (Swimsuit) and the footer line (Total)? If so, how can I achieve this using Angular 15? https://i.stack.imgur.com/581Nf.png ...

Today's Date Bootstrap Form

How can I show today's date in a Bootstrap form with the input type set to 'date' and another input with the type set to 'time'? Most solutions I've found involve changing the input type to 'text'. Is there a way to ...