Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<table> with fixed <thead> and scrollable <tbody> [duplicate]

I've looked through questions here and articles all over the internet, but haven't found yet solution that would satisfy my requirements. So now in 2017, is there an elegant way to have a <table> that would:

  1. be written in html+css (no js)
  2. have fixed header (not scrollable; not sticky)
  3. have scrollable <tbody> (scrollbar may be always visible)
  4. header and body would handle resizing properly and not mess alignment of the <thead> columns and the <tbody> columns
  5. would not use nested tables or separate table for header
like image 270
Dmitry Stril Avatar asked Dec 09 '17 00:12

Dmitry Stril


People also ask

How do I make my table scrollable with fixed header?

Create a Table That Has a Fixed Header. We can create an HTML table that has a fixed header with some CSS. We set the height of the table element to 120px to make restrict the height of it so we can make it scrollable. To make it scrollable, we set the overflow CSS property to scroll .

Can thead come after Tbody?

A <table> element. The <thead> must appear after any <caption> or <colgroup> element, even implicitly defined, but before any <tbody> , <tfoot> and <tr> element.

How do I add a scroll in table Tbody?

If you want tbody to show a scrollbar, set its display: block; . Set display: table; for the tr so that it keeps the behavior of a table. To evenly spread the cells, use table-layout: fixed; . Anyhow, to set a scrollbar, a display reset is needed to get rid of the table-layout (which will never show scrollbar).


1 Answers

This solution fulfills all 5 requirements:

table {    width: 100%;  }    table, td {    border-collapse: collapse;    border: 1px solid #000;  }    thead {    display: table; /* to take the same width as tr */    width: calc(100% - 17px); /* - 17px because of the scrollbar width */  }    tbody {    display: block; /* to enable vertical scrolling */    max-height: 200px; /* e.g. */    overflow-y: scroll; /* keeps the scrollbar even if it doesn't need it; display purpose */  }    th, td {    width: 33.33%; /* to enable "word-break: break-all" */    padding: 5px;    word-break: break-all; /* 4. */  }    tr {    display: table; /* display purpose; th's border */    width: 100%;    box-sizing: border-box; /* because of the border (Chrome needs this line, but not FF) */  }    td {    text-align: center;    border-bottom: none;    border-left: none;  }
<table>     <thead>       <tr>        <th>Table Header 1</th>        <th>Table Header 2</th>        <th>Table Header 3</th>      </tr>     </thead>    <tbody>      <tr>        <td>Data1111111111111111111111111</td>        <td>Data</td>        <td>Data</td>      </tr>      <tr>        <td>Data</td>        <td>Data2222222222222222222222222</td>        <td>Data</td>      </tr>      <tr>        <td>Data</td>        <td>Data</td>        <td>Data3333333333333333333333333</td>      </tr>      <tr>        <td>Data</td>        <td>Data</td>        <td>Data</td>      </tr>      <tr>        <td>Data</td>        <td>Data</td>        <td>Data</td>      </tr>      <tr>        <td>Data</td>        <td>Data</td>        <td>Data</td>      </tr>      <tr>        <td>Data</td>        <td>Data</td>        <td>Data</td>      </tr>      <tr>        <td>Data</td>        <td>Data</td>        <td>Data</td>      </tr>      <tr>        <td>Data</td>        <td>Data</td>        <td>Data</td>      </tr>      <tr>        <td>Data</td>        <td>Data</td>        <td>Data</td>      </tr>    </tbody>  </table>
like image 77
VXp Avatar answered Sep 21 '22 16:09

VXp