Sticky Elements
CSS can pin an element with position: sticky, but it has no state for
"is pinned right now", so compacting navbars and shadowed headers usually
get rebuilt per app with a scroll listener. LocoMotion ships the three
pieces instead:
stuck:variants inloco.css, keyed on adata-stuckattribute whose value lists the pinned edges (data-stuck="top").- The
loco-stickyStimulus controller, which stamps that attribute while the sticky offset is actually displacing the element. Atop-0bar resting at the top of the page is not stuck until the content under it scrolls. - A
sticky:option on the components that pin, which adds thestickyclass and wires the controller for you.
Live 1. All of it together
Scroll the pane. The navbar compacts from 4rem to 3rem once it pins; each section heading pins below it and slides up to track the shorter bar; and the roster table pins its header row and first column inside its own scroller.
Overview
Overview paragraph 1. Keep scrolling: the heading above stays pinned under the navbar until the next section pushes it away.
Overview paragraph 2. Keep scrolling: the heading above stays pinned under the navbar until the next section pushes it away.
Overview paragraph 3. Keep scrolling: the heading above stays pinned under the navbar until the next section pushes it away.
Overview paragraph 4. Keep scrolling: the heading above stays pinned under the navbar until the next section pushes it away.
Overview paragraph 5. Keep scrolling: the heading above stays pinned under the navbar until the next section pushes it away.
Overview paragraph 6. Keep scrolling: the heading above stays pinned under the navbar until the next section pushes it away.
Roster
| Name | Q1 | Q2 | Q3 | Q4 | Q5 | Q6 | Q7 | Q8 |
|---|---|---|---|---|---|---|---|---|
| Ada | 8 | 29 | 50 | 71 | 92 | 16 | 37 | 58 |
| Grace | 43 | 71 | 2 | 30 | 58 | 86 | 17 | 45 |
| Alan | 78 | 16 | 51 | 86 | 24 | 59 | 94 | 32 |
| Barbara | 16 | 58 | 3 | 45 | 87 | 32 | 74 | 19 |
| Edsger | 51 | 3 | 52 | 4 | 53 | 5 | 54 | 6 |
| Frances | 86 | 45 | 4 | 60 | 19 | 75 | 34 | 90 |
| Donald | 24 | 87 | 53 | 19 | 82 | 48 | 14 | 77 |
| Margaret | 59 | 32 | 5 | 75 | 48 | 21 | 91 | 64 |
| Ken | 94 | 74 | 54 | 34 | 14 | 91 | 71 | 51 |
| Radia | 32 | 19 | 6 | 90 | 77 | 64 | 51 | 38 |
| Linus | 67 | 61 | 55 | 49 | 43 | 37 | 31 | 25 |
| Guido | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
Notes
Notes paragraph 1. Keep scrolling: the heading above stays pinned under the navbar until the next section pushes it away.
Notes paragraph 2. Keep scrolling: the heading above stays pinned under the navbar until the next section pushes it away.
Notes paragraph 3. Keep scrolling: the heading above stays pinned under the navbar until the next section pushes it away.
Notes paragraph 4. Keep scrolling: the heading above stays pinned under the navbar until the next section pushes it away.
Notes paragraph 5. Keep scrolling: the heading above stays pinned under the navbar until the next section pushes it away.
Notes paragraph 6. Keep scrolling: the heading above stays pinned under the navbar until the next section pushes it away.
Notes paragraph 7. Keep scrolling: the heading above stays pinned under the navbar until the next section pushes it away.
Notes paragraph 8. Keep scrolling: the heading above stays pinned under the navbar until the next section pushes it away.
Setup 2. Pin it with sticky:
Pass the edge to pin to — "top" (or true), "bottom", "left",
"right", or several for a corner cell. The component gets the sticky
class and the loco-sticky controller. Today that means the navbar, a
table head, and table cells; for anything else, add the class and the
controller yourself.
= daisy_navbar(sticky: "top", css: "top-0 z-30 h-16 stuck:h-12")
= daisy_table do |table|
- table.with_head(sticky: "top", row_css: "top-0 z-[2] bg-base-100")
- table.with_row do |row|
- row.with_column(sticky: "left", css: "left-0 z-[1] bg-base-100")
%h3.sticky.top-16{ data: { controller: "loco-sticky" } } Any elementsticky: never emits top-0 or left-0. Tailwind resolves competing
utilities by stylesheet order, not by the order you wrote them, so an
emitted top-0 could silently beat your top-16. Always put the offset
in css: (or row_css: for a table head). The controller never needs
it: it compares the element's position with where it would sit in flow,
so offsets that change with stuck variants just work.
Styling 3. Style the pinned state
The variants come from loco.css, which your Tailwind build already
imports.
| Variant | Matches | Example |
|---|---|---|
stuck:
| The stuck element and anything inside it | py-4 stuck:py-1 on the navbar, h-8 stuck:h-6 on its logo
|
stuck-top: (bottom, left, right)
| One pinned edge only | stuck-left:border-r on a pinned column
|
group-stuck/name:
| Something inside a stuck group/name element | group-stuck/nav:hidden on a tagline
|
peer-stuck:
| A later sibling of a stuck peer | peer-stuck:mt-12
|
group-has-[.navbar[data-stuck]]/page:
| Anything in a group/page wrapper that holds a stuck navbar | top-16 group-has-[.navbar[data-stuck]]/page:top-12
|
Group variants only reach descendants, so a heading elsewhere on the
page cannot use group-stuck/nav: to track the navbar. Put group/page
on a shared ancestor and key on the stuck navbar inside it, exactly as the
demo's headings do. Add transition-[height] or transition-all to the
changing elements so they ease instead of jump.
Convention 4. Layer and back them
Pinned elements overlap whatever scrolls under them, so give each layer a
z-index and a background:
| Layer | z-index | Why |
|---|---|---|
| Page navbar | z-30
| Above everything that scrolls in the page |
| Section headings | z-20
| Below the navbar, above content and inner scrollers |
| Table header row | z-[2]
| Above pinned body cells as they pass under it |
| Pinned body cells | z-[1]
| Above the cells that scroll under them |
Without a background (bg-base-100, bg-base-200), content shows
through the pinned element. Table layers are scoped to their own
scroller, so small values are enough there.
Tables 5. Tables pin rows and cells
with_head(sticky: "top") pins the header's <tr> and puts one
controller on it; the header cells style off it through the
descendant-matching stuck: variant. A sticky <tr> pins in current
Chromium, Firefox, and WebKit; older engines only pinned cells, so don't
rely on it for browsers you can't test.
Columns pin per cell: sticky: "left" on the header cell and every
body cell in the column, each with its own controller so stuck-left:
can draw a divider only while the column is pinned. On a table with
hundreds of rows, prefer the plain sticky left-0 classes and skip the
per-cell detection.
stuck: also matches descendants, so it crosses nested sticky regions: a
pinned heading inside a pinned panel matches both. Reach for
stuck-top: or a named group there. While a parent pushes a sticky
element out, it keeps its stuck styling until it leaves.