Fixed Header Table

An all CSS fixed header table.

by mrcl

HTML

<div class="table-container">
    <div class="heading-bg"></div>
    <div class="table-container-inner">
        <table cellspacing="0">
            <thead>
                <tr>
                    <th>
                        <div class="heading">Heading 1</div>
                    </th>
                    <th>
                        <div class="heading">Heading 2</div>
                    </th>
                    <th>
                        <div class="heading">Heading 3</div>
                    </th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>First Columnghregztez tez zzez</td>
                    <td>First Row</td>
                    <td>First Row</td>
                </tr>
                <tr>
                    <td>First Column</td>
                    <td>Second Column</td>
                    <td>Third Column</td>
                </tr>
                <tr>
                    <td>First Column</td>
                    <td>Second Column</td>
                    <td>Third Column</td>
                </tr>
                <tr>
                    <td>First Column</td>
                    <td>Second Column</td>
                    <td>Third Column</td>
                </tr>
                <tr>
                    <td>First Column</td>
                    <td>Second Column</td>
                    <td>Third Column</td>
                </tr>
                <tr>
                    <td>First Column</td>
                    <td>Second Column</td>
                    <td>Third Column</td>
                </tr>
                <tr>
                    <td>First Column</td>
                    <td>Second Column</td>
                    <td>Third Column</td>
                </tr>
                <tr>
                    <td>First Column</td>
                    <td>Second Column</td>
                    <td>Third Column</td>
                </tr>
                <tr>
                   ...

CSS

td {
    border-bottom: 1px solid #ccc;
    padding: 5px;
    text-align: left;
}
td + td {
    border-left: 1px solid #ccc;
}
th {
    padding: 0 5px;
    text-align: left;
}
.table-container {
    width: 100%;
    height: 200px;
    border: 1px solid black;
    margin: 10px auto;
    background-color: white;
    position: relative; /* or absolute */
    padding-top: 30px; /* matches height of header */
}
.table-container-inner {
    overflow-x: hidden;
    overflow-y: auto;
    height: 100%;
}
.heading-bg {
    background-color: #ccc;
    height: 30px; /* matches padding of table-container */
    position: absolute;
    top: 0;
    right: 0;
    left: 0;
    border-bottom: 1px solid black;
}
table {
    background-color: white;
    width: 100%;
    overflow-x: hidden;
    overflow-y: auto;
}
.heading {
    position: absolute;
    top: 0;
https://jsfiddle.net/user/login/    line-height: 30px;
    text-align: left;
    border-left: 1px solid black;
    padding-left: 5px;
    margin-left: -5px;
}
th:first-of-type .heading {
    border-left: none;
    padding-left: 6px;
}
tr>td:first-child {
    background-color: aliceblue; 
    width: 100px;
}