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 in loco.css, keyed on a data-stuck attribute whose value lists the pinned edges (data-stuck="top").
  • The loco-sticky Stimulus controller, which stamps that attribute while the sticky offset is actually displacing the element. A top-0 bar 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 the sticky class 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

NameQ1Q2Q3Q4Q5Q6Q7Q8
Ada829507192163758
Grace437123058861745
Alan7816518624599432
Barbara165834587327419
Edsger513524535546
Frances864546019753490
Donald2487531982481477
Margaret593257548219164
Ken9474543414917151
Radia321969077645138
Linus6761554943373125
Guido56789101112

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 element
The offset is yours

sticky: 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.

VariantMatchesExample
stuck: The stuck element and anything inside itpy-4 stuck:py-1 on the navbar, h-8 stuck:h-6 on its logo
stuck-top: (bottom, left, right) One pinned edge onlystuck-left:border-r on a pinned column
group-stuck/name: Something inside a stuck group/name elementgroup-stuck/nav:hidden on a tagline
peer-stuck: A later sibling of a stuck peerpeer-stuck:mt-12
group-has-[.navbar[data-stuck]]/page: Anything in a group/page wrapper that holds a stuck navbartop-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:

Layerz-indexWhy
Page navbarz-30 Above everything that scrolls in the page
Section headingsz-20 Below the navbar, above content and inner scrollers
Table header rowz-[2] Above pinned body cells as they pass under it
Pinned body cellsz-[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.

Known limits

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.

Made with by Profoundry .
Copyright © 2023-2026 Profoundry LLC.
All rights reserved.