The position: sticky
CSS property offers support for both sticking elements to the top and the side in modern versions of Chrome, Firefox, and Edge browsers. You can leverage this feature by pairing it with a div
that has the overflow: scroll
property, allowing you to create a table with fixed headers that can be positioned anywhere on your webpage.
To implement this, start by enclosing your table within a container:
<div class="container">
<table></table>
</div>
Add the overflow: scroll
style to your container element to enable scrolling functionality:
div.container {
overflow: scroll;
}
An important note from Dagmar: make sure to set a max-width
and a max-height
for your container as well.
You can utilize the position: sticky
property to have certain table cells stick either to the edge or specific sides like top
, right
, or left
:
thead th {
position: -webkit-sticky; /* needed for Safari */
position: sticky;
top: 0;
}
tbody th {
position: -webkit-sticky; /* needed for Safari */
position: sticky;
left: 0;
}
In case your first column contains <td>
elements instead of <th>
elements, consider using tbody td:first-child
in your CSS per MarredCheese's suggestion.
To ensure the header in the first column remains stuck to the left, here's what you should adjust in your styles:
thead th:first-child {
left: 0;
z-index: 1;
}
/* Use overflow:scroll on your container to enable scrolling: */
div {
max-width: 400px;
max-height: 150px;
overflow: scroll;
}
/* Use position: sticky to have it stick to the edge
* and top, right, or left to choose which edge to stick to: */
thead th {
position: -webkit-sticky; /* for Safari */
position: sticky;
top: 0;
}
tbody th {
position: -webkit-sticky; /* for Safari */
position: sticky;
left: 0;
}
/* To have the header in the first column stick to the left: */
thead th:first-child {
left: 0;
z-index: 2;
}
/* Just to display it nicely: */
thead th {
background: #000;
color: #FFF;
/* Ensure this stays above the emulated border right in tbody th {}: */
z-index: 1;
}
tbody th {
background: #FFF;
border-right: 1px solid #CCC;
/* Browsers tend to drop borders on sticky elements, so we emulate the border-right using a box-shadow to ensure it stays: */
box-shadow: 1px 0 0 0 #ccc;
}
table {
border-collapse: collapse;
}
td,
th {
padding: 0.5em;
}
<div>
<table>
<thead>
<tr>
<th></th>
<th>headheadhead</th>
<th>headheadhead</th>
<th>headheadhead</th>
<th>headheadhead</th>
<th>headheadhead</th>
<th>headheadhead</th>
<th>headheadhead</th>
</tr>
</thead>
<tbody>
<tr>
<th>head</th>
<td>body</td>
<td>body</td>
<td>body</td>
<td>body</td>
<td>body</td>
<td>body</td>
<td>body</td>
</tr>
<tr>
<th>head</th>
<td>body</td>
<td>body</td>
<td>body</td>
<td>body</td>
<td>body</td>
<td>body</td>
<td>body</td>
</tr>
<tr>
<th>head</th>
<td>body</td>
<td>body</td>
<td>body</td>
<td>body</td>
<td>body</td>
<td>body</td>
<td>body</td>
</tr>
<tr>
<th>head</th>
<td>body</td>
<td>body</td>
<td>body</td>
<td>body</td>
<td>body</td>
<td>body</td>
<td>body</td>
</tr>
<tr>
<th>head</th>
<td>body</td>
<td>body</td>
<td>body</td>
<td>body</td>
<td>body</td>
<td>body</td>
<td>body</td>
</tr>
<tr>
<th>head</th>
<td>body</td>
<td>body</td>
<td>body</td>
<td>body</td>
<td>body</td>
<td>body</td>
<td>body</td>
</tr>
</tbody>
</table>
</div>
Visit this link to see the result in action!