Skip to content
Tutorials Center
My learning

Topics

CSS Reference ยท Media Queries and Features

The scroll-state() container query

Reference for CSS scroll-state() container queries: style elements when they are stuck, snapped or scrollable, with syntax, examples and a support warning.

A scroll-state() query is a kind of container query that asks about the scroll situation of an ancestor instead of its size. With it you can style a sticky header differently once it is actually stuck, highlight the snapped item in a carousel, or show a "more below" hint only while a scroller still has content to reveal.

This feature has limited browser support. It works in some Chromium-based browsers and is not available everywhere. Check a current compatibility table before depending on it, and make sure the page still works when the query never matches.

CSS
.header {
  position: sticky;
  top: 0;
  container-type: scroll-state;
}

@container scroll-state(stuck: top) {
  .header-inner { box-shadow: 0 2px 8px rgb(0 0 0 / 0.3); }
}

Try it

Scroll the box below. In a browser that supports scroll-state queries, the sticky bar turns dark and shows "stuck" once it touches the top. Elsewhere it simply stays light, which is the intended fallback.

A sticky bar that reacts when it is stuck

Syntax and arguments

CSS
.container { container-type: scroll-state; }

@container [<name>] scroll-state(<feature>: <value>) { ... }

scroll-state(stuck: none | top | right | bottom | left | inset-block-start | ...)
scroll-state(snapped: none | x | y | block | inline)
scroll-state(scrollable: none | top | right | bottom | left | block-start | ...)
Feature Description
stuck Matches when the container is a position: sticky element that is currently stuck against the given edge.
snapped Matches when the container is a scroll-snap target that is currently snapped on the given axis.
scrollable Matches when the container is a scroll container that can scroll in the given direction.

Newer scroll-state features may exist or be added; look them up in a current specification before using them.

Usage notes

  • Descendants only. As with all container queries, the rules style the children of the container, never the container itself. Put the container on a wrapper and style the inner element, as in the example.
  • The container must be the right element. For stuck it is the sticky element itself; for snapped it is the snap target; for scrollable it is the scroll container.
  • container-type: scroll-state has to be set for the query to work. See container-type.
  • Provide a good default. Write the unstuck or unsnapped look as the normal rules, and use the query only as an enhancement.
  • Don't hide content with it. Information that exists only inside a query that may never match is lost for some visitors.

Browser support

Limited: available in some Chromium-based browsers only, so check a compatibility table and treat it as progressive enhancement.