CSS scroll-state() Queries: Stuck, Snapped, Scrollable

CSS scroll-state() Queries: Stuck, Snapped, Scrollable

The zero-height sentinel and its observer existed only to tell CSS what the engine already knew. Working stuck, snapped, and scrollable examples, plus the enhancement-only rule.

Three familiar scroll-related styling jobs — a shadow when a sticky header pins, an active treatment on the currently snapped slide, an edge fade only when a container actually overflows — were each handled in JavaScript: an observer (an IntersectionObserver on a sentinel element, for the sticky header) or a scroll-size measurement, followed by a class toggle. Scroll-state container queries let the stylesheet ask the container about its own condition instead. This article covers the container requirement, working stuck, snapped, and scrollable examples, and the enhancement-only posture the current support picture demands.

The sentinel hack

position: sticky has a peculiar gap in it. The engine knows perfectly well when an element has stopped scrolling with the page and pinned against its boundary — that is the entire mechanism — but until recently it never told CSS. So the extremely ordinary request "give the header a shadow once it sticks" had no direct expression, and the workaround became folklore.

The sentinel version went like this. You inserted a zero-height div immediately before the sticky element, purely so there was something to observe. You attached an IntersectionObserver to that sentinel with a root margin tuned to the header's offset. When the sentinel scrolled out of view, the callback wrote a class onto the header; when it came back, the callback removed it. The stylesheet then keyed off the class. Three artifacts — a meaningless element, an observer, and a class — to express one condition the engine already knew.

The same gap produced two more patterns. Which slide in a snap container is currently snapped was tracked with its own observer and threshold arithmetic. Whether a container overflows at all — the precondition for showing a scroll affordance — needed a measurement comparing scroll size to client size, re-run on resize and on content change, and written back as yet another class.

Scroll-state queries close all three. The condition lives where the styling lives, and the sentinel, the observer, and the class disappear together.

The container model

The mechanism reuses the container query machinery. You declare the element whose scroll condition matters as a query container, and its descendants query it.

css
/* the sticky element is the container */
.site-header {
  container-type: scroll-state;
  position: sticky;
  inset-block-start: 0;
}

/* .site-header-bar, a descendant, runs the queries */

The constraint that catches people first: an element cannot query its own state. The query applies to descendants of the container, so the element whose state you query must itself be the container — the sticky element for stuck, the snap target for snapped, the scroll container for scrollable — and the styles go on content nested inside it. A wrapper around the sticky element does not work: stuck never matches when the query container is not itself sticky positioned. Something must be inside something, and the outer one is the element with the state.

One value is worth noting because it removes a false constraint: container-type: scroll-state does not apply size containment, so declaring it does not change how the element is sized (MDN, accessed November 2026). It can also be combined with a size container type on the same element when you need both. The mental model is otherwise identical to size container queries — including named containers with container-name when queries nest and you need to disambiguate which ancestor a rule is asking about.

stuck: the pinned header

The pinned state is queried directionally, naming the boundary the element has stuck to.

css
.site-header {
  container-type: scroll-state;
  position: sticky;
  inset-block-start: 0;
}

.site-header-bar {
  transition: box-shadow 0.2s ease, padding-block 0.2s ease;
}

@container scroll-state(stuck: top) {
  .site-header-bar {
    box-shadow: 0 2px 12px rgb(0 0 0 / 0.12);
    padding-block: 0.5rem;
    backdrop-filter: blur(8px);
  }
}

The directional values extend the same idea to the other edges — a toolbar stuck to the bottom of a scroll container, a sidebar heading stuck to an inline edge — so the query reads naturally for footers and rails, not only page headers.

The deletion here is concrete and worth stating in full, because it is the argument for the feature: the sentinel element leaves the markup, the observer and its root-margin tuning leave the JavaScript, the class-add and class-remove calls leave with it, and the stylesheet stops depending on a class whose only job was to mirror something the engine already knew. The compressed-on-pin variation — the header that shrinks its padding once stuck — used to require the same plumbing a second time with different thresholds. Now it is another declaration in the same block.

snapped and scrollable

The snapped condition matches when the container — a snap target in a scroll-snap container — is snapped, or would be snapped, to its scroll container on the given axis, and it styles content inside that snap target. It answers the active-item question directly, replacing the observer that watched slide intersections and maintained a current-index variable in order to write an active class.

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

@container scroll-state(snapped: x) {
  .slide-caption {
    opacity: 1;
    transform: none;
  }
}

That pairs with, but does not duplicate, the carousel construction work — the snap setup, markers, and navigation controls have their own treatment. What belongs here is only the styling that follows from being the snapped one.

The scrollable condition asks whether the container has clipped content in a given direction that the user can reach by scrolling, which is exactly the precondition for a scroll affordance. The container must itself be the scroll container — none of the values match otherwise — and a container with overflow: hidden or an overflow region the user cannot reach does not match either. Showing an edge fade or a more-content hint unconditionally is the wrong behavior; showing it only when it is true previously meant measuring scroll extents on load, on resize, and on every content mutation, then writing a class.

css
.panel {
  container-type: scroll-state;
  overflow-y: auto;
  max-block-size: 20rem;
}

.panel-fade {
  opacity: 0;
  transition: opacity 0.2s ease;
}

@container scroll-state(scrollable: bottom) {
  .panel-fade {
    opacity: 1;
  }
}

Three conditions, three retired observer patterns. Naming them individually is what makes the deletion inventory concrete when you are deciding whether the migration is worth doing in a given component — and it is also worth being precise that these are binary conditions, not scroll positions. The three conditions covered here answer stuck-or-not, snapped-or-not, more-or-not. Anything that needs a continuous value along the scroll is a different mechanism with its own coverage, and conflating the two leads to disappointment. A fourth condition, scrolled, queries the direction of a scroll container's most recent relative scroll; it shipped later, from Chrome and Edge 144 (as of Nov 2026, web-features).

Support posture: enhancement only

The support picture is the reason this article is framed the way it is. Scroll-state queries ship in Chromium engines — Chrome and Edge from version 133, released in February 2025 — and have not shipped in Firefox or Safari, which leaves the feature at limited availability rather than Baseline (Chrome for Developers for the Chrome 133 release; Web Platform Features Explorer for the Limited availability status, as of Nov 2026). Re-verify before you commit; a single-engine claim that ages silently into an assumed interoperability claim is the specific failure this dated note exists to prevent.

So the posture is enhancement-only, and it should be stated without hedging rather than dressed up as broad support with a caveat attached.

That posture is workable because of what these conditions are usually driving. A shadow on a pinned header, a fade at a scroll edge, a slightly emphasized active slide — these are affordance polish. Their absence is a plainer interface, not a broken one, and a user in Safari never learns they were supposed to see a shadow. That is the acceptable-loss test, and it is the only question worth asking: if the styling carries functional meaning — if the stuck state is how a user knows a control is now floating over content, or the snapped treatment is the only indication of which item is selected — then the loss is not acceptable and the observer stays in place until support broadens.

The layering rule matters as much as the decision. Ship the query in a block that unsupporting engines naturally ignore, or gate it explicitly, and let one mechanism run per browser. What you must not do is keep the observer running everywhere and add the query on top: two systems writing the same visual state, disagreeing at the edges, is worse than either alone. When the gate says the primitive is available, the JavaScript path should be off entirely — which also means the migration only pays off when you actually delete the observer, not when you merely stop using it.

Project Inquiry