Menu
CoddyTech

Sticky Table Headers

Part of the Responsive UI Patterns section of Coddy's Tailwind CSS journey. Lesson 24 of 55.

Long tables are easier to scan when their column labels remain visible. Use a bounded scrolling wrapper, then apply sticky and a top inset to the header cells. Sticky positioning keeps those cells in flow until their scrolling boundary reaches the inset.

<div class="h-48 overflow-y-auto"><table><thead><tr><th scope="col" class="sticky top-6 z-10 bg-white">Session</th></tr></thead><tbody><tr><td>Workshop</td></tr></tbody></table></div>

The example uses a 24px inset because another control occupies that space above the header. top-0 means no reserved space; top-8 reserves 32px. The relevant boundary is the nearest scrolling ancestor, not necessarily the page viewport.

Give sticky headers an opaque background and an appropriate stacking level such as z-10. A transparent header can let body text show through. Keep the native header cells and scopes so the visual persistence also retains data relationships.

challenge icon

Challenge

Medium

Keep the column headers at the top of the supplied 160px scrolling table region. Both header cells must use sticky positioning with a zero top inset, an opaque white background and stacking level 10. Preserve the caption and header scopes.

Preserve the supplied content, semantic elements and IDs. Edit the requested utility classes. Press Run preview after edits, then Check/Run code to grade the result. Supplied JavaScript is read-only.

Try it yourself

<!doctype html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Interface workshop</title><link rel="stylesheet" href="./styles.css"></head><body>
<div id="wrap" class="h-40 overflow-y-auto"><table id="table" class="w-full border-separate border-spacing-0"><caption>Studio schedule</caption><thead><tr><th scope="col" class="static bg-white">Session</th><th scope="col" class="static bg-white">Room</th></tr></thead><tbody><tr id="r1" class=""><th scope="row">Session 1</th><td id="cell1">Room 1</td></tr><tr id="r2" class=""><th scope="row">Session 2</th><td id="cell2">Room 2</td></tr><tr id="r3" class=""><th scope="row">Session 3</th><td id="cell3">Room 3</td></tr><tr id="r4" class=""><th scope="row">Session 4</th><td id="cell4">Room 4</td></tr><tr id="r5" class=""><th scope="row">Session 5</th><td id="cell5">Room 5</td></tr><tr id="r6" class=""><th scope="row">Session 6</th><td id="cell6">Room 6</td></tr><tr id="r7" class=""><th scope="row">Session 7</th><td id="cell7">Room 7</td></tr><tr id="r8" class=""><th scope="row">Session 8</th><td id="cell8">Room 8</td></tr><tr id="r9" class=""><th scope="row">Session 9</th><td id="cell9">Room 9</td></tr><tr id="r10" class=""><th scope="row">Session 10</th><td id="cell10">Room 10</td></tr><tr id="r11" class=""><th scope="row">Session 11</th><td id="cell11">Room 11</td></tr><tr id="r12" class=""><th scope="row">Session 12</th><td id="cell12">Room 12</td></tr></tbody></table></div>
</body></html>
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Responsive UI Patterns