Skip to content
Esc
↑↓navigate↵open⌘Jpreview
On this page

Horizontal Scroll for Astro

Move content sideways as the user scrolls down the page.

Installation

bunx --bun shadcn@latest add @gnostic/horizontal-scroll

Usage

---
import { HorizontalScroll } from "@/components/ui/horizontal-scroll"
---

<HorizontalScroll role="region" aria-label="Project stages" top={80}>
  <article class="w-80 max-w-full">Plan the work</article>
  <article class="w-96 max-w-full">Build the first version</article>
  <article class="w-80 max-w-full">Share the result</article>
</HorizontalScroll>

Supply your own content in the default slot. Each direct child is an item in the row. Set its width with classes. Items can have different widths. Use max-w-full to keep items within the page width in the vertical layout.

This is a UI effect. It supplies scroll behavior and accepts any content. A block can compose it with headings, cards, and other section content. Use ScrollArea for a box with native scrolling, or Carousel for slides with controls.

Flex-basis sizing

You can also size the direct children with flex-basis utilities, as you would with Carousel. Use basis-full for one item per view, basis-1/2 for two, or basis-1/3 for three. Responsive utilities can change that count.

Percentage bases use the track’s inner width, excluding its padding. A separate gap adds to those widths. For exact divisions, use trackClass="gap-0" and put spacing inside each item wrapper:

<HorizontalScroll trackClass="gap-0">
  {["Planning", "Design", "Build", "Support"].map((title) => (
    <article class="w-full min-w-0 basis-full p-3 md:basis-1/2 lg:basis-1/3">
      <h3>{title}</h3>
      <p>Details for this service.</p>
    </article>
  ))}
</HorizontalScroll>

Use fixed widths when the cards need different sizes. Flex-basis is another option; it does not change the scroll effect or its vertical fallback.

Image cards

Put an image above the card heading, description, and service details. This example uses the reusable Horizontal Scroll features block. The component and block pages render the same block and sample content. Install @gnostic/features-horizontal-scroll to use this complete section.

Page scrolling

The component measures the row and adds the required vertical space. Its viewport stays in place while the row moves sideways. At the end of the row, the next page section comes into view. Scrolling up reverses the effect.

The page keeps its normal scroll controls. Mouse wheels, touch gestures, keyboard scrolling, and the page scrollbar all use the same scroll position. The component does not cancel wheel or touch events. There are no inner scrollbars or animation dependencies.

Set top to leave space for a fixed header. The default speed={1} moves one horizontal pixel for each vertical scroll pixel. A value of 2 halves the vertical distance needed to pass through the row.

The example uses a separate preview document so its page can scroll. It sets minWidth={0} to show the effect at each preview width. The component default is 768 pixels.

Layout and access

The content uses a vertical layout when JavaScript is unavailable, reduced motion is selected, the browser is below minWidth, or enabled={false}. It also uses this layout when the row fits without horizontal movement or the content is too tall for the viewport. Print output uses the vertical layout.

Keyboard focus brings an item into view. Links to IDs within the row also bring their target into view. The factory measures content again after size changes. The component initializes on script load and astro:page-load, then destroys its controller on astro:before-swap. If your code adds or removes an instance directly, call createHorizontalScroll(root) to attach it and controller.destroy() before removing it. Import the factory from the component index.

Use this component in the page document, outside nested scroll areas. Sticky positioning needs ancestors without overflow: auto, scroll, or hidden. Use overflow: clip when an ancestor must clip content. Do not nest horizontal scroll effects.

The root accepts native attributes, classes, and styles. Keep its height, vertical padding, and borders free for the scroll calculation. Put section spacing on an outer wrapper. viewportClass can set the pinned surface color. trackClass sets row spacing and padding:

<HorizontalScroll
  top={64}
  speed={1.5}
  viewportClass="bg-muted/40"
  trackClass="gap-8 px-6 py-10"
>
  <slot />
</HorizontalScroll>

Colors and fonts come from your content and the selected project preset.

API Reference

PropType
enabled?boolean

Enable the effect when the viewport and motion settings permit it.

Typeboolean
Defaulttrue
speed?number

Horizontal pixels moved per vertical scroll pixel. Must be positive.

Typenumber
Default1
top?number

Space above the sticky viewport, in pixels.

Typenumber
Default0
minWidth?number

Minimum browser width for the effect, in pixels.

Typenumber
Default768
viewportClass?string

Classes for the sticky viewport, such as its background color.

Typestring
trackClass?string

Classes for the content row, such as gap and padding.

Typestring

Native attributes such as id, class, role, and aria-label are supported. Read the installed source.