CSS scroll-state() Queries: Practical Patterns That Work
Learn how CSS scroll-state() queries detect scrolling, sticky positioning and snap points, with practical patterns, setup guidance and trade-offs.

CSS scroll-state() Queries: Build Scroll-Aware Interfaces Without JavaScript
Things like a navigation bar that changes when it sticks, a back-to-top link that shows up after you've scrolled a bit, or a slide that reacts when it snaps into place have usually meant JavaScript. CSS now has another option: the scroll-state() query. It lets styles react to a few specific, scroll-related states for a container (or element).
That doesn't make it a universal replacement for scroll event handlers. scroll-state() reports particular states - like scroll direction or whether something is stuck - but it won't give you a continuous scroll position, and it won't cover every interaction pattern that relies on event sequences. And picking the wrong tool can make an interface harder to debug, not easier.
This guide walks through what scroll-state() evaluates, how to set up its four features, and where the practical limits show up in real projects. You'll see common patterns for directional controls, sticky headers, and scroll-snapped content - plus the setup details that tend to trip people up.
What does CSS scroll-state() query?
Despite the parentheses, scroll-state() isn't a CSS function that returns a value. It's a condition you use inside a container query. The browser checks whether a configured container is in a matching scroll-related state. If it is, the declarations inside the @container rule apply; if not, they don't.
The general form looks like this:
@container container-name scroll-state(feature: value) {
/* styles for matching descendants */
}
To use it, your container has to be configured with container-type: scroll-state, and you'll typically give it a name. For example:
.page-scroller {
container-type: scroll-state;
container-name: page-scroll;
}
Then you can target descendants inside that container:
@container page-scroll scroll-state(scrolled: block-end) {
.back-to-top {
opacity: 1;
}
}
One detail that matters: you're not applying the conditional styles to the container itself. Container queries apply to elements inside the container. So for cases like sticky headers or snap targets, you usually put the scroll-state container capability on a wrapper (or the element that's snapping), then style its children.
The four features answer different questions
scroll-state() supports four feature groups. They're related, but they solve different problems:
scrolledtells you the direction of the most recent relative scroll. It's about the latest movement, not the user's current "where am I?" position.scrollabletells you whether the container has scrollable overflow in a given direction. It can become false at an edge when there's no more content to scroll that way.stucktells you whether a sticky-positioned element has been pushed to an edge.snappedtells you whether a snap target has snapped on an axis.
Directions can be physical (top, right, bottom, left) or logical (block-start, block-end). Logical directions follow the document's writing mode. In a typical English page, block-end is "down," but that won't be true for every writing system.
A simple way to choose: use the feature that matches the question your UI needs answered. scrollable for "is there more content this way?" scrolled for "which way did the user just move?" And use stuck/snapped for the state of the specific sticky or snapped element.
How do you use scrolled and scrollable states?
The scrolled feature is handy when the direction of the user's latest movement matters. A common example is showing a back-to-top control when someone scrolls toward the end of the page. You can do that without a JavaScript scroll listener.
.page-scroller {
container-type: scroll-state;
container-name: page-scroll;
}
.back-to-top {
position: fixed;
inset-block-end: 1rem;
inset-inline-end: 1rem;
translate: 0 200px;
transition: translate 250ms ease;
}
@container page-scroll scroll-state(scrolled: block-end) {
.back-to-top {
translate: 0 0;
}
}
That transition is what makes the change feel smooth as the query starts and stops matching. In this example, block-end is the downward direction for the usual English writing mode. Using logical directions is generally safer than hard-coding bottom, especially if you're dealing with different writing modes.
One important limitation: scrolled is about the most recent relative movement, not about a threshold like "after 500px." It doesn't tell CSS how far down the page the user is. If you need "reveal after X distance," you'll likely want a scroll-driven animation or JavaScript - depending on the exact behavior you need and what browsers you support.
Use scrollable for available content, not user progress
scrollable answers a different question: is there overflow you can still scroll in a particular direction? That makes it a good fit for directional hints - things like "swipe right" or "scroll down for more" - that should show only when additional content is available.
@container page-scroll scroll-state(scrollable: block-end) {
.scroll-hint {
display: block;
}
}
When the page reaches the end in the block-end direction, there's no more overflow to scroll that way, so the condition stops matching and the hint can return to its default hidden state.
You can combine scrolled and scrollable, but make sure the result matches how the product should behave. For instance, if you want a hint that hides after the user starts scrolling in either direction, don't base it only on scrolled: block-end. If they scroll back toward block-start, that condition would flip and the hint might come back. Add the second direction if that's not what you want.
@container page-scroll scroll-state(scrollable: block-end) {
.scroll-hint {
display: block;
}
}
@container page-scroll scroll-state(scrolled: block-end),
page-scroll scroll-state(scrolled: block-start) {
.scroll-hint {
display: none;
}
}
In practice, the "right" setup depends on your UI. A persistent hint might make sense for a horizontal carousel, while a one-time cue on a long article could get annoying after the first interaction. Also test the default state - especially if a browser doesn't support the feature or the query hasn't matched yet - so the page stays usable.
How do stuck and snapped queries work?
The stuck feature applies to a sticky-positioned element that has reached an edge. Unlike the scroll-container states, this state belongs to the sticky element itself. Set up a wrapper as the scroll-state container, make it sticky, then style descendants when the wrapper is stuck.
.header-wrapper {
position: sticky;
inset-block-start: 0;
container-type: scroll-state;
container-name: header-state;
}
.site-header {
padding: 0.75rem 1rem;
background: #40313d;
transition: padding 200ms ease, background-color 200ms ease;
}
@container header-state scroll-state(stuck: top) {
.site-header {
padding: 0.4rem 1rem;
background: rgb(64 49 61 / 85%);
}
}
Because container queries style descendants, the rule doesn't directly style .header-wrapper itself. Keeping the visual change on .site-header makes it clear what's changing and avoids weird "query from inside itself" situations.
Avoid sticky-size feedback loops
If your header gets shorter when it becomes stuck, it can create a feedback loop:
- the sticky state changes the header size,
- that changes the sticky boundary,
- the element may no longer count as "stuck,"
- then it returns to the original size and becomes stuck again.
The result can be flicker or a stutter near the top of the viewport.
A practical fix is to keep the sticky wrapper's height stable, so internal layout changes don't alter the wrapper's sticky geometry. This works when the header content can fit or overflow within the reserved space. If you can't do that, consider transforming the visual content instead of changing layout size. Transforms affect how an element is drawn without changing its layout footprint in the same way. Either way, test around the exact sticky threshold - smooth animations elsewhere won't guarantee the boundary stays stable.
Apply snapped to the snap target
For scroll snapping, set up the query on the element that is snapped, not just on the scrolling container. Configure the scroll container and its snap targets first:
html {
scroll-snap-type: y mandatory;
}
.slide {
min-height: 100vh;
scroll-snap-align: start;
scroll-snap-stop: always;
container-type: scroll-state;
container-name: slide-state;
}
Then apply descendant styles when the slide snaps vertically:
@container slide-state scroll-state(snapped: y) {
.slide-image {
scale: 1.05;
transition: scale 300ms ease;
}
}
Again, the container is the slide, so the conditional styling targets elements inside it. Also remember that the document-level scrolling element is usually html, not body. Put scrolling-related styles on the element that actually owns the scroll behavior, and verify in the layout you ship.
What are the limits of scroll-state() in production?
A good way to think about scroll-state() is as a state query, not a general scroll-control system. It's great for small, declarative "when X state is true, change Y styles" reactions. But it doesn't expose a numeric scroll offset, compute progress, or replace app logic that depends on a sequence of scroll events.
Choose CSS or JavaScript by the requirement
CSS is a good fit when you can map behavior directly from state to style. For example:
- sticky header is stuck → change a descendant's look
- a snap target is active → adjust an element's appearance
- more content is scrollable → show an affordance
JavaScript is still the right tool when you need to:
- trigger at an exact pixel or percentage threshold,
- coordinate scroll state with application data or analytics,
- run more complex logic across multiple scroll containers,
- support environments where the required CSS feature isn't available,
- control focus, announcements, or other interaction behaviors CSS alone can't provide.
This isn't an argument for "avoid JavaScript." The point is to put each responsibility in the layer that can express it most clearly. Swapping a simple event listener for CSS can reduce code and synchronization work. But forcing complex behavior into CSS can also make the mental model harder to follow.
Build a resilient default
Browser support for new CSS features changes over time, and the browsers you test in aren't always the browsers your users run. Before you rely on a scroll-state() query for navigation or access to content, check current support for your audience.
When possible, ship a functional default and treat scroll-state() as an enhancement.
A production review should check:
- the right element has
container-type: scroll-state, - the query uses the correct named container and feature/value pair,
- the styled element is actually a descendant of that container,
- logical directions behave correctly for the writing modes you support,
- the default state still works without the conditional styling,
- sticky size changes won't shift the sticky threshold and cause flicker,
- reduced-motion preferences are respected when you add transitions.
That last one is easy to overlook. If the only way to understand or operate the page is through motion on a decorative element, you've probably gone too far. Keep the static layout clear, and use motion as feedback - not as the sole signal.
Conclusion
CSS scroll-state() queries give you a straightforward way to tie a small set of scroll states to visual updates. They work well for directional controls, sticky-header styling, and snap-target effects - assuming you configure the correct element as the container and target its descendants.
The real decision is simple: do you need a state, or do you need a measurement? If your UI just needs to know whether something is stuck or whether more content is available, a CSS query can keep things declarative. If you need exact scroll progress, thresholds, or application logic, use the tool built for that job.