Sticky header
A sticky header stays at the top while the content under it scrolls past. The page's own
<header> is sticky with no class at all. For anything else, such as a
table head, a toolbar or the headings of a long list, add class="sticky".
The page header
The header at the top of the body is sticky already: it is the bar at the
top of this page. Its stacked bars stay pinned on a wide screen, and fewer of them stay on
narrower ones so the content keeps the room it needs.
A link to a section scrolls the page so the section lands below the header, not under
it, and a field that takes keyboard focus is not hidden behind it either. Both use
var(--pinned), the height of the header that stays on screen.
The page header belongs to the whole page, so it cannot sit in a box. The box beside the code is a stand-in: a yellow bar at the top of a small scrolling area. Scroll it to see the bar stay put, as the real header at the top of this page does.
Site name
The content scrolls up under the header.
The header stays where it is.
Further down the page.
Further still.
The end of the page.
<body>
<header>
<nav aria-label="Primary">…</nav>
</header>
<main>…</main>
</body>
A table head
In a tall table, a sticky thead keeps the column names in view. Here the
table sits in a scroll area, so the head sticks to
the top of that box. Scroll it to see.
| Name | Role | City |
|---|---|---|
| Ada | Engineer | London |
| Bea | Designer | Leeds |
| Cal | Writer | York |
| Dev | Engineer | Bristol |
| Eli | Manager | Bath |
| Fay | Engineer | Derby |
| Gus | Support | Hull |
| Hal | Designer | Ely |
| Ivy | Writer | Wells |
| Jo | Engineer | Ripon |
<div role="region" aria-label="Team" tabindex="0" style="max-height: 260px">
<table>
<thead class="sticky">
<tr><th scope="col">Name</th><th scope="col">Role</th><th scope="col">City</th></tr>
</thead>
<tbody>
<tr><td>Ada</td><td>Engineer</td><td>London</td></tr>
<tr><td>Bea</td><td>Designer</td><td>Leeds</td></tr>
…
</tbody>
</table>
</div>
Headings in a list
Make each group's heading sticky and it stays in view while its own group scrolls past, then the next heading pushes it away. A sticky element only sticks within its parent, so each heading lets go when its section ends.
A
Ada
Alma
Amir
B
Bea
Bram
Bruno
C
Cal
Cora
Cyrus
<div role="region" aria-label="Contacts" tabindex="0" style="max-height: 240px">
<section>
<h3 class="sticky" style="background: var(--surface)">A</h3>
<p>Ada</p><p>Alma</p><p>Amir</p>
</section>
<section>
<h3 class="sticky" style="background: var(--surface)">B</h3>
<p>Bea</p><p>Bram</p><p>Bruno</p>
</section>
…
</div>
Below the page header
Something sticky in the page itself, rather than in a scroll area, would slide under the
page header at top: 0. Set its top to
var(--pinned), as in style="top: var(--pinned)", so it comes
to rest just below the header instead.
Here the toolbar sits in a box that scrolls, so it sticks to the top of the box at
top: 0: scroll it to see. A sticky element needs something to scroll past
it before it does anything, so the paragraphs under the toolbar give it room to stick.
It lets go when its parent ends, because a sticky element never leaves its parent.
The first paragraph of the text being edited.
The second paragraph, further down.
The third paragraph, further still.
The fourth paragraph.
The end of the text.
<div role="region" aria-label="Editor" tabindex="0" style="max-height: 200px">
<div class="column">
<menu class="toolbar sticky">
<li><button type="button">Bold</button></li>
<li><button type="button" class="secondary">Italic</button></li>
<li><button type="button" class="secondary">Link</button></li>
</menu>
<p>The first paragraph of the text being edited.</p>
…
</div>
</div>
Accessibility
- Focus stays visible
- A sticky bar must not cover the thing that has keyboard focus. The page keeps a margin the size of the header at the top for links and focus, and a sticky element in the page should rest below the header.
- Keep it short
- A tall sticky bar takes room from the content on every screen. On a small screen or a zoomed page, pin less.
- Background
- Give anything sticky a solid background so the content scrolling under it does not show through and become hard to read.