Carousel for Astro
Embla carousel parts for Astro image and content sequences.
Installation
bunx --bun shadcn@latest add @gnostic/carouselnpx shadcn@latest add @gnostic/carouselpnpm dlx shadcn@latest add @gnostic/carouselInstall component packages
bun add embla-carouselnpm install embla-carouselpnpm add embla-carouselCopy the files
src/components/ui/carousel/carousel-content.astrosrc/components/ui/carousel/carousel-dots.astrosrc/components/ui/carousel/carousel-item.astrosrc/components/ui/carousel/carousel-layer.astrosrc/components/ui/carousel/carousel-next.astrosrc/components/ui/carousel/carousel-parallax.astrosrc/components/ui/carousel/carousel-previous.astrosrc/components/ui/carousel/carousel.astrosrc/components/ui/carousel/carousel.tssrc/components/ui/carousel/effects.tssrc/components/ui/carousel/index.tssrc/components/ui/carousel/types.ts
About
The carousel is built with Embla Carousel for motion and swipe. The Astro components follow the same composition as shadcn/ui.
Usage
---
import {
Carousel,
CarouselContent,
CarouselDots,
CarouselItem,
CarouselNext,
CarouselPrevious,
} from "@/components/ui/carousel"
---
<Carousel>
<CarouselContent>
<CarouselItem>...</CarouselItem>
<CarouselItem>...</CarouselItem>
<CarouselItem>...</CarouselItem>
</CarouselContent>
<CarouselPrevious />
<CarouselNext />
</Carousel>
Composition
Arrange the parts as shown for a Carousel:
Carousel
├── CarouselContent
│ ├── CarouselItem
│ └── CarouselItem
├── CarouselPrevious
└── CarouselNext
Examples
Sizes
To set the size of the items, use the basis utility classes on
<CarouselItem />.
<!-- 33% of the carousel width -->
<Carousel>
<CarouselContent>
<CarouselItem class="basis-1/3">...</CarouselItem>
<CarouselItem class="basis-1/3">...</CarouselItem>
<CarouselItem class="basis-1/3">...</CarouselItem>
</CarouselContent>
</Carousel>
<!-- 50% on small screens and 33% on larger screens -->
<Carousel>
<CarouselContent>
<CarouselItem class="md:basis-1/2 lg:basis-1/3">...</CarouselItem>
<CarouselItem class="md:basis-1/2 lg:basis-1/3">...</CarouselItem>
<CarouselItem class="md:basis-1/2 lg:basis-1/3">...</CarouselItem>
</CarouselContent>
</Carousel>
Spacing
Set gap on <CarouselContent /> to set the space between items. The default is
1rem. This prop sets --carousel-gap, which the content and items use for
their spacing.
<Carousel>
<CarouselContent gap="1.5rem">
<CarouselItem>...</CarouselItem>
<CarouselItem>...</CarouselItem>
<CarouselItem>...</CarouselItem>
</CarouselContent>
</Carousel>
Use a CSS value such as clamp() when the gap must change with the viewport:
<Carousel>
<CarouselContent gap="clamp(0.5rem, 2vw, 1rem)">
<CarouselItem>...</CarouselItem>
<CarouselItem>...</CarouselItem>
<CarouselItem>...</CarouselItem>
</CarouselContent>
</Carousel>
Scale and fade
Effects are optional. A normal CarouselItem has one default slot and accepts
any content. Put content inside CarouselLayer to apply scale, fade, or arc
movement. This works with text, cards, buttons, and images.
<Carousel effects={{ scale: { min: 0.8 }, fade: { min: 0.35, distance: 2 } }}>
<CarouselContent>
<CarouselItem>
<CarouselLayer>
<article>Any slide content</article>
</CarouselLayer>
</CarouselItem>
<!-- Add more items. -->
</CarouselContent>
</Carousel>
Each effect can be true for its defaults, an options object, or false.
Omitted effects stay off. scale.distance and fade.distance are distances from
the viewport center in slide widths. They work on both axes, including grouped
slides and slides with different widths.
Parallax
Use effects.parallax with CarouselParallax. This part clips its inner content.
The renderer translates and enlarges that content to keep the edges covered.
It uses the carousel’s axis and direction. Put it inside CarouselLayer to
combine parallax with scale, fade, or arc movement.
<Carousel effects={{ parallax: { amount: 0.2 } }}>
<CarouselContent>
<CarouselItem>
<CarouselParallax class="rounded-xl">
<img src="/project.jpg" alt="A restored deck." class="aspect-square w-full object-cover" />
</CarouselParallax>
</CarouselItem>
<!-- Add more items. -->
</CarouselContent>
</Carousel>
amount is the maximum translation as a fraction of the content size. Its
default is 0.2. The part can contain any content; it has no image slot.
Arc
Use effects.arc for a curved row. The arc changes position and rotation.
Scale and fade remain separate options.
<Carousel
effects={{
arc: { radius: 2.4, angle: 18 },
scale: { min: 0.5, distance: 2.5 },
fade: { distance: 2.5 },
}}
carouselOptions={{ loop: true, align: "center", containScroll: false }}
aria-label="Travel photos"
>
<CarouselContent viewportClass="pt-6 pb-24">
<CarouselItem class="basis-5/12">
<CarouselLayer class="overflow-hidden rounded-xl">
<button type="button" data-carousel-select aria-label="Show coast photo" class="block w-full">
<img src="/coast.jpg" alt="A rocky coast." class="aspect-[4/5] w-full object-cover" />
</button>
</CarouselLayer>
</CarouselItem>
<!-- Add more items. -->
</CarouselContent>
</Carousel>
The radius is in layer widths. The angle is in degrees per slide width.
The defaults are 2.4 and 18. The radius must be positive; the angle is limited
to 1–45 degrees. Increase viewport padding for a deeper arc.
The renderer keeps the visible arc position continuous when Embla relocates a slide for looping. At the opposite end of the loop, it fades a layer over one slide width before that layer switches sides. This prevents a visible jump, including when a small radius puts both ends inside the viewport.
Arc movement supports horizontal carousels. On a vertical carousel, other enabled effects still work. Reduced motion removes all effects and makes button and keyboard navigation instant. Fully transparent layers become inert. Cleanup restores the caller’s inline styles and original inert state.
3D arc
Set effects.arc.mode to "3d" to turn each card around its vertical axis.
The center card faces forward. As a card moves to either side, it recedes into
the distance. Use angle to control how far it turns per slide.
Selection styling and overlays
Items expose data-selected and the group/carousel-item class. Compose an
overlay as ordinary content inside CarouselLayer. For example, use
group-data-[selected=false]/carousel-item:invisible to hide an overlay and its
controls on unselected slides. Blur, dimming, rounding, and overlay placement
belong in this composition.
See Projects Carousel for a complete section with these styles.
The old variant, root arc prop, and item image and overlay slots have been
replaced by effects and the optional parts.
Dot navigation and selection
Put CarouselDots inside Carousel. It creates one labelled button for each
scroll position and sets aria-current="true" on the selected button. The dots
update after a resize or a slide change. They also support grouped slides.
Set label to change the navigation group’s accessible name.
The dots follow the carousel orientation. The active dot grows wider in a horizontal row and taller in a vertical column. The button grows with the dot. Use the Up and Down arrow keys when focus is inside a vertical carousel.
Set --carousel-duration and --carousel-ease on Carousel to control its
CSS transitions. Dots use these values automatically. Captions can use the same
values for a change in opacity or color. The defaults are 300ms and
cubic-bezier(0.22, 1, 0.36, 1). Reduced motion makes these changes instant.
<Carousel style="--carousel-duration: 200ms; --carousel-ease: ease-out">
<CarouselContent>
<CarouselItem>
<p class="transition-opacity duration-(--carousel-duration) ease-(--carousel-ease) group-data-[selected=false]/carousel-item:opacity-60 motion-reduce:transition-none">
Mountain lake
</p>
</CarouselItem>
<!-- Add the other slides here. -->
</CarouselContent>
<CarouselDots />
</Carousel>
carouselOptions.duration controls how quickly Embla settles after a button or
keyboard command. It is a unitless value with a default of 25.
The client script sets data-selected="true" on items in the selected group.
To select a slide from a button, put data-carousel-select on that button
inside the item. Keep overlay links separate from that button.
API Reference
Read the installed source and the Embla v8 API.
carouselOptions?CarouselEmblaOptions
JSON-compatible Embla options. Orientation and Astro parts control the axis and elements.
CarouselEmblaOptionsorientation?'horizontal' | 'vertical'
Scroll axis for the carousel.
'horizontal' | 'vertical'"horizontal"effects?CarouselEffects
Independent effects applied to the optional inner parts.
CarouselEffectsactive?boolean
Enable the carousel's controls.
booleantruecontainScroll?NonNullable<EmblaOptionsType['containScroll']>
Prevent the carousel from scrolling outside of its container.
NonNullable<EmblaOptionsType['containScroll']>truedirection?'ltr' | 'rtl'
The direction of the carousel.
'ltr' | 'rtl''ltr' (left to right)slidesToScroll?NonNullable<EmblaOptionsType['slidesToScroll']>
The number of slides to scroll on a single previous/next button click.
NonNullable<EmblaOptionsType['slidesToScroll']>1dragFree?boolean
Allow the user to drag the carousel freely.
booleanfalsedragThreshold?number
The percentage of the slide that must be dragged to be considered a drag.
number0.5inViewThreshold?NonNullable<EmblaOptionsType['inViewThreshold']>
The percentage of the slide that must be visible to be considered in view.
NonNullable<EmblaOptionsType['inViewThreshold']>0.5loop?boolean
Return to the starting slide when the end is reached.
booleanfalseskipSnaps?boolean
Skip the snap points when the carousel is at the start or end.
booleanfalseduration?number
Unitless duration for programmatic scrolling. Higher values scroll more slowly.
number25startIndex?number
The index of the first slide to show.
number0align?CarouselAlign
Slide alignment. Callback functions require browser setup.
CarouselAlignwatchDrag?boolean
Enable drag handling.
booleantruewatchResize?boolean
Observe carousel size changes.
booleantruewatchSlides?boolean
Observe added or removed slides.
booleantruewatchFocus?boolean
Scroll to focused slide content.
booleantruebreakpoints?Record<string, CarouselEmblaBaseOptions>
Media queries with the same options, without nested breakpoints.
Record<string, CarouselEmblaBaseOptions>gap?string
CSS gap value for space between items.
string"1rem"viewportClass?string
Classes for the clipping viewport, such as padding for an arc.
stringarc?boolean | CarouselArcOptions
Position and rotate CarouselLayer parts on a horizontal arc.
boolean | CarouselArcOptionsscale?boolean | CarouselScaleOptions
Scale CarouselLayer parts.
boolean | CarouselScaleOptionsfade?boolean | CarouselFadeOptions
Fade CarouselLayer parts. Fully transparent layers become inert.
boolean | CarouselFadeOptionsparallax?boolean | CarouselParallaxOptions
Move content inside CarouselParallax parts. Supports either axis.
boolean | CarouselParallaxOptionsmode?'2d' | '3d'
Use a flat arc or turn the layers around the vertical axis in depth.
'2d' | '3d'"2d"radius?number
Circle radius in layer widths. Must be positive.
number2.4angle?number
Rotation per slide width, in degrees. Range: 1–45.
number18min?number
Smallest scale. Range: 0–1.
number0.8distance?number
Distance to the smallest scale, in slide widths.
number1min?number
Lowest opacity. Range: 0–1.
number0distance?number
Distance to the lowest opacity, in slide widths.
number2.5amount?number
Maximum translation as a fraction of the content size. Range: 0–1.
number0.2Embla options in Astro
carouselOptions accepts JSON-compatible values, including loop, duration,
slidesToScroll, and media-query breakpoints. Set align to "start", "center",
or "end" inside this object. The watchDrag, watchFocus, watchResize, and
watchSlides options accept booleans. Breakpoints use the same options and cannot
contain another breakpoints object.
Callbacks and DOM elements cannot pass through the HTML data attribute. Use
orientation for the axis and the Astro parts for the container and slides.
Keep callback options in a browser script through the client API below.
Image loading
Use loading="lazy" on images outside the initial view. Astro’s Image uses
this value by default. Use priority for an important image visible when the
page opens. The browser can load nearby slides before they become visible;
lazy loading does not limit downloads to the selected slide.
Client setup
The Astro component calls create() from carousel/carousel.ts. It binds each
unbound root once and destroys its controllers on removal or an Astro page swap.
There is no carousel state on window.
For manually rendered markup, createCarousel(root, options) returns
{ root, api, destroy }. A repeated call returns the existing controller.
Client options accept carouselOptions, orientation, effects, and Embla
plugins. Browser carouselOptions accepts the full EmblaOptionsType, including
callbacks and DOM elements. The root orientation still sets the base axis.
Plugin functions stay in the client script; do not pass them through Astro
frontmatter props. Call destroy() before binding new controller options.
getCarouselApi(root) reads an existing Embla instance. The same shared effects
setup is available to client scripts:
import { setupCarouselEffects } from "@/components/ui/carousel/effects.ts"
// api is an initialized Embla instance, for example from carousel:init.
const cleanup = setupCarouselEffects(api, {
arc: { radius: 2.4, angle: 18 },
scale: { min: 0.8 },
})
Calling this setup again replaces the earlier effects. Its cleanup removes
listeners and restores styles. Destroying Embla also cleans up the effects.
Keep caller transforms on another element: the effect renderer owns transforms
on CarouselLayer and the inner CarouselParallax content while active.
Events
The carousel root emits these events. Both events use the same detail.
Outbound events
| Event | Detail |
|---|---|
carousel:init |
{ api: EmblaCarouselType, canScrollPrev: boolean, canScrollNext: boolean, selectedScrollSnap: number, scrollSnapList: number[] } |
carousel:select |
{ api: EmblaCarouselType, canScrollPrev: boolean, canScrollNext: boolean, selectedScrollSnap: number, scrollSnapList: number[] } |
When to use Carousel
Put related slides inside CarouselContent and keep each slide in a CarouselItem. Pass Embla options through the carouselOptions prop. Choose an orientation and slide width that match the content rather than forcing all content into one layout.
Keyboard and behavior
The client script starts Embla for drag and swipe behavior. Provide labelled previous and next controls for people who cannot drag. Essential content should remain understandable without swiping through an unexplained sequence.
Theme and layout
Set image dimensions to reduce layout movement and check the last slide at narrow widths. Theme tokens style the controls; slide composition stays in your Astro markup. Autoplay is not supplied by the base component.



