Container Style Queries and Scroll-State Queries
Container queries beyond size: style() queries that react to an ancestor's custom properties, including range comparisons, and scroll-state() queries for stuck headers, snapped items and scrollers that can scroll further, with fallbacks.
kvi5hnuCode Sprout team member7 min readOn this page
Size container queries (article 78) ask how big a container is. Two newer kinds ask different questions:
- Style queries: what value does a custom property have on the container?
- Scroll-state queries: is the container stuck, snapped, scrollable, or being scrolled?
Both use @container, and both style the container's descendants, never the container itself.
Style queries
.card-list { --layout: compact; }
@container style(--layout: compact) {
.card { padding: 0.5rem; font-size: 0.875rem; }
}
Every element is a style container by default (container-type: normal allows style queries), so no container-type is needed. An unnamed style query checks the parent element's computed value; a named one (@container sidebar style(…)) checks the named ancestor.
Since custom properties inherit, the parent usually has the same value as wherever it was set further up, so in practice you set a property on a section and every component inside can respond.
What can be queried
Tested in Chrome 157:
| Query | Result |
|---|---|
style(--theme: dark) | true when the computed value is dark |
style(--theme) | true when the property has any value other than its initial one |
not style(--theme: light) | negation, and and / or work as in other queries |
style(--size > 10) | range comparison of a custom property: Chrome 142, Firefox 151 |
style(color: red) | false: only custom properties can be queried so far |
Values are compared as computed values. For an unregistered property that means the tokens as written, so --gap: 1rem doesn't equal 16px. Registering the property with @property and a syntax like <length> (article 27) makes values compare by their computed meaning.

The HTML and the component's CSS are identical in all four panels; only a custom property set on the surrounding list differs, and the last panel shows two style queries combined with and.
Why use them
Style queries let a parent set a variant that changes several things at once in its children, without classes on every child:
.section { --tone: default; }
.section.is-inverted { --tone: inverted; }
@container style(--tone: inverted) {
.card { background: #1f2933; color: #f5f7fa; }
.button { border-color: currentColor; }
.icon { filter: invert(1); }
}
The child components don't need to know which class the section used, only the property's value. For choosing a single value from a property, the if() function (article 30) is shorter; style queries are better when a whole set of rules changes.
body { font: 0.85rem system-ui, sans-serif; margin: 1rem; }
fieldset { border: 1px solid #d8cfc0; border-radius: 6px; display: flex; gap: 0.6rem; flex-wrap: wrap; }
.section { --tone: default; margin-top: 0.75rem; }
.card { padding: 0.75rem; border-radius: 8px; background: #f4efe6; color: #2a2118; }
.card h3 { margin: 0 0 0.25rem; }
.card p { margin: 0 0 0.5rem; }
.card button { font: inherit; padding: 0.3rem 0.8rem; border-radius: 6px; border: 2px solid #8a3b12; background: transparent; color: inherit; }
@container style(--tone: inverted) {
.card { background: #1f2933; color: #f5f7fa; }
.card button { border-color: currentColor; }
}
@container style(--tone: warning) {
.card { background: #fff4d6; border-inline-start: 6px solid #b7791f; }
.card h3::before { content: "⚠ "; }
}
Only the section's --tone changes; the card's styles come entirely from the queries.
Scroll-state queries
A scroll-state container knows about its own scrolling state, and its descendants can be styled by it:
.site-header { position: sticky; inset-block-start: 0; container-type: scroll-state; }
@container scroll-state(stuck: top) {
.site-header > .bar { box-shadow: 0 2px 12px rgb(0 0 0 / 0.2); background: white; }
}
There are four states, all in Chromium browsers (Chrome 133+, scrolled 144+):
| Query | Container | True when |
|---|---|---|
stuck: top (or bottom, left, right, block-start, inline-end …) | a position: sticky element | it's currently stuck to that edge |
snapped: x (or y, block, inline, both) | a scroll snap target (article 115) | it's the currently snapped item on that axis |
scrollable: top (or another edge, or an axis) | a scroll container | it can scroll further in that direction |
scrolled: bottom (or another edge) | a scroll container | the most recent scroll was in that direction |
Tested in Chrome 157:
- A sticky element with
container-type: scroll-statematchedstuck: toponce its scroller had scrolled past it, one frame after the scroll. - In a mandatory snap carousel, only the first, snapped item matched
snapped: x. - A page that could scroll down matched
scrollable: bottomat the top. scrolledfollowed relative scrolls: afterscrollBy(0, 300)it matchedscrolled: bottom, and afterscrollBy(0, -100),scrolled: top. Jumping withscrollTo()didn't change it.

No script runs in these panels: the header's look and the hint's visibility come only from @container scroll-state(…) rules reacting to each scroller's position.
Typical uses:
- Shadows on stuck headers, without JavaScript scroll listeners.
- Highlighting the snapped slide in a carousel, and dimming the others.
- Scroll hints: fade the edge of a scroller only where there's more to scroll.
- Hiding a header when scrolling down, and showing it again when scrolling up, with
scrolled.
html { container-type: scroll-state; }
@container scroll-state(scrolled: bottom) {
.site-header { translate: 0 -100%; } /* hide while scrolling down */
}
@media (prefers-reduced-motion: no-preference) {
.site-header { transition: translate 0.25s; }
}
container-type can combine values: container-type: scroll-state inline-size makes one element both a size and a scroll-state container.
Fallbacks
Firefox and Safari don't support scroll-state queries yet. Treat them as enhancements: the base design must work without them, with the header readable without its shadow, and the carousel usable without the highlight. To change other styles depending on support, use @supports (container-type: scroll-state) (article 80).
Other container query types
@container name { }with only a name and no condition matches when an ancestor with that name exists: Chrome 148, Firefox 149, Safari 26.4. Tested in Chrome: it applied inside acontainer-name: sidebarelement and not outside.- Anchor queries: with
container-type: anchoredon an anchor-positioned element,@container anchored(fallback: …)styles its descendants depending on which position fallback is in use. Chrome 143 only (article 59).
What's current
- Style queries for custom properties: Chrome 111, Safari 18, Firefox 151, so in all current browsers.
- Range syntax in style queries: Chrome 142 and Firefox 151, not yet Safari.
- Scroll-state queries: Chromium only (Chrome 133;
scrolled144). - Queries on standard properties, like
style(display: flex), are specified but not supported anywhere.
Common questions
Do style queries need container-type?
No. Every element is a style container. You only need container-name to query a specific ancestor.
Why doesn't style(color: red) match?
Browsers only support style queries on custom properties.
Why doesn't my style query see the property I set on the same element? Queries style descendants, and an unnamed one checks the parent. Set the property on an ancestor.
Can I use scroll-state queries in production? As progressive enhancement, yes; they're ignored in browsers that don't support them.
Summary
@container style(--prop: value)styles descendants based on an ancestor's custom property; nocontainer-typeneeded.style(--prop)tests for any value, and range comparisons likestyle(--n > 10)work in Chrome and Firefox.- Only custom properties can be queried; register them with
@propertyfor typed comparison. container-type: scroll-stateenablesstuck,snapped,scrollableandscrolledqueries, in Chromium.- Use them as enhancements, with a base design that works without them.
Practice questions
Write CSS so that buttons inside any element with --density: compact get smaller padding.
Answer
@container style(--density: compact) {
button { padding: 0.25rem 0.5rem; }
}
No container-type is needed. Because --density inherits, buttons at any depth inside that element match.
Why doesn't this rule make the header's shadow appear when it sticks?
.header { position: sticky; top: 0; }
@container scroll-state(stuck: top) { .header { box-shadow: 0 2px 8px #0003; } }
Answer
Two problems. The header isn't a scroll-state container, so it needs container-type: scroll-state. And a query can't style the container itself, so the shadow must go on a child, such as an inner .bar element.
A carousel should enlarge the currently snapped slide. What does each slide need, and what's the query?
Answer
Each slide must be a snap target with container-type: scroll-state. Style a child of the slide with @container scroll-state(snapped: inline) { .slide-inner { scale: 1.05; } }.
--gap: 1rem is set on a section. Does @container style(--gap: 16px) match inside it?
Answer
Not if --gap is unregistered: its computed value is the token 1rem, which doesn't equal 16px. Registering --gap with @property and syntax: '<length>' computes it to an absolute length, so it would match.
Previous: Container Queries: Size Containers, Container Names and Container Units
Next: Feature Queries in Depth: @supports, selector() and font-tech()

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.