Sticky table header in CSS
29 A table header that stays put
fairMake the header row of a scrolling table stick to the top. Then fix the border that disappears when you do, which is the part nobody warns you about.
Stuck? Three hints, smallest first
- 1. Put
position: stickyon the<th>cells, not on<thead>. - 2. A sticky cell needs an opaque
background, or the rows scroll through it. - 3. With
border-collapse: collapsethe borders belong to the table, not the cell, so they do not travel. Useseparateand an insetbox-shadow.
<div class="scrollwrap" tabindex="0" role="region" aria-label="Departures">
<table class="sched">
<caption>All departures today</caption>
<thead>
<tr>
<th scope="col">Time</th>
<th scope="col">Route</th>
<th scope="col">Bay</th>
</tr>
</thead>
<tbody>
<tr><th scope="row">06:30</th><td>Abuja</td><td>B</td></tr>
<tr><th scope="row">08:00</th><td>Kano</td><td>C</td></tr>
<tr><th scope="row">11:15</th><td>Enugu</td><td>D</td></tr>
<tr><th scope="row">14:45</th><td>Ibadan</td><td>A</td></tr>
<tr><th scope="row">17:00</th><td>Benin</td><td>E</td></tr>
<tr><th scope="row">19:30</th><td>Warri</td><td>F</td></tr>
</tbody>
</table>
</div>
.scrollwrap {
max-height: 150px;
overflow: auto;
border: 1px solid #c9cad1;
border-radius: 8px;
}
.scrollwrap:focus-visible {
outline: 2px solid #3055d8;
outline-offset: 2px;
}
/* separate, not collapse: with collapsed borders the lines belong to the table
and stay behind while the sticky cell travels. */
.sched {
width: 100%;
border-collapse: separate;
border-spacing: 0;
font-size: 0.82rem;
}
.sched caption {
padding: 8px 10px;
text-align: left;
color: #5c5c66;
font-weight: 600;
}
.sched thead th {
position: sticky;
top: 0;
background: #17171a; /* opaque, or the rows show through */
color: #ffffff;
text-align: left;
padding: 8px 10px;
/* The bottom rule, drawn as a shadow so it travels with the cell. */
box-shadow: inset 0 -1px 0 #5c5c66;
}
.sched tbody th,
.sched tbody td {
padding: 7px 10px;
border-bottom: 1px solid #e4e5e9;
text-align: left;
font-weight: 400;
}
8 checks that prove this answer
Assertions, run in the browser. The markup and styles above are handed to them
as html and css, so press Run and each check prints
ok. The build fails if a single one fails.
test('the header cells are sticky, not the thead', /thead th\s*\{[^}]*position:\s*sticky/.test(css), true);
test('with a threshold to stick at', /top:\s*0/.test(css), true);
test('and an opaque background, or rows show through', /thead th\s*\{[\s\S]*?background:\s*#17171a/.test(css), true);
test('borders are separate, so the sticky cell keeps its rule', /border-collapse:\s*separate/.test(css), true);
test('collapse is avoided for exactly that reason', /border-collapse:\s*collapse/.test(css), false);
test('the header rule is drawn as an inset shadow instead', /box-shadow:\s*inset 0 -1px 0/.test(css), true);
test('the wrapper is what scrolls, and by keyboard too', /overflow:\s*auto/.test(css) && /tabindex="0"/.test(html), true);
test('every header still declares its scope', (html.match(/scope="(col|row)"/g) || []).length, 9);
Output
position: sticky on <thead> is ignored in several browsers, so it goes on the <th> cells. Then two things go wrong that look like separate bugs and are not. The header is transparent by default, so rows scroll through it — hence an opaque background. And with border-collapse: collapse the borders are owned by the table rather than the cell, so they stay behind while the cell travels, leaving the header with no bottom rule. Switching to separate with border-spacing: 0 and drawing the line as an inset shadow fixes it without changing the look.