Search code examples
htmlcsstwitter-bootstraphtml-table

Table fixed header and scrollable body


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>


Solution

  • Here is the working solution:

    table {
        width: 100%;
    }
    
    thead, tbody, tr, td, th { display: block; }
    
    tr:after {
        content: ' ';
        display: block;
        visibility: hidden;
        clear: both;
    }
    
    thead th {
        height: 30px;
    
        /*text-align: left;*/
    }
    
    tbody {
        height: 120px;
        overflow-y: auto;
    }
    
    thead {
        /* fallback */
    }
    
    
    tbody td, thead th {
        width: 19.2%;
        float: left;
    }
    <link href="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet"/>
    
    <table class="table table-striped">
        <thead>
            <tr>
                <th>Make</th>
                <th>Model</th>
                <th>Color</th>
                <th>Year</th>
            </tr>
        </thead>
        <tbody>
            <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>
            <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>

    Link to jsfiddle