4.3 · Responsive components
Is it stuck? Scroll-state queries
Style sticky headers when they stick, and scrollers when they can scroll.
container-type: scroll-state lets descendants ask about
scrolling. scroll-state(stuck: top) matches while a
sticky element is stuck.
scroll-state(scrollable: top) matches when its scroller
can scroll back up.
These used to need a scroll listener or an
IntersectionObserver plus a class toggle. Now the
condition is part of the stylesheet.
new IntersectionObserver(([entry]) => {
header.classList.toggle(
'is-stuck',
entry.intersectionRatio < 1,
)
}, { threshold: [1] }).observe(sentinel)
.bar {
position: sticky;
container-type: scroll-state;
}
@container scroll-state(stuck: top) {
.bar-inner { box-shadow: …; }
}