@cssints/stuck
Style what is inside a sticky element while it is stuck, for cssints: sticky() makes an element
sticky and a scroll-state container, stuck(...) wraps styles of its descendants in
@container scroll-state(stuck: top) (Chrome 133). Where a target lacks scroll-state queries, stuck(...) also emits
the same styles under an attribute that a tiny IntersectionObserver sets, in the way of
bramus/sticky-observer.
import { cn, color, hover } from "cssints" with { type: "cssints" };
import { sticky, stuck } from "@cssints/stuck" with { type: "cssints" };
export const header = sticky().top("0"); // <header class={header}>
export const title = cn(color("gray"), stuck(color("red"))); // <h1 class={title}>
export const footer = sticky().bottom(2);
export const link = stuck("bottom")(color("green"), hover(color("black")));
@layer _.k {
._stuck-core {
position: sticky;
container-type: scroll-state;
}
}
@layer _.a {
._4k81c2 {
top: 0;
}
._kg6w97 {
bottom: 0.5rem;
}
}
@layer _.b {
@container scroll-state(stuck: top) {
._it4l7h {
color: red;
}
}
/* only when a browserslist target lacks scroll-state queries */
:where(._g-stuck[data-stuck~="top"]) ._fec935 {
color: red;
}
}
header is _stuck-core _g-stuck _4k81c2, title holds both _it4l7h and _fec935.
Browsers
- Native:
@container scroll-state()andcontainer-type: scroll-state, Chrome and Edge 133. Not in Firefox or Safari yet. - Fallback:
@cssints/stuck/observer(about 1.3 kB minified, no imports) setsdata-stuck="top"(a list of the stuck edges) on eachsticky()element while it is stuck, and the fallback rules read it. It checksCSS.supports("container-type", "scroll-state")first and does nothing where the browser has the queries. Nothing to import by hand: the runtime entry names it (script), so the module that usessticky()imports it throughvirtual:cssints/runtime/stuck.observerwhen a target lacks the queries; that module is empty when none does. - The fallback rules are in the sheet only when a browserslist target lacks the queries (
chrome 133alone gets the@containerrules only):stuck()askslacking()ofcssints/plugin, the same query and rules as the support warnings. The kernel's support warnings (itsrequires, the queries, and its declarationcontainer-type: scroll-state) are covered by the observer, so a build for the default targets prints none. - Checked in Chrome (native, and the observer with the queries cut out), Firefox and WebKit (the observer):
bun run test:browsers.
Rules
sticky()isposition: stickyandcontainer-type: scroll-state, with methods for the insets, each once, in any order:top(),right(),bottom(),left(), a<length-percentage>(a lone number is a spacing step, 0.25rem). Its class list also holds_g-stuck, the group marker the fallback rules read.stuck(...styles)applies the styles while the neareststicky()ancestor is stuck to its top edge;stuck(side)(...styles)totop,right,bottomorleft. It is a condition: it counts as one (layer_.b), wraps others (stuck(hover(...))) and goes inside them.- Descendants only. A scroll-state query matches the descendants of the container, never the container itself (the
spec's rule), so
stuck()styles go on an element inside the sticky one. To style the sticky box itself, put an inner wrapper in it. - Conflicts.
sticky()setspositionandcontainer-type: onecn()withposition,containerorcontainer-typeis a build error, derived from the kernel (written as styles):cssints: stuck: kernel core sets position, container-type (this style also sets position). - Errors: a bad inset is the grammar's, in the types and at build time (
sticky.top: unexpected "red" in "red", expected <length-percentage>), a method twice iscssints: sticky().top() is used twice, and an unknown side is a type error andcssints: stuck: stuck() takes top, right, bottom or left, got "middle". Each points at the call.
Limits
- The fallback reads the insets once, when it first sees the element: an inset that a media query changes later is not followed. A sticky element taller than its scroll container is not reported stuck. Shadow roots are not crossed.
- The fallback's root is the nearest ancestor whose
overflowis notvisible, else the viewport; the native query follows the real scroll container. - No logical sides (
block-start,inline-end), and noscrollableorsnappedscroll-state queries.
cd packages/stuck && bun run test # fixtures through the engine (output, targets, errors, observer size)
cd packages/stuck && bun run typecheck # the type test
cd packages/stuck && bun run test:browsers # Chrome, Firefox and WebKit through playwright-core
Credits
The fallback follows bramus/sticky-observer (MIT, Bramus Van Damme): an IntersectionObserver whose root is shrunk by the element's inset plus 1px. The observer is written here; no code is copied.