Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML css Table misaligned Columns

I tried to create a table in AngularJS with sticky header and footer. I've managed to do that; here's a Plunker demo and code:

<body ng-controller="MainCtrl as ctrl">
    <table class="table table-bordered table-hover">
        <thead>
            <tr>
                <th>
                    Column1
                </th>
                <th>
                    Column2
                </th>
                <th>
                    Column3
                </th>
                <th>
                    Column4
                </th>
                <th>
                    Column5
                </th>
            </tr>
        </thead>
        <tbody>
            <tr class="info" ng-repeat="item in items">
              <td>
                {{item.name}}
              </td>
              <td>
                  {{item.type}}
              </td>
              <td>
                  {{item.value}}
              </td>
              <td>
                  {{item.code}}
              </td>
              <td>
                  {{item.id}}
              </td>
            </tr>
        </tbody>
        <tfoot>
            <tr>
                <th>
                    Column1
                </th>
                <th>
                    Column2
                </th>
                <th>
                    Column3
                </th>
                <th>
                    Column4
                </th>
                <th>
                    Column5
                </th>
            </tr>
        </tfoot>
    </table>
  </body>

But the only problems are:

  1. The column width isn't dynamic, as you can see, in the first row the data overflows into the 2nd column.
  2. The columns are misaligned.

Any idea how to fix this?

like image 654
Richard Avatar asked Apr 29 '16 02:04

Richard


People also ask

How do you align columns in a table in HTML?

Table data defaults to left alignment; table headers to center. In order to change the alignment in one cell, insert the appropriate "ALIGN=" attribute within the code for that cell. In order to change the alignment in all cells in a row, insert the appropriate alignment attribute within the code for that row.

How do I fix aligned tables in HTML?

To center this table, you would need to add ;margin-left:auto;margin-right:auto; to the end of the style attribute in the <table> tag. The table tag would look like the following. Changing the style attribute in the <table> tag, as shown above, results in the table being centered on the web page, as shown below.

How do you fix column width in a table?

The width of the columns i.e. td in a table can be fixed very easily. This can be done by adding the width attribute in the <td> tag. If the width is not specified, the width of the column changes according to the change in the content. The specifications of width for the columns can be in pixels, or percentage.


4 Answers

The criteria for success of this question are:

  • Pure CSS.
  • Dynamically sized columns.
  • Sticky header and footer that size with the columns.

What you want is impossible.

The reason why the cells are almost right is because the table is semi-there, but there are actually multiple tables in the document. By overriding the <table> elements display type to flex, you've rendered the page in several different groups. The <thead> and <tfoot> are their own table, and their <tbody> is its own table. They do not size to one another, rather to other table cells in their own group.

Other CSS guides about this topic require a fixed width. http://joshondesign.com/2015/05/23/csstable

After playing around with it (specifically, trying to move the thead and tfoot to fixed positions), I've decided that any attempt to give specific rules to the header/footer breaks the table layout and will cause the sizing to work differently, which is why fixed width is required on the cells. Even in the examples, they are broken in this way, but the fixed width nullifies the problem.

Your absolute easiest fix is to fix the widths and give them overflow-x properties.

The alternative is to use JavaScript. With JS, all bets are off, and you can do anything you imagine. Specifically, you could use JS to autosize cells. I remember having success with a jQuery plugin that accomplished that.
https://www.datatables.net/

Otherwise, no. I cannot find any example online that does what you need it to do. Any attempts to get this specific layout working is a hack and you're better off making a no-JS version with overflow-x cells and fixed widths, and a JS-enabled version that autosizes cells.

like image 89
Josh Avatar answered Oct 23 '22 02:10

Josh


As you already know from the other answers, you should remove white-space: nowrap;, but there's also something you can do about the scrollbar:

table thead, table tfoot {
    width: calc(100% - 17px);
}

enter image description here

Updated Plunker

this looks perfect on my PC because the Windows scrollbars are 17px broad. If you want to be safe and check the scrollbar width, use this:

window.onload = function() {
    var tr = document.getElementById("__tbody").children[0];
    var scrWidth = window.innerWidth - tr.clientWidth - 3;
    var width = "calc(100% - " + scrWidth + "px)";
    document.getElementById("__thead").style.width = width;
    document.getElementById("__tfoot").style.width = width;
}

This calculates how broad the scrollbars are and then adjusts thead and tfoot. Of course then you have to set the ids <thead id="__thead">, <tbody id="__tbody"> and <tfoot id="__tfoot">.

like image 40
Aloso Avatar answered Oct 23 '22 04:10

Aloso


In order to return the table back to its normal, dynamically-resizing self, there are a few steps to follow. Each step mentioned will give freedom back to its respective table elements, making their lives much simpler.

First, remove all instances of flex. You want the table to act like a table, right? Next, let your thead, tr, and tfoot be themselves as well. Why make them display as a table? Lastly, your tbody is being set to display as a block. This, in a sense, segregates it from its other table friends, namely thead and tfoot. This creates a lonely situation for everyone involved.

Your final code will look like this:

table {
    table-layout: auto;
    max-height: 300px;
}

table thead, table tfoot,
table tbody tr {
    table-layout: fixed;
}

tbody td,
thead th,
tfoot td{
    border-right: 1px solid transparent;
    vertical-align: middle;
    white-space: nowrap;
}

table thead {
    width: 100%;
}
table tfoot {
    width: 100%;
}
table tbody {
    overflow-y: auto;
}

table tbody tr {
    width: 100%;
}

This will allow your table cells to be themselves--dynamically resizing as they see fit.

like image 4
Wes Foster Avatar answered Oct 23 '22 04:10

Wes Foster


Remove

white-space: nowrap;

and add

word-wrap: break-word;

tfoot td{
        border-right: 1px solid transparent;
        vertical-align: middle;
        word-wrap: break-word;
    }
like image 3
Mohsin Muzawar Avatar answered Oct 23 '22 03:10

Mohsin Muzawar