Basic example
TECarousel
component is useful to navigate through a collection of images in a sequential fashion, moving to the previous/next one through the arrows on the sides.
import React from "react"; import { TECarousel, TECarouselItem } from "tw-elements-react"; export default function CarouselBasicExample(): JSX.Element { return ( <> <TECarousel showControls showIndicators ride="carousel"> <div className="relative w-full overflow-hidden after:clear-both after:block after:content-['']"> <TECarouselItem itemID={1} className="relative float-left -mr-[100%] hidden w-full transition-transform duration-[600ms] ease-in-out motion-reduce:transition-none" > <img src="https://tecdn.b-cdn.net/img/Photos/Slides/img%20(15).jpg" className="block w-full" alt="..." /> <div className="absolute inset-x-[15%] bottom-5 hidden py-5 text-center text-white md:block"> <h5 className="text-xl">First slide label</h5> <p> Some representative placeholder content for the first slide. </p> </div> </TECarouselItem> <TECarouselItem itemID={2} className="relative float-left hidden -mr-[100%] w-full transition-transform duration-[600ms] ease-in-out motion-reduce:transition-none" > <img src="https://tecdn.b-cdn.net/img/Photos/Slides/img%20(22).jpg" className="block w-full" alt="..." /> <div className="absolute inset-x-[15%] bottom-5 hidden py-5 text-center text-white md:block"> <h5 className="text-xl">Second slide label</h5> <p> Some representative placeholder content for the second slide. </p> </div> </TECarouselItem> <TECarouselItem itemID={3} className="relative float-left -mr-[100%] hidden w-full transition-transform duration-[600ms] ease-in-out motion-reduce:transition-none" > <img src="https://tecdn.b-cdn.net/img/Photos/Slides/img%20(23).jpg" className="block w-full" alt="..." /> <div className="absolute inset-x-[15%] bottom-5 hidden py-5 text-center text-white md:block"> <h5 className="text-xl">Third slide label</h5> <p> Some representative placeholder content for the third slide. </p> </div> </TECarouselItem> </div> </TECarousel> </> ); }
Slides only
Carousel slides only allows you to automatically and smoothly switch between gallery images without controls. It is a useful component for dynamic design where user involvement is important.
import React from "react"; import { TECarousel, TECarouselItem } from "tw-elements-react"; export default function CarouselSlidesOnly(): JSX.Element { return ( <> <TECarousel ride="carousel"> <div className="relative w-full overflow-hidden after:clear-both after:block after:content-['']"> <TECarouselItem itemID={1} className="relative float-left -mr-[100%] hidden w-full transition-transform duration-[600ms] ease-in-out motion-reduce:transition-none" > <img src="https://mdbcdn.b-cdn.net/img/new/slides/041.webp" className="block w-full" alt="..." /> </TECarouselItem> <TECarouselItem itemID={2} className="relative float-left hidden -mr-[100%] w-full transition-transform duration-[600ms] ease-in-out motion-reduce:transition-none" > <img src="https://mdbcdn.b-cdn.net/img/new/slides/042.webp" className="block w-full" alt="..." /> </TECarouselItem> <TECarouselItem itemID={3} className="relative float-left -mr-[100%] hidden w-full transition-transform duration-[600ms] ease-in-out motion-reduce:transition-none" > <img src="https://mdbcdn.b-cdn.net/img/new/slides/043.webp" className="block w-full" alt="..." /> </TECarouselItem> </div> </TECarousel> </> ); }
With controls
Use the showControls
property to add the previous and next buttons which allow you to control automatically moving gallery items.
import React from "react"; import { TECarousel, TECarouselItem } from "tw-elements-react"; export default function CarouselWithControls(): JSX.Element { return ( <> <TECarousel showControls ride="carousel"> <div className="relative w-full overflow-hidden after:clear-both after:block after:content-['']"> <TECarouselItem itemID={1} className="relative float-left -mr-[100%] hidden w-full transition-transform duration-[600ms] ease-in-out motion-reduce:transition-none" > <img src="https://mdbcdn.b-cdn.net/img/new/slides/041.webp" className="block w-full" alt="..." /> </TECarouselItem> <TECarouselItem itemID={2} className="relative float-left hidden -mr-[100%] w-full transition-transform duration-[600ms] ease-in-out motion-reduce:transition-none" > <img src="https://mdbcdn.b-cdn.net/img/new/slides/042.webp" className="block w-full" alt="..." /> </TECarouselItem> <TECarouselItem itemID={3} className="relative float-left -mr-[100%] hidden w-full transition-transform duration-[600ms] ease-in-out motion-reduce:transition-none" > <img src="https://mdbcdn.b-cdn.net/img/new/slides/043.webp" className="block w-full" alt="..." /> </TECarouselItem> </div> </TECarousel> </> ); }
With indicators
Use the showIndicators
property to add the indicators. Adding it to the carousel, alongside the controls, can make it easier to navigate between the available images.
import React from "react"; import { TECarousel, TECarouselItem } from "tw-elements-react"; export default function CarouselWithIndicators(): JSX.Element { return ( <> <TECarousel showControls showIndicators ride="carousel"> <div className="relative w-full overflow-hidden after:clear-both after:block after:content-['']"> <TECarouselItem itemID={1} className="relative float-left -mr-[100%] hidden w-full transition-transform duration-[600ms] ease-in-out motion-reduce:transition-none" > <img src="https://mdbcdn.b-cdn.net/img/new/slides/041.webp" className="block w-full" alt="..." /> </TECarouselItem> <TECarouselItem itemID={2} className="relative float-left hidden -mr-[100%] w-full transition-transform duration-[600ms] ease-in-out motion-reduce:transition-none" > <img src="https://mdbcdn.b-cdn.net/img/new/slides/042.webp" className="block w-full" alt="..." /> </TECarouselItem> <TECarouselItem itemID={3} className="relative float-left -mr-[100%] hidden w-full transition-transform duration-[600ms] ease-in-out motion-reduce:transition-none" > <img src="https://mdbcdn.b-cdn.net/img/new/slides/043.webp" className="block w-full" alt="..." /> </TECarouselItem> </div> </TECarousel> </> ); }
Crossfade
Use fade transition on your slides easily by adding the crossfade
property to the TECarousel
.
TECarouselItem
elements. import React from "react"; import { TECarousel, TECarouselItem } from "tw-elements-react"; export default function CarouselCrossfade(): JSX.Element { return ( <> <TECarousel showControls showIndicators crossfade ride="carousel"> <div className="relative w-full overflow-hidden after:clear-both after:block after:content-['']"> <TECarouselItem itemID={1} className="relative float-left -mr-[100%] hidden w-full !transform-none transition-opacity duration-[600ms] ease-in-out motion-reduce:transition-none" > <img src="https://mdbcdn.b-cdn.net/img/new/slides/041.webp" className="block w-full" alt="..." /> </TECarouselItem> <TECarouselItem itemID={2} className="relative float-left -mr-[100%] hidden w-full !transform-none opacity-0 transition-opacity duration-[600ms] ease-in-out motion-reduce:transition-none" > <img src="https://mdbcdn.b-cdn.net/img/new/slides/042.webp" className="block w-full" alt="..." /> </TECarouselItem> <TECarouselItem itemID={3} className="relative float-left -mr-[100%] hidden w-full !transform-none opacity-0 transition-opacity duration-[600ms] ease-in-out motion-reduce:transition-none" > <img src="https://mdbcdn.b-cdn.net/img/new/slides/043.webp" className="block w-full" alt="..." /> </TECarouselItem> </div> </TECarousel> </> ); }
Dark variant
Use nextBtnIcon
, prevBtnIcon
properties and theme
object to customize carousel controls and indicators as you want.
import React from "react"; import { TECarousel, TECarouselItem } from "tw-elements-react"; export default function CarouselDarkVariant(): JSX.Element { return ( <> <TECarousel showControls showIndicators crossfade ride="carousel" prevBtnIcon={ <> <span className="inline-block text-black h-8 w-8 [&>svg]:h-8"> <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" strokeWidth="1.5" stroke="currentColor" > <path strokeLinecap="round" strokeLinejoin="round" d="M15.75 19.5L8.25 12l7.5-7.5" /> </svg> </span> <span className="!absolute !-m-px !h-px !w-px !overflow-hidden !whitespace-nowrap !border-0 !p-0 ![clip:rect(0,0,0,0)]"> Previous </span> </> } nextBtnIcon={ <> <span className="inline-block text-black h-8 w-8 [&>svg]:h-8"> <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" strokeWidth="1.5" stroke="currentColor" > <path strokeLinecap="round" strokeLinejoin="round" d="M8.25 4.5l7.5 7.5-7.5 7.5" /> </svg> </span> <span className="!absolute !-m-px !h-px !w-px !overflow-hidden !whitespace-nowrap !border-0 !p-0 ![clip:rect(0,0,0,0)]"> Next </span> </> } theme={{ indicator: "mx-[3px] box-content h-[3px] w-[30px] flex-initial cursor-pointer border-0 border-y-[10px] border-solid border-transparent bg-black bg-clip-padding p-0 -indent-[999px] opacity-50 transition-opacity duration-[600ms] ease-[cubic-bezier(0.25,0.1,0.25,1.0)] motion-reduce:transition-none", }} > <div className="relative w-full overflow-hidden after:clear-both after:block after:content-['']"> <TECarouselItem itemID={1} className="relative float-left -mr-[100%] hidden w-full !transform-none transition-opacity duration-[600ms] ease-in-out motion-reduce:transition-none" > <img src="https://mdbcdn.b-cdn.net/img/Photos/Slides/img%20(19).webp" className="block w-full" alt="..." /> <div className="absolute inset-x-[15%] bottom-5 hidden py-5 text-center text-black md:block"> <h5 className="text-xl">First slide label</h5> <p> Some representative placeholder content for the first slide. </p> </div> </TECarouselItem> <TECarouselItem itemID={2} className="relative float-left -mr-[100%] hidden w-full !transform-none opacity-0 transition-opacity duration-[600ms] ease-in-out motion-reduce:transition-none" > <img src="https://mdbcdn.b-cdn.net/img/Photos/Slides/img%20(35).webp" className="block w-full" alt="..." /> <div className="absolute inset-x-[15%] bottom-5 hidden py-5 text-center text-black md:block"> <h5 className="text-xl">Second slide label</h5> <p> Some representative placeholder content for the second slide. </p> </div> </TECarouselItem> <TECarouselItem itemID={3} className="relative float-left -mr-[100%] hidden w-full !transform-none opacity-0 transition-opacity duration-[600ms] ease-in-out motion-reduce:transition-none" > <img src="https://mdbcdn.b-cdn.net/img/Photos/Slides/img%20(40).webp" className="block w-full" alt="..." /> <div className="absolute inset-x-[15%] bottom-5 hidden py-5 text-center text-black md:block"> <h5 className="text-xl">Third slide label</h5> <p> Some representative placeholder content for the third slide. </p> </div> </TECarouselItem> </div> </TECarousel> </> ); }
Related resources
If you are looking for more advanced options, try Bootstrap Carousel from MDBootstrap.