I am trying to make a table with fixed header and a scrollable content using the bootstrap 3 table. Unfortunately the solutions I have found does not work with bootstrap or mess up the style.
Here there is a simple bootstrap table, but for some reason to me unknown the height of the tbody is not 10px.
height: 10px !important; overflow: scroll;
Example:
<link rel="stylesheet" type="text/css" href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css">
<table class="table table-striped">
<thead>
<tr>
<th>Make</th>
<th>Model</th>
<th>Color</th>
<th>Year</th>
</tr>
</thead>
<tbody style="height: 10px !important; overflow: scroll; ">
<tr>
<td class="filterable-cell">111 Ford</td>
<td class="filterable-cell">Escort</td>
<td class="filterable-cell">Blue</td>
<td class="filterable-cell">2000</td>
</tr>
<tr>
<td class="filterable-cell">Ford</td>
<td class="filterable-cell">Escort</td>
<td class="filterable-cell">Blue</td>
<td class="filterable-cell">2000</td>
</tr>
<tr>
<td class="filterable-cell">Ford</td>
<td class="filterable-cell">Escort</td>
<td class="filterable-cell">Blue</td>
<td class="filterable-cell">2000</td>
</tr>
<tr>
<td class="filterable-cell">Ford</td>
<td class="filterable-cell">Escort</td>
<td class="filterable-cell">Blue</td>
<td class="filterable-cell">2000</td>
</tr>
</tbody>
</table>
Here is the working solution
HTML
CSS
Link to jsfiddle
You can place two div where 1st div (Header) will have transparent scroll bar and 2nd div will be have data with visible/auto scroll bar. Sample has angular code snippet for looping through the data.
Below code worked for me -
Additional style to hide header scroll bar -
For whatever it's worth now: I did post a solution to a sister-thread Table scroll with HTML and CSS which
visibility
, notdisplay
) the header of the lower table and make the lower table scrollable w/in a divThe solution is agnostic to any styles / frameworks used - so maybe it's useful here as well...
A long description is in Table scroll with HTML and CSS / the code is also in this pen: https://codepen.io/sebredhh/pen/QmJvKy
You should try with "display:block;" to tbody, because now it's inline-block and in order to set height, the element should be "block"
I am using StickyTableHeaders on GitHub and it works like charm!
I had to add this css to make the header not transparent though.
Fixed table head - CSS-only
Simply
position: sticky; top: 0;
yourth
elements. (Chrome, FF, Edge)Fixed table head - using JS
You can use a bit of JS and translateY the
th
elementsjQuery example
Or plain ES6 if you prefer (no jQuery required):