Detect When position: sticky Is Actually Stuck: css scroll-state in Pure CSS

October 9, 2026  |  9 min read  |  CSS

By Syed · Frontend Engineer & Creator of UITweak

Every sticky header has two personalities: the relaxed one sitting in the page flow, and the pinned one clinging to the top of the viewport. For years, styling that second personality meant reaching for JavaScript: a scroll listener, a sentinel div, an IntersectionObserver, all just to toggle a class. css scroll-state queries finally answer that question in pure CSS. With @container scroll-state(stuck: top), the browser tells your stylesheet the exact moment a sticky element pins, and you restyle it with zero JavaScript running.

30-second version: Put container-type: scroll-state on the sticky element itself, then style a descendant inside @container scroll-state(stuck: top). A scroll-state container can never style itself, which is the gotcha that breaks every first attempt. Chromium 133+ supports it; Firefox and Safari still do not, so ship the small IntersectionObserver fallback for them.

The Old Pattern You Are Replacing

Before the native answer, there were two workarounds, and both still show up in every codebase. The scroll listener version reads the scroll position on every frame:

/* the classic: JS does the detecting, CSS does the painting */
const header = document.querySelector(".site-header");

window.addEventListener("scroll", () => {
  header.classList.toggle("is-stuck", window.scrollY > 0);
}, { passive: true });

This runs on the main thread during every scroll tick. With passive: true it is tolerable, but it still fires constantly for a boolean that changes twice per page view. The fancier version uses a sentinel div: an empty element parked right above the header, watched by an IntersectionObserver. When the sentinel leaves the viewport, the header must be stuck:

<!-- sentinel version: extra DOM, an observer, and a class toggle -->
<div class="sticky-sentinel"></div>
<header class="site-header">...</header>

<script>
  const header = document.querySelector(".site-header");
  const sentinel = document.querySelector(".sticky-sentinel");

  new IntersectionObserver(([entry]) => {
    header.classList.toggle("is-stuck", entry.boundingClientRect.top < 0);
  }).observe(sentin);
</script>

The sentinel version is efficient, but it is three extra moving parts (markup, observer, class) for what is conceptually one question: is this element stuck? css scroll-state queries move that question into the stylesheet, where it always belonged.

What css scroll-state Queries Actually Test

Scroll-state queries are a flavor of container queries. You declare an element a query container with container-type: scroll-state, then write @container scroll-state(...) blocks that test four kinds of scroll state:

  • stuck: is a position: sticky container pinned to an edge of its scroll container, like stuck: top.
  • snapped: is the container snapped to a scroll-snap ancestor along an axis, like snapped: x.
  • scrollable: can the container's scroll ancestor still scroll toward an edge, like scrollable: top.
  • scrolled: was the container most recently scrolled toward an edge (a newer state, Chromium-only).

The one we care about today is stuck. And here is the subtle part: the stuck state is asked of the sticky element itself, not of the page or the scroller. So the sticky header is the scroll-state container:

/* the sticky element IS the scroll-state container */
.site-header {
  position: sticky;
  top: 0;
  container-type: scroll-state;
}

@container scroll-state(stuck: top) {
  .site-header .header-inner {
    box-shadow: 0 4px 20px rgb(0 0 0 / 0.12);
    background: rgb(255 255 255 / 0.96);
  }
}

If you have several scroll-state containers on a page, give the header a name so queries do not leak across: container: page-header / scroll-state on the element, then @container page-header scroll-state(stuck: top). A named container keeps a carousel's snapped query from accidentally matching your header.

The Gotcha: A Container Cannot Style Itself

This is the part that trips everyone, including experienced CSS authors. Container queries match descendants of the container, never the container box itself. So this does absolutely nothing:

/* BROKEN: the container is not its own descendant */
.site-header {
  position: sticky;
  top: 0;
  container-type: scroll-state;
}

@container scroll-state(stuck: top) {
  .site-header {
    box-shadow: 0 4px 20px rgb(0 0 0 / 0.12); /* never applies */
  }
}

The selector matches. The query matches. But container query rules apply to children of the container, and .site-header is not its own child, so the rule is dead on arrival. DevTools will not even flag it; it just silently never fires. There are two clean ways around it.

Option A: Style a real descendant

Wrap the header's contents in an inner element and style that. This is the clearest pattern and the one to reach for first:

<header class="site-header">
  <div class="header-inner">
    <a href="/">Logo</a>
    <nav>...</nav>
  </div>
</header>
.site-header {
  position: sticky;
  top: 0;
  container-type: scroll-state;
}

/* the inner div carries all the visual styles */
.header-inner {
  background: transparent;
  box-shadow: none;
  transition: box-shadow 0.25s ease, background 0.25s ease;
}

@container scroll-state(stuck: top) {
  .site-header .header-inner {
    background: rgb(255 255 255 / 0.96);
    box-shadow: 0 4px 20px rgb(0 0 0 / 0.12);
  }
}

Option B: Use a pseudo-element

If you cannot touch the markup, the container's own ::before and ::after do count as descendants for container queries. Paint the stuck-state decoration on a pseudo instead:

.site-header {
  position: sticky;
  top: 0;
  container-type: scroll-state;
}

/* the shadow lives on a pseudo that covers the header */
.site-header::after {
  content: "";
  position: absolute;
  inset: 0;
  box-shadow: 0 4px 20px rgb(0 0 0 / 0.12);
  opacity: 0;
  transition: opacity 0.25s ease;
  pointer-events: none;
}

@container scroll-state(stuck: top) {
  .site-header::after {
    opacity: 1;
  }
}

Option B is handy for retrofitting an existing header, but Option A is easier to read and extend, so prefer the inner element for new code.

Recipe 1: The Stuck-Header Shadow With a Real Transition

The most common stuck-state design: the header is transparent over the hero, then gains a solid background and a soft shadow once it pins. The trap here is box-shadow itself, which cannot transition from none. Declare the shadow in both states with a zero alpha instead, so the transition interpolates cleanly:

.header-inner {
  background: rgb(255 255 255 / 0);
  box-shadow: 0 4px 20px rgb(0 0 0 / 0); /* alpha 0, NOT none */
  transition:
    background 0.25s ease,
    box-shadow 0.25s ease;
}

@container scroll-state(stuck: top) {
  .site-header .header-inner {
    background: rgb(255 255 255 / 0.96);
    box-shadow: 0 4px 20px rgb(0 0 0 / 0.12);
  }
}

When I prototyped this transition, I did not hand-tune the timing values blind. I used uitweak's animation generator to preview the ease curve and duration, then pasted the resulting transition declaration straight into the stuck-state rule. Generating the motion first and wiring it into the scroll-state query second keeps the trial and error in the tool instead of in your stylesheet. If you also want the shadow to look intentional rather than default, our box-shadow guide covers the multi-layer shadow syntax this recipe uses.

Recipe 2: Snapped vs Stuck, Same File

The same container-type: scroll-state declaration powers the other states too, and they compose in one stylesheet. A snap carousel can highlight whichever slide is currently snapped, with no observer per slide:

.slide {
  container-type: scroll-state;
}

@container scroll-state(snapped: x) {
  .slide .slide-card {
    outline: 2px solid #6366f1;
    outline-offset: 4px;
  }
}

And the scrollable state replaces the "show back-to-top only after scrolling" listener with a query on the scroller itself. Here the page root is the container, and the button is the descendant:

html {
  container-type: scroll-state;
  container-name: page;
}

.back-to-top {
  position: fixed;
  bottom: 1rem;
  right: 1rem;
  translate: 80px 0; /* parked off-screen */
  transition: translate 0.4s ease;
}

@container page scroll-state(scrollable: top) {
  .back-to-top {
    translate: 0 0; /* page scrolled down: slide in */
  }
}

Every one of these is decoration, which is exactly the point. A shadow that never appears in an older browser leaves a working header behind. If you want the deeper scroll-driven story, our scroll-driven animations guide pairs well with this one.

Recipe 3: The Honest Fallback for Firefox and Safari

Scroll-state queries are Chromium-only. That is verified, current, and the reason this section exists. For Firefox and Safari, keep the sentinel observer as a fallback, but gate it so it only runs where the CSS cannot. The same stuck styles get a plain class-based twin:

/* fallback twin of the stuck styles: identical, class-based */
.site-header.is-stuck .header-inner {
  background: rgb(255 255 255 / 0.96);
  box-shadow: 0 4px 20px rgb(0 0 0 / 0.12);
}
/* only run the observer where scroll-state is unsupported */
if (!CSS.supports("container-type: scroll-state")) {
  const header = document.querySelector(".site-header");
  const sentinel = document.createElement("div");

  sentinel.style.cssText = "position:absolute;top:0;height:1px;width:1px;";
  header.before(sentin);

  new IntersectionObserver(([entry]) => {
    header.classList.toggle("is-stuck", entry.boundingClientRect.top < 0);
  }).observe(sentin);
}

Two details matter here. First, the sentinel goes before the header, not inside it, so it scrolls away while the header stays. Second, the fallback test is CSS.supports("container-type: scroll-state"), not a user agent sniff, so it automatically retires itself the day Firefox or Safari ship the feature. And a hard rule: do not delete the JavaScript from a component where the stuck state changes behavior (a condensed nav layout, a hidden logo). Pure CSS is for appearance; keep JS for logic.

Browser Support: Verified, Not Baseline

Claims about this feature drift fast, so here is the verified picture as of October 2026:

  • Chrome / Edge 133+ (February 2025), Opera 118+, Samsung Internet 29+: full support for stuck, snapped, scrollable. The scrolled state landed later in Chrome 144.
  • Firefox: no support, including Firefox 158. WebKit position is still "no signal" for the shipping version.
  • Safari: no support, including Safari 27 and iOS Safari 26.x.
  • Global coverage: roughly 72% per caniuse. Not Baseline, and it should not be described as Baseline.
  • Feature detection: @supports (container-type: scroll-state) or CSS.supports("container-type: scroll-state").

The practical takeaway: treat scroll-state queries as progressive enhancement for appearance. Build the un-stuck state as the default and layer the stuck styling on top, so unsupported browsers get a perfectly fine header that simply never grows a shadow.

Quick Troubleshooting Checklist

  • Nothing happens at all: check the stuck rule targets a descendant or pseudo, not the container itself. Then confirm position: sticky plus a top value are set on the container.
  • Works in Chrome, invisible in Safari: expected. Safari ignores the whole block, so your default styles must carry the design alone. Add Recipe 3's fallback.
  • Shadow pops instead of fading: you transitioned from box-shadow: none. Use a zero-alpha shadow in the default state, or animate the pseudo's opacity like Option B.
  • Query matches the wrong element: an unnamed @container scroll-state(...) queries every scroll-state container on the page. Add container-name and target it explicitly.
  • Sticky never sticks in the first place: a common culprit is an ancestor with overflow: hidden, which silently disables sticking before any query can help.

Related: stuck-state transitions are entry and exit animations in disguise. If you want the header to animate its pinned layout (not just its colors), our @starting-style entry and exit animations guide covers the discrete-transition technique that makes those state flips smooth.

Frequently Asked Questions

Can a scroll-state container query style the container itself?

No. Like all container queries, scroll-state rules apply to descendants of the container. Style an inner wrapper element, or paint on the container's ::before / ::after, which do count as descendants. Writing the container's own selector inside the query block is the most common reason the feature "does not work".

Do I need container-type: scroll-state on every sticky element?

For stuck queries, yes: the query is evaluated against the sticky element itself, so each sticky element that needs stuck styling becomes its own scroll-state container. Use container-name to keep their queries from overlapping. Forscrollable queries it is the opposite: one container on the scroller (often html) covers the whole page.

Which browsers support css scroll-state queries?

Chrome and Edge 133+, Opera 118+, Samsung Internet 29+, about 72% global coverage as of October 2026. Firefox and Safari do not support them yet. It is not Baseline. Ship the stuck styling as enhancement with the un-stuck look as the default, and add the IntersectionObserver fallback from Recipe 3 for Firefox and Safari.

Can I replace all my scroll listeners with this?

Appearance listeners, yes: stuck shadows, snapped highlights, and scroll hints are exactly what scroll-state queries replace. Keep JavaScript for behavior: lazy loading, analytics, infinite scroll, and any layout change the stuck state triggers. CSS reports the state; JS still owns the logic.

Share this guide