Fixed and sticky

Topic 2 of 4 in Positioning7 min readBeginnerFree course

Overview

Some elements should stay visible while the page scrolls: a site header, a “back to top” button, a table of contents, a cookie banner. CSS has two positioning values for this — fixed and sticky — and they behave quite differently.

position: fixed

A fixed element is removed from the flow and positioned relative to the viewport. It stays in the same place on screen no matter how far you scroll:

CSS
.back-to-top {
  position: fixed;
  right: 1.5rem;
  bottom: 1.5rem;
}

.cookie-banner {
  position: fixed;
  inset-inline: 0;
  bottom: 0;
}

Because fixed elements leave the flow, they cover whatever is underneath. A fixed header needs matching padding on the body, or the top of the page hides beneath it:

CSS
.site-header { position: fixed; top: 0; inset-inline: 0; height: 4rem; }
body { padding-top: 4rem; }   /* keep content out from under the header */

That coupling is fragile — if the header’s height changes (longer text, larger font), the padding is wrong. For headers, sticky is usually better.

Gotcha: like absolute positioning, fixed is relative to an ancestor with transform, filter or contain instead of the viewport. A “fixed” element inside an animated, transformed container scrolls with it.

position: sticky

A sticky element is a hybrid. It stays in the normal flow (taking up its space), scrolls normally — and when scrolling would move it past its offset, it sticks there:

Three snapshots of a scrolling panel. Before scrolling, the section header sits at the top of its section. Scrolled down, the header sticks to the top of the scroll container while the content scrolls beneath it. At the end of its section, the header is pushed off as its parent section leaves, and the next section’s header follows. Sticky with top: 0 scrolls normally, sticks at the top of its scroll container, and leaves with its parent section.

CSS
.site-header {
  position: sticky;
  top: 0;
  z-index: 10;
}

Because the header still occupies its space, the content below never hides under it — no body padding, no magic numbers.

The rules of sticky

  1. It needs an offset. top, bottom, left or right (or their logical versions) — without one, sticky behaves like relative.
  2. It sticks within its parent. Once the parent’s bottom edge scrolls past, the sticky element goes with it. That’s perfect for section headings in a long list — each heading sticks until its section ends — but it means a sticky element whose parent is the same height as itself never visibly sticks.
  3. It sticks relative to the nearest scroll container. If any ancestor between the sticky element and the page has overflow: hidden, auto or scroll, the element sticks inside that ancestor instead. If that ancestor doesn’t actually scroll, the element appears not to stick at all. (overflow: clip doesn’t create a scroll container, so it’s safe.)

Sticky in grid and flex layouts

Grid and flex items stretch to fill their row by default. A sidebar stretched to the full height of the article has nowhere to move, so it can’t stick. Stop it stretching:

CSS
.sidebar {
  position: sticky;
  top: 5rem;
  align-self: start;
}

When a link jumps to #section-2, the browser scrolls the heading to the very top — underneath your sticky header. Leave room for it:

CSS
h2, h3 { scroll-margin-top: 5rem; }
/* or for everything: */
html { scroll-padding-top: 5rem; }

Debugging “sticky doesn’t work”

Work through this checklist:

  1. Does it have top (or another offset)?
  2. Is its parent taller than it? If the parent is exactly its height, there’s nowhere to stick.
  3. Is it a grid/flex item being stretched? Add align-self: start.
  4. Does any ancestor have overflow: hidden/auto? Change it to clip, or move the sticky element out.
  5. Is something covering it? Add a z-index and a background colour.

Layering and backgrounds

Sticky and fixed elements usually sit above scrolling content, so give them:

  • a background colour — a transparent header lets text show through,
  • a z-index — positioned content later in the page would otherwise paint over them (see Stacking and z-index).

Mobile considerations

  • Keep sticky headers short. On a small phone in landscape, a 100px header plus browser toolbars can leave almost no room for content.
  • Consider hiding the header when scrolling down and showing it when scrolling up (this needs a little JavaScript).
  • Fixed bottom bars must leave room for the device’s home indicator: padding-bottom: env(safe-area-inset-bottom) (with viewport-fit=cover in the viewport meta tag).

Default to sticky for headers and sidebars: it keeps elements in the flow, so nothing needs to compensate for them. Use fixed for things that truly float above the page, like a chat button.

Exercise

A sticky header and table of contents

Build a page with a site header that stays at the top while scrolling, and an article layout with a sidebar table of contents (.toc) that sticks 1rem below the header while the article scrolls. Make sure in-page links (#section-2) don’t scroll their headings underneath the sticky header. Assume the header is 4rem tall.

HTML
<header class="site-header">Academy</header>
<div class="layout">
  <nav class="toc">
    <a href="#section-1">Section 1</a>
    <a href="#section-2">Section 2</a>
  </nav>
  <article>
    <h2 id="section-1">Section 1</h2>
    <p style="height: 120vh">…</p>
    <h2 id="section-2">Section 2</h2>
    <p style="height: 120vh">…</p>
  </article>
</div>

<style>
  * { box-sizing: border-box; }
  body { margin: 0; }
  .site-header { height: 4rem; background: #1b1d21; color: white; padding: 1rem; }
  .layout { display: grid; grid-template-columns: 12rem 1fr; gap: 2rem; padding: 1rem; }
  .toc { display: flex; flex-direction: column; gap: 0.5rem; }
  /* your code here */
</style>

Summary

  • position: fixed removes an element from the flow and pins it to the viewport.
  • position: sticky keeps the element in the flow and sticks it once scrolling reaches its top (or other) offset.
  • A sticky element can only stick within its parent, and relative to its nearest scrolling ancestor.
  • Sticky fails silently when an ancestor has overflow: hidden, when no offset is set, or when the parent is no taller than the element.
  • Use scroll-margin-top so anchor links don’t hide headings under sticky headers.