Carousel
A slideshow component for cycling through elements—images or text—like a carousel.
When To Use
When there is a group of content on the same level. When there is insufficient content space, it can be used to save space in the form of a revolving door. Commonly used for a group of pictures/cards.
Example Usage
astro
---
import Carousel from '@/components/data-display/Carousel.astro';
import CarouselItem from '@/components/data-display/CarouselItem.astro';
---
<Carousel class="rounded-lg h-48">
<CarouselItem>
<div class="bg-blue-500 h-full">Slide 1</div>
</CarouselItem>
<CarouselItem>
<div class="bg-green-500 h-full">Slide 2</div>
</CarouselItem>
</Carousel> Autoplay
You can enable automatic cycling by adding the `autoplay` prop. You can control the speed using `autoplayInterval` (in ms, defaults to 3000). Hovering over the carousel automatically pauses it.
astro
<Carousel autoplay autoplayInterval={2000}>
<!-- items -->
</Carousel> Controls & Aspect Ratio
Use the `showControls` prop to display navigation arrows. The `aspectRatio` prop helps enforce consistent dimensions (`video`, `square`, or `auto`).
astro
<Carousel showControls aspectRatio="video" class="rounded-lg">
<CarouselItem>
<div class="bg-blue-500 h-full flex items-center justify-center">1</div>
</CarouselItem>
<CarouselItem>
<div class="bg-green-500 h-full flex items-center justify-center">2</div>
</CarouselItem>
</Carousel> API Reference
| Property | Description | Type | Default |
|---|---|---|---|
| autoplay | Whether to automatically cycle through the items. | boolean | false |
| autoplayInterval | The interval in milliseconds for autoplay. | number | 3000 |
| showDots | Whether to show the pagination dots. | boolean | true |
| showControls | Whether to show the next/previous navigation arrows. | boolean | false |
| aspectRatio | Enforces a specific aspect ratio for the carousel. | 'video' | 'square' | 'auto' | 'auto' |
| class | Additional CSS classes to apply to the carousel container. | string | - |