Interaction & Visual Craft

CSS scroll-state() query replaces scroll-tracking JavaScript

The CSS scroll-state() container query detects scroll direction, overflow, sticky and snap state, replacing JavaScript scroll listeners with declarative styling across four features.

  • AuthorTom Whitfield
  • ChannelInteraction & Visual Craft
  • Filed
  • Read3 min
Scroll-state()
Innovalabs / pixabay
  • scroll-state() is a container query, not a function, with four features: scrolled, scrollable, stuck and snapped.
  • Logical values such as block-end and inline-start resolve by writing-mode, so queries work across translations.
  • Sticky-header restyling via stuck requires styling descendants because a container query cannot style its own container.
  • Resizing a stuck element can create an unstick-restick loop; a fixed wrapper height or transforms prevent it.
  • snap targets are styled through snapped on the snapped element, paired with scroll-snap-type on the scroller.

The CSS scroll-state() query moves four scroll-driven interface behaviors — direction reveals, scroll indicators, sticky-header restyling and snap animations — from JavaScript into declarative container queries. The feature is not a function, despite its parentheses. It is a container query in the same family as style(), asking the browser whether a container's scroll state matches a value; if the container lacks the feature entirely, the query returns false and the styles do not apply.

The syntax follows the container-query form: @container name scroll-state(feature: value) { /* styles */ }. Four features exist, each with a set of values.

What can scroll-state() detect?

  • scrolled: whether the container has scrolled, and in which direction. Values include none, the physical edges (top, right, bottom, left), logical edges (block-start, block-end, inline-start, inline-end), and the axes x, y, block and inline, with logical directions resolved by writing-mode.
  • scrollable: whether scrollable overflow exists past a given edge or axis. It turns off once the user reaches the end of the scrollable area — a built-in cutoff JavaScript implementations must track manually.
  • stuck: whether a position: sticky element is currently adhered to an edge. The query must sit on the stuck element, not the scroll container.
  • snapped: whether an element is a snap target on an axis (x, y, block, inline, both). It must also be defined on the snapped element and returns false when scroll snapping has not been applied.

The canonical demo is a back-to-top link. A fixed-position element starts translated 200px off-screen; a query on scroll-state(scrolled: block-end) resets the translate to zero, and a 0.5s transition animates the link into view. Reversing scroll direction animates it back out — roughly a dozen lines of CSS where a scroll listener and state tracking previously stood.

The scrollable feature suits a scroll-direction indicator for translated pages or full-viewport hero sections where the scroll affordance is ambiguous. Combining queries handles the edge case: an indicator shown with scroll-state(scrollable: block-end) can be suppressed once the user has scrolled in either direction, using a compound query with scrolled: block-end or scrolled: block-start. Both conditions are necessary, because scrolled: block-end alone stops matching when the user scrolls back up mid-page.

What does the stuck feature change for sticky headers?

A sticky header can restyle itself on contact — reduced padding (0.25em versus 0.5em), 0.8em font size, a semi-transparent background (#40313D99) and a rotated logo, each over a 1s transition. One structural constraint applies: a container query cannot style the container itself, not even via :has(). The wrapper carries container-type: scroll-state, container-name and position: sticky, while all visible changes must target descendant elements.

Resizing the stuck element creates a feedback loop the source documents precisely: the header sticks, shrinks, pulls away from the stuck point, un-sticks and sticks again, which can make scrolling past that point impossible. A transition mitigates it, and a fixed height on the stuck wrapper — ideally smaller than the header's final size — prevents it, at the cost of content overflow. Where overflow is unacceptable, transforms are the workaround: a transformed element leaves a placeholder in the layout that determines sticky position and does not move during animation.

The snapped feature pairs with scroll-snap-type: y mandatory on the html element (the scroller for document-level scrolling, not body) and scroll-snap-align: start plus scroll-snap-stop: always on each 100vh slide. A query for snapped: y then scales the slide's image to 1.9 over a 1s transition, with the same descendant-only styling rule as stuck.

Because every logical value respects writing-mode, the same queries survive translation into vertical or right-to-left scripts without conditional code — a maintenance gain for international interfaces. scroll-state() consolidates the last major class of scroll-dependent styling that still required imperative scripting, and its container-query shape means the browser, not the author, arbitrates when the styles apply.

Source: CSS-Tricks

More from Tom Whitfield

Tom Whitfield

Show full bio

Correspondent covering companies, deals and strategy in Product and interaction design, design systems at Interface Wire.

185 articles

Related articles

« Previous articleNext article »