Skip to main content

Modernizing Web Interfaces with CSS scroll-state() Container Queries

Web developers have long relied on JavaScript event listeners, IntersectionObserver instances, and complex scroll handlers to respond to user scrolling behavior. Common interface patterns—such as revealing back-to-top buttons, shrinking sticky headers, displaying scroll directional cues, or triggering animations when sections snap into view—traditionally required scripting to monitor DOM positions and toggling CSS classes dynamically.

The introduction of the CSS scroll-state() query offers a declarative, CSS-native approach to state-driven styling. Despite what its name might casually suggest to those unfamiliar with emerging web standards, scroll-state() is not designed to measure user mood or engagement. Instead, it provides a native mechanism to conditionally apply styles based directly on container scroll states. By moving these responsive interfaces from JavaScript into stylesheets, developers can write cleaner code while allowing browser layout engines to handle scroll-driven UI updates efficiently.

Understanding the Syntax and Mechanics of scroll-state()

While the syntax of scroll-state() features the characteristic parentheses common to CSS functional expressions, it functions strictly as a container query feature under the @container at-rule, operating in a manner similar to style() queries.

In practice, a scroll-state() query allows a web page to query the browser about specific state conditions of a designated container element. If the specified feature and value pair evaluates to true, the browser applies the CSS rules declared inside the container query block. Conversely, if the container lacks the queried feature or if the feature’s current value does not match the query condition, the @container evaluation returns false, and the enclosed styles are ignored.

The fundamental CSS syntax follows the standard container query structure:

@container scroll-state( <scroll-state-feature>: <value> ) 
  /* styles */

By leveraging this query structure, layout engines can evaluate scroll metadata directly within the styling pipeline without needing round-trip execution through the main JavaScript thread.

The Four Core Scroll Features

The scroll-state() query specification centers around four distinct features, each evaluating a specific aspect of a container’s scrolling state or positional behavior.

scrolled

The scrolled feature checks whether a container has been scrolled in a designated direction. It allows styles to respond dynamically once scrolling has commenced along specified logical or physical axes, such as block-start, block-end, inline-start, or inline-end.

scrollable

The scrollable feature evaluates whether a container currently possesses overflow that allows scrolling in a given direction. Unlike simple overflow detection, this query dynamically updates based on whether additional scrollable content remains available in that direction.

stuck

The stuck feature checks whether an element with sticky positioning (position: sticky) is actively stuck against one of its positioning bounds, such as the top, bottom, left, or right edges of its scroll container.

snapped

The snapped feature determines whether a designated snap target element has currently locked into position along a particular axis (x or y) within a CSS Scroll Snap container.

Practical Implementations and Code Examples

To illustrate how scroll-state() replaces legacy scripting approaches, several practical interface scenarios demonstrate the syntax and behavior across different features.

Revealing UI Controls with scrolled

A widespread design pattern is the "Back to Top" interface control, which remains hidden while a user is at the top of a page and animates into view once downward scrolling begins.

Using scroll-state(), this behavior can be implemented purely in CSS by querying whether the scroll container has scrolled toward the block-end direction (downward in standard top-to-bottom writing modes):

.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, .toplink is initially positioned off-screen using translate: 0 200px. When the scroll container named top-revealer experiences scrolling toward block-end, the container query evaluates to true. The element updates its translation to translate: 0 0, bringing it smoothly into view via the specified CSS transition. If the user scrolls back to the very top, the query evaluates to false, and the element transitions back off-screen automatically.

Managing Dynamic Scroll Indicators with scrollable

Indicating scroll availability is essential in complex layouts, hero sections, or multi-language interfaces where writing modes or viewport sizes might make the presence of additional content less obvious. The scrollable feature simplifies rendering directional indicators only when actionable scroll space exists.

.scrollindicator 
  display: none;

  @container top-revealer scroll-state(scrollable: block-end) 
    display: block;
  

A key advantage of scrollable is its dynamic edge detection. When a user scrolls all the way to the end of the scrollable region, scrollable: block-end evaluates to false because no further scrolling is possible in that direction. As a result, the indicator hides automatically.

For more refined control, developers can combine multiple scroll conditions using logical operators, ensuring an indicator remains visible only when the container is scrollable and has not yet been scrolled extensively in either direction:

.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;
  

This combination prevents visual clutter by dismissing the indicator as soon as the user initiates scrolling in either direction while ensuring it reappears if the container layout resets.

Elevating Sticky Headers with stuck

Adapting navigation bars when they become pinned to the top of the viewport is another common design requirement. With scroll-state(), styles can respond directly when an element enters a stuck state.

Unlike queries on scroll containers, the stuck feature must be declared on the element that experiences the sticky behavior. Furthermore, because CSS container query rules prohibit a container from directly styling itself based on its own container state (even when combined with selectors like :has()), the implementation requires establishing a sticky wrapper element and querying that wrapper to style its children.

.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;

The container rule targets the child <header> element once .header-wrapper reaches its stuck threshold:

@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;
  

When the header wrapper sticks to top: 0, the child header element seamlessly shrinks its padding, reduces typography size, adjusts background opacity, and transforms its logo graphic.

Mitigating Layout Loops in Sticky Elements

A known challenge when modifying sticky elements dynamically is the potential creation of an infinite rendering loop, sometimes referred to as layout thrashing or stutter step.

This issue occurs when shrinking an element in response to it becoming stuck reduces the overall layout height of the sticky container. The reduction in height can inadvertently pull the container away from its sticky boundary, canceling the stuck condition. Once unstuck, the element expands back to its original size, which immediately causes it to reach the sticky threshold again. The browser enters a rapid cyclic feedback loop, toggling between stuck and unstuck states continuously.

To resolve this layout loop, developers can establish an explicit height on the sticky container (.header-wrapper). Setting this container height smaller than or equal to the final collapsed state of the header prevents DOM layout shifts from un-sticking the parent. If content overflow is restricted, an alternative solution is to apply visual size adjustments using CSS hardware-accelerated transformations (scale or translate) rather than modifying layout properties like padding, height, or font-size. Because transformed elements maintain their original layout bounds in the geometry calculation pipeline, the sticky layout boundary remains perfectly stable.

Enhancing Scroll Snap Views with snapped

The snapped feature brings container query capabilities to CSS Scroll Snap interfaces, enabling visual feedback when elements lock into alignment.

To use snapped, scroll snapping must first be established on the scrolling container. When using standard document scrolling, developers should note that scroll container properties apply to the <html> element rather than the <body> element.

html 
  scroll-snap-type: y mandatory;

The individual section elements serve as snap targets and host the scroll-state container definition:

.slide 
  height: 100vh;
  scroll-snap-align: start;
  scroll-snap-stop: always;
  container-type: scroll-state;
  container-name: getting-snappy;

When a slide snaps into focus along the vertical axis, container query rules apply changes to descendant elements within that slide:

@container getting-snappy scroll-state(snapped: y) 
  img 
    scale: 1.9;
    transition: 1s;
  

As with the stuck feature, styles inside the @container block cannot target the container element directly; they apply exclusively to child nodes within the snapped item.

Browser Support and Specification Status

The scroll-state() query represents an incremental addition to the CSS Container Queries suite. Web platform standardization bodies track implementation across browser engines through Baseline status metrics, ensuring developers can monitor cross-browser availability as implementation matures across major rendering engines.

Ammar Sabilarrohman

Author at DesignEnt.

Flash News