# Using CSS @container scroll-state() for sticky headers and scroll effects

<https://ics.media/en/entry/250602/>

Author: ICS Editorial Team

Published: 2026-05-15 / Updated: 2026-11-18

`@container scroll-state()` is a type of CSS container query. It lets you change a header's style after scrolling using only CSS, where JavaScript was previously required.

This article introduces how to use `scroll-state()` and what it can do through a series of demos.

Container queries are introduced in “[コンテナークエリーの使い方](https://ics.media/entry/240617/)”. They are useful not only for controlling layouts based on container width, but also for building interactive UIs. This article should give you a sense of the broader possibilities of container queries.

<small>View the demos in this article in Chrome and Edge 144 or later.</small>

### How to use `@container scroll-state()`

This query can be used on **descendant elements** of an element with `container-type: scroll-state`. The following values can be specified inside the parentheses of `@container scroll-state()`.

- `stuck`
  - The element is stuck with `position: sticky`
- `scrollable`
  - The element can be scrolled in one of the four directions
- `snapped`
  - The element snaps into place with the `scroll-snap-type` property
- `scrolled`
  - The most recent scroll direction can be detected

After the colon (`:`), specify the edge, direction, or axis to detect.

Example of `stuck`

```html
<div class="item-container">
  <div class="item"></div>
</div>
```

```css
.item-container {
  container-type: scroll-state;
  position: sticky;
  top: 0;
  left: 0;
}

.item {
  @container scroll-state(stuck: top) {
    /* Write styles for when .item-container is stuck */
  }
}
```

The following sections introduce UIs you can build with each scroll state.

### stuck: detect sticky positioning

With `@container scroll-state(stuck: top)`, styles can be applied when an element becomes stuck near the top of the viewport with `position: sticky`. In the following demo, the header background changes to white when the header becomes stuck `20px` from the top of the viewport.

<https://ics-creative.github.io/250526_scroll-state/stuck-header/>

- [Open the demo in a new window](https://ics-creative.github.io/250526_scroll-state/stuck-header/)
- [View the source code](https://github.com/ics-creative/250526_scroll-state/blob/main/stuck-header/index.html)

#### Reveal content when stuck

In the following demo, the button expands to reveal navigation links when it reaches the top of the viewport.

<https://ics-creative.github.io/250526_scroll-state/stuck-navigation/>

- [Open the demo in a new window](https://ics-creative.github.io/250526_scroll-state/stuck-navigation/)
- [View the source code](https://github.com/ics-creative/250526_scroll-state/blob/main/stuck-navigation/index.html)

#### Apply styles when sticking ends

Styles can be applied when `position: sticky` stops sticking. Use the following scroll state queries to detect that moment.

- `@container scroll-state(stuck: bottom)`
- A `not` condition (`@container not scroll-state(stuck: bottom)`)

In the following demo, a button in “Section 01” sticks to the bottom of the viewport. Its style changes after you scroll past it.

<https://ics-creative.github.io/250526_scroll-state/stuck-section-button/>

- [Open the demo in a new window](https://ics-creative.github.io/250526_scroll-state/stuck-section-button/)
- [View the source code](https://github.com/ics-creative/250526_scroll-state/blob/main/stuck-section-button/index.html)

### scrollable: detect scrollability

With `@container scroll-state(scrollable: top)`, styles can be applied when an element can be scrolled upward. In the following demo, a “Back to top” button slides in from the top edge when you scroll down the list. It disappears when you return to the top.

<https://ics-creative.github.io/250526_scroll-state/scrollable-hint/>

- [Open the demo in a new window](https://ics-creative.github.io/250526_scroll-state/scrollable-hint/)
- [View the source code](https://github.com/ics-creative/250526_scroll-state/blob/main/scrollable-hint/index.html)

#### Show arrows for available scroll directions

`scrollable` also accepts logical directions such as `inline-start` and `inline-end`. In a horizontal layout with text flowing from left to right, `inline-start` means left and `inline-end` means right.

The following demo shows arrows only in the directions the row of tabs can scroll. The right arrow disappears at the right edge, and the left arrow disappears when you return to the left edge.

<https://ics-creative.github.io/250526_scroll-state/scrollable-tabs/>

- [Open the demo in a new window](https://ics-creative.github.io/250526_scroll-state/scrollable-tabs/)
- [View the source code](https://github.com/ics-creative/250526_scroll-state/blob/main/scrollable-tabs/index.html)

The scrollable `.tabs` element is a container named `tabs`. Its scroll state controls the visibility of the arrow images inside it. The following excerpt shows the relevant CSS.

```css
.tabs {
  container: tabs / scroll-state;
  display: flex;
  overflow-x: auto;

  .previous,
  .next {
    display: none;
  }

  @container tabs scroll-state(scrollable: inline-start) {
    .previous {
      display: block;
    }
  }

  @container tabs scroll-state(scrollable: inline-end) {
    .next {
      display: block;
    }
  }
}
```

The `::scroll-button()` pseudo-element handles scrolling when an arrow is clicked. See “[Creating a carousel UI with HTML and CSS without JavaScript](https://ics.media/en/entry/250516/)” for details.

#### Show scroll hints in a table

The following demo is a table that scrolls horizontally and vertically. It uses scroll state queries to show arrows in the directions where more content is available. The arrows are hidden when you reach the right or bottom edge.

<https://ics-creative.github.io/250526_scroll-state/scrollable-table/>

- [Open the demo in a new window](https://ics-creative.github.io/250526_scroll-state/scrollable-table/)
- [View the source code](https://github.com/ics-creative/250526_scroll-state/blob/main/scrollable-table/index.html)

The table's header row and columns use `position: sticky`. See “[CSS single-axis scroll containers for horizontal tables with sticky headers](https://ics.media/en/entry/261104/)” for details.

### snapped: detect scroll snapping

With `@container scroll-state(snapped: block)`, styles apply when the target is snapped, or is about to snap, along the block axis. Set `scroll-snap-type` on the scroll container. In horizontal writing, the block axis is vertical. The following demo slightly enlarges the element with a spring effect to make it stand out.

<https://ics-creative.github.io/250526_scroll-state/snapped-vertical/>

- [Open the demo in a new window](https://ics-creative.github.io/250526_scroll-state/snapped-vertical/)
- [View the source code](https://github.com/ics-creative/250526_scroll-state/blob/main/snapped-vertical/index.html)

#### Apply to a carousel UI

The following demo applies this feature to the carousel UI introduced in “[Creating a carousel UI with HTML and CSS without JavaScript](https://ics.media/en/entry/250516/)”. When the “▶” button is clicked, the photos slide horizontally, and an effect reduces their size only while they are sliding. The value passed to `snapped` is `inline`, which refers to the inline direction.

<https://ics-creative.github.io/250526_scroll-state/snapped-carousel/>

- [Open the demo in a new window](https://ics-creative.github.io/250526_scroll-state/snapped-carousel/)
- [View the source code](https://github.com/ics-creative/250526_scroll-state/blob/main/snapped-carousel/index.html)

Excerpt from the added CSS

```css
.item {
  container-type: scroll-state;
  img {
    transition: scale 0.3s;
    transition-delay: 0.2s;

    @container not scroll-state(snapped: inline) {
      scale: 0.85;
      transition-delay: 0s;
    }
  }
}
```

### scrolled: detect scroll direction

With `@container scroll-state(scrolled: bottom)`, styles can be applied when the container was most recently scrolled downward. Use `top` to detect a recent upward scroll.

In the following demo, the header hides when you scroll down and reappears when you scroll up.

<https://ics-creative.github.io/250526_scroll-state/scrolled-header/>

- [Open the demo in a new window](https://ics-creative.github.io/250526_scroll-state/scrolled-header/)
- [View the source code](https://github.com/ics-creative/250526_scroll-state/blob/main/scrolled-header/index.html)

The `scrolled` and `scrollable` conditions are combined with `and`. The header hides only after scrolling down, while the page can still scroll back up. It stays visible at the top of the page.

```css
html {
  --duration-header: 0.3s;

  container-type: scroll-state;
}

.site-header {
  position: sticky;
  top: 0;
  translate: 0 0;
  transition: translate var(--duration-header) ease;

  /* Keep the header visible at the top and hide it only when scrolling down. */
  @container scroll-state((scrolled: bottom) and (scrollable: top)) {
    translate: 0 -100%;
  }

  /* Reveal the header when a hidden link receives keyboard focus. */
  &:focus-within {
    translate: 0 0;
  }
}
```

When scrolling upward makes the condition false, the header returns to its default `translate: 0 0`. The `:focus-within` pseudo-class also reveals the header when a link inside it receives keyboard focus.

### Browser support

For `scroll-state()`, `stuck`, `scrollable`, and `snapped` are available in Chrome and Edge 133 or later (February 2025). `scrolled` requires Chrome and Edge 144 or later (January 2026).

<https://webstatus.dev/features/container-scroll-state-queries>

Reference: [Can I use...](https://caniuse.com/mdn-css_at-rules_container_scroll-state_queries)

### Conclusion

Scrolling is one of the most basic ways people interact with websites. Responding to scroll actions can make a site more pleasant to use.

Browser support for scroll state queries is still limited, but they can be useful in many situations. Try them when you want to add CSS effects based on scroll state.
