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".

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.

NameRoleCity
AdaEngineerLondon
BeaDesignerLeeds
CalWriterYork
DevEngineerBristol
EliManagerBath
FayEngineerDerby
GusSupportHull
HalDesignerEly
IvyWriterWells
JoEngineerRipon
<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>
A sticky table head in a scroll area

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>
Group headings that hand over to each other

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>
    A sticky toolbar over text that scrolls

    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.