Container styles and scroll-state queries
Definition and purpose
Not all container queries measure size. Style queries can inspect custom property values and scroll-state queries can react to scrolling conditions such as a sticky container being stuck.
Density token
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; } #a, #b { background: lightblue; } #p { --density: compact; } #a { padding: 12px; } @container style(--density: compact) { #a { padding: 4px; } } </style> </head> <body> <div id="p"> <div id="a">A</div> <div id="b">B</div> </div> </body></html>The inherited token on the ancestor selects compact padding in Chromium.
Syntax and mental model
A custom property such as --density: compact is a named CSS value inherited by default. @container style(--density: compact) can style descendants without size containment. A scroll-state query uses an eligible ancestor with container-type: scroll-state, for example @container scroll-state(scrollable: bottom). This queries possible user scrolling, not a numeric offset. Keep a base style outside the query because support varies.
Scroll enhancement with base
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; } #a, #b { background: lightblue; } #p { height: 50px; overflow-y: auto; container-type: scroll-state; } #a { height: 40px; color: navy; } #b { height: 100px; } @container scroll-state(scrollable: bottom) { #a { color: green; } } </style> </head> <body> <div id="p"> <div id="a">A</div> <div id="b">B</div> </div> </body></html>The scroll container and readable base text work independently of the optional color cue. The supplementary test checks the live cue after scrolling.
Common mistakes
Do not assume arbitrary regular CSS properties are interoperably queryable with style(). Do not make essential instructions available only through a scroll-state enhancement.
Practice and next steps
After trying the examples, complete the linked coding exercise and review questions. Predict the result before running your code. Prerequisite: Container size queries. Next lesson: Adaptive layout project.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Container styles and scroll-state queries — knowledge check
Five questions on container styles and scroll-state queries.
What does a custom-property style query inspect?
Which type enables scroll-state queries?
What does scrollable: bottom describe?
Should essential content depend on experimental queries?
Does a style query require inline-size containment?