Modern web development has long relied on JavaScript to handle complex scroll-driven interactions, from revealing navigation bars and fading in "back to top" links to triggering intricate animations based on user position. However, the introduction of the CSS scroll-state() query marks a paradigm shift in how front-end developers approach scroll behavior. By shifting these responsibilities entirely into the style sheet, web standards are empowering browsers to handle layout transformations with greater efficiency, improved performance, and cleaner codebases.
The scroll-state() query functions as a specialized container query—similar in syntax to @container style()—that allows developers to conditionally apply styles based on the real-time interaction between a container and its scrolling mechanics. Rather than relying on event listeners, scroll-device polling, or heavy Intersection Observer APIs, the browser natively evaluates the state of a container and dynamically applies CSS rules when specific conditions are met. This capability represents a significant milestone in the evolution of CSS, bridging the gap between static layout design and dynamic behavioral control.
Fundamental Syntax and Working Mechanism
To understand how scroll-state() operates, it is essential to examine its underlying mechanics. Despite its parentheses-heavy appearance, which often misleads developers into thinking it is a traditional CSS function, scroll-state() is a structural query used within @container rules.
The standard syntax follows a straightforward pattern:
@container container-name scroll-state( <scroll-state-feature>: <value> )
/* Conditional CSS rules applied when true */
When the browser evaluates this query, it checks whether a designated container possesses a specific scrolling characteristic matching the requested value. If the condition holds true, the encapsulated rules take effect. If the state changes—such as when a user scrolls away from a threshold—the query evaluates to false, and the styles are cleanly reverted. This declarative approach eliminates the synchronization lag often associated with main-thread JavaScript handlers, resulting in smoother frame rates and a more resilient user experience.
The Four Core Scroll-State Features
The specification defines four primary features that can be queried using scroll-state(): scrolled, scrollable, stuck, and snapped. Each feature provides granular insight into different aspects of layout behavior.
Scrolled: Tracking Directional Movement
The scrolled feature evaluates whether a designated scroll container has been displaced in a specific direction. For instance, developers can query whether a container has moved toward the block-end (typically downward in standard writing modes).
Consider the implementation of a dynamic "Back to Top" utility:
.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;
In this scenario, as the user scrolls toward the end of the block axis, the container query registers the state change, removes the vertical translation, and smoothly animates the link into view. Because native CSS transitions handle the interpolation, reversing the scroll direction gracefully animates the element back out of sight without requiring manual state management.
Scrollable: Conditional Boundary Indicators
The scrollable feature addresses a different design challenge: determining whether a container can still be scrolled further in a given direction. This is particularly useful for localized scrolling regions, overflowing hero elements, or multi-directional layouts where user affordances are necessary.
A common application involves displaying a scroll indicator arrow that remains visible only while content remains hidden beyond the viewport boundary:
.scrollindicator
display: none;
@container top-revealer scroll-state(scrollable: block-end)
display: block;
Once the user reaches the absolute end of the scrollable region, the scrollable: block-end state automatically evaluates to false, causing the indicator to disappear. By combining scrollable and scrolled queries using logical operators (or), developers can craft sophisticated UI patterns that display indicators only when relevant and suppress them immediately upon user interaction.
Stuck: Managing Sticky Elements
One of the most anticipated use cases for scroll state queries involves sticky positioning. Traditional CSS offers limited styling hooks for elements in a position: sticky state, often forcing developers to toggle classes via JavaScript when an element hits its offset threshold.
With scroll-state(stuck: top), the browser natively detects when a sticky element makes contact with its positioning edge. However, because the container query must be declared on the element itself, developers must structure their HTML carefully to avoid recursive styling loops.
.header-wrapper
container-type: scroll-state;
container-name: getting-stuck;
position: sticky;
top: 0;
@container getting-stuck scroll-state(stuck: top)
header
padding: 0.25em;
font-size: 0.8em;
background-color: #40313D99;
transition: 1s;
Addressing Layout Loops in Sticky Elements
A known technical hurdle when animating sticky elements is the potential for layout thrashing. If a sticky header shrinks upon becoming stuck, its total height decreases. This reduction can momentarily pull the element away from the sticky threshold, causing the container query to evaluate to false, which in turn restores the original size, re-initiating the stuck state in an infinite loop.
To mitigate this behavior, engineers recommend explicitly defining a fixed height on the outer wrapper element that is smaller than the expanded state, forcing the content to overflow or utilizing CSS transforms rather than layout-triggering properties. Because transforms leave a persistent placeholder in the Document Object Model (DOM), the browser calculates the sticky offset based on the original layout box, ensuring stable evaluation.
Snapped: Enhancing Scroll-Snap Interactions
Scroll snapping has transformed modern web design by allowing smooth, page-by-page or card-by-card navigation. The snapped feature extends this functionality by enabling style modifications precisely when a designated snap target locks into place.
.slide
height: 100vh;
scroll-snap-align: start;
container-type: scroll-state;
container-name: getting-snappy;
@container getting-snappy scroll-state(snapped: y)
img
scale: 1.9;
transition: 1s;
By defining the container type on individual slide elements rather than the parent scroller, developers can target specific children—such as enlarging images or revealing captions—only when that specific slide achieves active snap alignment.
Industry Implications and Future Outlook
The introduction of CSS scroll-state() queries aligns with a broader industry movement toward native, declarative web platform features. For years, performance bottlenecks on mobile devices and low-power hardware stemmed from heavy JavaScript scroll listeners executing style calculations on every tick of the event loop. By offloading these checks to the browser’s rendering engine, scroll-state() significantly reduces main-thread congestion, lowers battery consumption, and improves core web vitals metrics such as Interaction to Next Paint (INP).
As browser vendors continue to roll out implementation support, front-end architecture is expected to adapt rapidly. Design systems that previously depended on custom React or vanilla JavaScript hooks for sticky headers, scroll-linked progress bars, and snapping galleries can now be authored entirely in lightweight, declarative CSS. While developers must still exercise caution regarding layout loops and container hierarchy rules, the long-term impact of native scroll-state queries promises a cleaner, faster, and more accessible web ecosystem.