Web developers have long relied on JavaScript scroll event handlers, custom dynamic scripts, and complex DOM manipulations to build reactive interface elements. Common UI patterns—such as revealing a hidden "Back to Top" button, shrinking a navigation bar when it sticks to the top of the viewport, or highlighting carousel items as they snap into place—have historically required constant monitoring of scroll position. This script-heavy approach often introduced performance bottlenecks, scroll-jank, and complex state management overhead.
The introduction of the CSS scroll-state() container query marks a significant shift in how browsers handle stateful user interfaces. Despite its function-like appearance with parentheses, scroll-state() is not a standard CSS function; rather, it operates as a specialized @container query. By allowing styles to respond natively to scrolling behavior, the feature enables developers to build highly interactive layouts directly within CSS, eliminating the need for scripting for many standard scroll-driven UI patterns.
Syntax
The syntax for container scroll-state queries builds directly upon the established CSS Container Queries specification. Instead of querying a container’s inline size or dimensional constraints, the browser evaluates stateful features related to scrolling positioning and layout states.
@container scroll-state( <scroll-state-feature>: <value> )
/* styles */
Under this architecture, the browser continuously queries whether a designated container element meets specific structural scroll criteria. If the query evaluates to true, the browser applies the CSS rules contained within the declaration block. If the feature is missing or its current value does not match the conditions specified in the query, the container query evaluates to false, and the enclosed styles are ignored.
Values
The scroll-state() specification introduces four primary container query features, each engineered to evaluate a distinct operational state of a layout element.
scrolled
The scrolled feature checks whether a container element has undergone scrolling movement in a specified direction.
scrollable
The scrollable feature evaluates whether a container currently possesses overflow content that permits scrolling in a given direction.
stuck
The stuck feature determines whether an element with position: sticky has reached its specified boundary and is currently anchored within the viewport.
snapped
The snapped feature detects whether a scroll snap target has locked into place along a designated axis within a scroll snap container.
Demos
To understand how container scroll-state queries operate in practical layout scenarios, it is helpful to examine how each feature handles specific UI requirements.
Scrolled
The scrolled feature allows elements to react based on whether a container has been scrolled in a given logical direction, such as block-start, block-end, inline-start, or inline-end. A typical application of this state is revealing a "Back to Top" interface control once the user begins navigating down a page.
.toplink
position: fixed;
bottom: 0.125em;
right: 1em;
translate: 0 200px;
transition: translate 0.5s;
@container top-revealer scroll-state(scrolled: block-end)
translate: 0 0;
transition: translate 0.5s;
In this implementation, the .toplink element begins positioned outside the visible viewport area via a CSS translation (translate: 0 200px). When the target scroll container, identified as top-revealer, undergoes scrolling toward its block-end (downward in standard top-to-bottom writing modes), the query evaluates to true. The element then translates back to its default position (translate: 0 0), sliding smoothly into view. Because the movement is governed by CSS transitions, scrolling back toward the block-start automatically resets the layout, smoothly animating the element back offscreen without JavaScript event listeners.
Scrollable
While scrolled tracks actual movement, scrollable detects the existence of scrollable real estate. This is particularly valuable in dynamic layouts, responsive hero sections, or multi-language sites utilizing varied writing modes where visible directional cues enhance usability.
.scrollindicator
display: none;
@container top-revealer scroll-state(scrollable: block-end)
display: block;
A key operational advantage of scrollable is its self-terminating behavior. When a user scrolls to the absolute end of the scrollable region, scrollable: block-end evaluates to false because no additional content remains in that direction. Consequently, the scroll indicator automatically hides itself.
Developers can also combine multiple state queries using logical operators to refine control over UI visibility based on position:
.scroll-indicator
display: none;
@container top-revealer scroll-state(scrollable: block-end)
display: block;
@container top-revealer scroll-state(scrolled: block-end) or scroll-state(scrolled: block-start)
display: none;
In this configuration, the scroll indicator is rendered when additional content exists toward the end of the block axis, but hides as soon as active scrolling is detected in either direction.
Stuck
Querying the sticky state of an element introduces distinct structural requirements compared to traditional container queries. When evaluating scroll-state(stuck: ...), the container query context must be defined on the wrapper element that becomes stuck, rather than an arbitrary outer scroll ancestor.
Because CSS container queries prevent an element from directly styling itself based on its own container state—even when attempting to leverage the :has() relational pseudo-class—developers must establish a container parent and target child elements inside the declaration block.
.header-wrapper
container-type: scroll-state;
container-name: getting-stuck;
position: sticky;
top: 0;
header
display: flex;
flex-wrap: wrap;
align-items: center;
background-color: #40313D;
padding: 0.5em;
font-family: Tahoma;
Once the parent .header-wrapper attaches to the top boundary, descendant styles within <header> can be mutated dynamically:
@container getting-stuck scroll-state(stuck: top)
header
padding: 0.25em;
font-size: 0.8em;
background-color: #40313D99;
transition: 1s;
header .search ::is(input, button)
font-size: 0.8em;
header .logo
width: 1.5em;
rotate: -375deg;
transition: 1s;
Stuck in the Middle
Applying dimensional changes to elements inside a stuck container can trigger layout instability. If shrinking a stuck header reduces the container’s overall height, the element may pull away from its sticking threshold. This immediate loss of sticky status cancels the applied styles, restoring the original larger size, which instantly makes it sticky again. This cycle creates a rapid visual layout loop, often manifesting as severe element flickering.
To stabilize layouts susceptible to sticky recalculation loops, developers can apply fixed structural dimensions to the outer wrapper element (.header-wrapper). Setting a explicit height on the sticky container prevents content size shifts from altering the container’s physical layout bounds. If content must shrink without overflowing, CSS visual transformations (scale, translate) offer a stable alternative, as transformed elements preserve their original layout placeholder within the DOM during rendering.
Snapped
Container scroll-state queries also provide integration with CSS Scroll Snap layouts. When configuring snap detection, the scroll-state(snapped: ...) feature is assigned to individual snap target elements rather than the overarching scroll container. If scroll snapping is not explicitly configured or activated on the target, the query resolves to false.
Scroll snap behavior is first established on the scrolling root:
html
scroll-snap-type: y mandatory;
Individual target elements are subsequently established as container contexts:
.slide
height: 100vh;
scroll-snap-align: start;
scroll-snap-stop: always;
container-type: scroll-state;
container-name: getting-snappy;
When a target slide locks into position along the specified axis, child elements can be transformed or animated accordingly:
@container getting-snappy scroll-state(snapped: y)
img
scale: 1.9;
transition: 1s;
As with the stuck state query, styles cannot be applied directly to the container element itself, requiring visual alterations to be applied to descendant nodes within the snapped slide structure.
Browser Support
Container scroll-state queries represent an evolving standard in modern CSS layout specifications. Browser implementation and Baseline status tracking reflect ongoing integration efforts across major web rendering engines, aiming to streamline state-driven styling and reduce standard web project dependencies on script-based scroll handlers.