Use our Tailwind CSS carousel for your website for sliding through multiple elements or images.
See below our simple Carousel
example that you can use in your Tailwind CSS and React project.
import { Carousel } from "@material-tailwind/react";
export function CarouselDefault() {
return (
<Carousel className="rounded-xl">
<img
src="https://images.unsplash.com/photo-1497436072909-60f360e1d4b1?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2560&q=80"
alt="image 1"
className="h-full w-full object-cover"
/>
<img
src="https://images.unsplash.com/photo-1493246507139-91e8fad9978e?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2940&q=80"
alt="image 2"
className="h-full w-full object-cover"
/>
<img
src="https://images.unsplash.com/photo-1518623489648-a173ef7824f3?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2762&q=80"
alt="image 3"
className="h-full w-full object-cover"
/>
</Carousel>
);
}
Use the example below for a complex carousel example with more content.
It is not so much for its beauty that the forest makes a claim upon men's hearts, as for that subtle something, that quality of air that emanation from old trees, that so wonderfully changes and renews a weary spirit.
It is not so much for its beauty that the forest makes a claim upon men's hearts, as for that subtle something, that quality of air that emanation from old trees, that so wonderfully changes and renews a weary spirit.
It is not so much for its beauty that the forest makes a claim upon men's hearts, as for that subtle something, that quality of air that emanation from old trees, that so wonderfully changes and renews a weary spirit.
import { Carousel, Typography, Button } from "@material-tailwind/react";
export function CarouselWithContent() {
return (
<Carousel className="rounded-xl">
<div className="relative h-full w-full">
<img
src="https://images.unsplash.com/photo-1497436072909-60f360e1d4b1?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2560&q=80"
alt="image 1"
className="h-full w-full object-cover"
/>
<div className="absolute inset-0 grid h-full w-full place-items-center bg-black/75">
<div className="w-3/4 text-center md:w-2/4">
<Typography
variant="h1"
color="white"
className="mb-4 text-3xl md:text-4xl lg:text-5xl"
>
The Beauty of Nature
</Typography>
<Typography
variant="lead"
color="white"
className="mb-12 opacity-80"
>
It is not so much for its beauty that the forest makes a claim
upon men's hearts, as for that subtle something, that quality
of air that emanation from old trees, that so wonderfully changes
and renews a weary spirit.
</Typography>
<div className="flex justify-center gap-2">
<Button size="lg" color="white">
Explore
</Button>
<Button size="lg" color="white" variant="text">
Gallery
</Button>
</div>
</div>
</div>
</div>
<div className="relative h-full w-full">
<img
src="https://images.unsplash.com/photo-1493246507139-91e8fad9978e?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2940&q=80"
alt="image 2"
className="h-full w-full object-cover"
/>
<div className="absolute inset-0 grid h-full w-full items-center bg-black/75">
<div className="w-3/4 pl-12 md:w-2/4 md:pl-20 lg:pl-32">
<Typography
variant="h1"
color="white"
className="mb-4 text-3xl md:text-4xl lg:text-5xl"
>
The Beauty of Nature
</Typography>
<Typography
variant="lead"
color="white"
className="mb-12 opacity-80"
>
It is not so much for its beauty that the forest makes a claim
upon men's hearts, as for that subtle something, that quality
of air that emanation from old trees, that so wonderfully changes
and renews a weary spirit.
</Typography>
<div className="flex gap-2">
<Button size="lg" color="white">
Explore
</Button>
<Button size="lg" color="white" variant="text">
Gallery
</Button>
</div>
</div>
</div>
</div>
<div className="relative h-full w-full">
<img
src="https://images.unsplash.com/photo-1518623489648-a173ef7824f3?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2762&q=80"
alt="image 3"
className="h-full w-full object-cover"
/>
<div className="absolute inset-0 grid h-full w-full items-end bg-black/75">
<div className="w-3/4 pl-12 pb-12 md:w-2/4 md:pl-20 md:pb-20 lg:pl-32 lg:pb-32">
<Typography
variant="h1"
color="white"
className="mb-4 text-3xl md:text-4xl lg:text-5xl"
>
The Beauty of Nature
</Typography>
<Typography
variant="lead"
color="white"
className="mb-12 opacity-80"
>
It is not so much for its beauty that the forest makes a claim
upon men's hearts, as for that subtle something, that quality
of air that emanation from old trees, that so wonderfully changes
and renews a weary spirit.
</Typography>
<div className="flex gap-2">
<Button size="lg" color="white">
Explore
</Button>
<Button size="lg" color="white" variant="text">
Gallery
</Button>
</div>
</div>
</div>
</div>
</Carousel>
);
}
You can change the carousel transition using the transition
prop, you can pass the transition duration as seconds for transition
prop.
import { Carousel } from "@material-tailwind/react";
export function CarouselTransition() {
return (
<Carousel transition={{ duration: 2 }} className="rounded-xl">
<img
src="https://images.unsplash.com/photo-1497436072909-60f360e1d4b1?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2560&q=80"
alt="image 1"
className="h-full w-full object-cover"
/>
<img
src="https://images.unsplash.com/photo-1493246507139-91e8fad9978e?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2940&q=80"
alt="image 2"
className="h-full w-full object-cover"
/>
<img
src="https://images.unsplash.com/photo-1518623489648-a173ef7824f3?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2762&q=80"
alt="image 3"
className="h-full w-full object-cover"
/>
</Carousel>
);
}
You can change the carousel arrows using the prevArrow
and nextArrow
props.
import { Carousel, IconButton } from "@material-tailwind/react";
export function CarouselCustomArrows() {
return (
<Carousel
className="rounded-xl"
prevArrow={({ handlePrev }) => (
<IconButton
variant="text"
color="white"
size="lg"
onClick={handlePrev}
className="!absolute top-2/4 left-4 -translate-y-2/4"
>
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
strokeWidth={2}
stroke="currentColor"
className="h-6 w-6"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M10.5 19.5L3 12m0 0l7.5-7.5M3 12h18"
/>
</svg>
</IconButton>
)}
nextArrow={({ handleNext }) => (
<IconButton
variant="text"
color="white"
size="lg"
onClick={handleNext}
className="!absolute top-2/4 !right-4 -translate-y-2/4"
>
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
strokeWidth={2}
stroke="currentColor"
className="h-6 w-6"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M13.5 4.5L21 12m0 0l-7.5 7.5M21 12H3"
/>
</svg>
</IconButton>
)}
>
<img
src="https://images.unsplash.com/photo-1497436072909-60f360e1d4b1?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2560&q=80"
alt="image 1"
className="h-full w-full object-cover"
/>
<img
src="https://images.unsplash.com/photo-1493246507139-91e8fad9978e?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2940&q=80"
alt="image 2"
className="h-full w-full object-cover"
/>
<img
src="https://images.unsplash.com/photo-1518623489648-a173ef7824f3?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2762&q=80"
alt="image 3"
className="h-full w-full object-cover"
/>
</Carousel>
);
}
You can change the carousel bottom navigation using the navigation
prop.
import { Carousel } from "@material-tailwind/react";
export function CarouselCustomNavigation() {
return (
<Carousel
className="rounded-xl"
navigation={({ setActiveIndex, activeIndex, length }) => (
<div className="absolute bottom-4 left-2/4 z-50 flex -translate-x-2/4 gap-2">
{new Array(length).fill("").map((_, i) => (
<span
key={i}
className={`block h-1 cursor-pointer rounded-2xl transition-all content-[''] ${
activeIndex === i ? "w-8 bg-white" : "w-4 bg-white/50"
}`}
onClick={() => setActiveIndex(i)}
/>
))}
</div>
)}
>
<img
src="https://images.unsplash.com/photo-1497436072909-60f360e1d4b1?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2560&q=80"
alt="image 1"
className="h-full w-full object-cover"
/>
<img
src="https://images.unsplash.com/photo-1493246507139-91e8fad9978e?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2940&q=80"
alt="image 2"
className="h-full w-full object-cover"
/>
<img
src="https://images.unsplash.com/photo-1518623489648-a173ef7824f3?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2762&q=80"
alt="image 3"
className="h-full w-full object-cover"
/>
</Carousel>
);
}
The following props are available for carousel component. These are the custom props that we've added for the carousel component and you can use all the other native props as well.
Attribute | Type | Description | Default |
---|---|---|---|
prevArrow | Prev Arrow | Change the previous arrow component for carousel | Arrow |
nextArrow | Next Arrow | Change the next arrow component for carousel | Arrow |
navigation | Navigation | Change the navigation component for carousel | Dots |
slideRef | Slide Ref | Add reference for the carousel slide | undefined |
autoplay | boolean | Sets the autoplay mode for carousel | false |
autoplayDelay | number | Sets the interval duration for autoplay mode in miliseconds | 5000 |
transition | Framer Motion | Sets the transition for carousel |
|
loop | boolean | Sets the looping mode for the carousel | false |
className | string | Add custom className for card | '' |
children | node | Add content for card | No default value it's a required prop. |
import type { CarouselProps } from "@material-tailwind/react";
type prevArrow = (args: {
loop: boolean;
handlePrev: () => void;
activeIndex: number;
firstIndex: boolean;
}) => React.ReactNode | void;
type nextArrow = (args: {
loop: boolean;
handleNext: () => void;
activeIndex: number;
lastIndex: boolean;
}) => React.ReactNode | void;
type navigation = (args: {
setActiveIndex: React.Dispatch<React.SetStateAction<number>>;
activeIndex: number;
length: number;
}) => React.ReactNode | void;
export type slideRef = React.Ref<HTMLDivElement>;
Learn how to customize the theme and styles for carousel component, the theme object for carousel component has two main objects:
A. The defaultProps
object for setting up the default value for props of carousel component.
B. The styles
object for customizing the theme and styles of carousel component.
You can customize the theme and styles of carousel component by adding Tailwind CSS classes as key paired values for objects.
interface CarouselStylesType {
defaultProps: {
prevArrow: (args: {
loop: boolean;
handlePrev: () => void;
activeIndex: number;
firstIndex: boolean;
}) => React.ReactNode | void;
nextArrow: (args: {
loop: boolean;
handleNext: () => void;
activeIndex: number;
lastIndex: boolean;
}) => React.ReactNode | void;
navigation: (args: {
setActiveIndex: React.Dispatch<React.SetStateAction<number>>;
activeIndex: number;
length: number;
}) => React.ReactNode | void;
transition: object;
autoplay: boolean;
autoplayDelay: number;
loop: boolean;
className: string;
};
styles: {
base: {
carousel: object;
slide: object;
};
};
}
import { CarouselStylesType } from "@material-tailwind/react";
const theme = {
carousel: {
defaultProps: {
prevArrow: ({ loop, handlePrev, firstIndex }) => {
return (
<button
onClick={handlePrev}
disabled={!loop && firstIndex}
className="!absolute top-2/4 left-4 -translate-y-2/4 rounded-full select-none transition-all disabled:opacity-50 disabled:shadow-none disabled:pointer-events-none w-12 max-w-[48px] h-12 max-h-[48px] text-white hover:bg-white/10 active:bg-white/30 grid place-items-center"
>
<ChevronLeftIcon strokeWidth={3} className="-ml-1 h-7 w-7" />
</button>
);
},
nextArrow: ({ loop, handleNext, lastIndex }) => (
<button
onClick={handleNext}
disabled={!loop && lastIndex}
className="!absolute top-2/4 right-4 -translate-y-2/4 rounded-full select-none transition-all disabled:opacity-50 disabled:shadow-none disabled:pointer-events-none w-12 max-w-[48px] h-12 max-h-[48px] text-white hover:bg-white/10 active:bg-white/30 grid place-items-center"
>
<ChevronRightIcon strokeWidth={3} className="ml-1 h-7 w-7" />
</button>
),
navigation: ({ setActiveIndex, activeIndex, length }) => (
<div className="absolute bottom-4 left-2/4 z-50 flex -translate-x-2/4 gap-2">
{new Array(length).fill("").map((_, i) => (
<span
key={i}
className={`block h-3 w-3 cursor-pointer rounded-full transition-colors content-[''] ${
activeIndex === i ? "bg-white" : "bg-white/50"
}`}
onClick={() => setActiveIndex(i)}
/>
))}
</div>
),
autoplay: false,
autoplayDelay: 5000,
transition: {
type: "tween",
duration: 0.5,
},
loop: false,
className: "",
},
styles: {
base: {
carousel: {
position: "relative",
width: "w-full",
height: "h-full",
overflowX: "overflow-x-hidden",
display: "flex",
},
slide: {
width: "w-full",
height: "h-full",
display: "inline-block",
flex: "flex-none",
},
},
},
},
};