This React JS tutorial with Tailwind CSS will teach you how to create a carousel slider with various functionalities. We’ll explore building carousels with arrow navigation, autoplay, and even a vertical layout, all leveraging the power of Tailwind CSS for styling.
First you need to setup react project with tailwind css. You can install manually or you read below blog.
How to install Tailwind CSS in React
Install & Setup Vite + React + Typescript + Tailwind CSS 3
Install swiper in React
Swiper React is available only via NPM as a part of the main Swiper library:
npm i swiper
Example 1
Build a Simple Arrow Navigation Slider in React JS with Tailwind CSS and SwiperJS.
import React from "react";
// Import Swiper React components
import { Swiper, SwiperSlide } from "swiper/react";
// Import Swiper styles
import "swiper/css";
import "swiper/css/navigation";
// import required modules
import { Navigation } from "swiper";
export default function SliderComponent() {
return (
<>
<Swiper
navigation={true}
modules={[Navigation]}
className="mySwiper"
>
<SwiperSlide>
<img
className="object-fill w-full h-96"
src="https://cdn.pixabay.com/photo/2022/03/20/15/40/nature-7081138__340.jpg"
alt="image slide 1"
/>
</SwiperSlide>
<SwiperSlide>
<img
className="object-fill w-full h-96"
src="https://cdn.pixabay.com/photo/2022/07/24/17/55/wind-energy-7342177__340.jpg"
alt="image slide 2"
/>
</SwiperSlide>
<SwiperSlide>
<img
className="object-fill w-full h-96"
src="https://cdn.pixabay.com/photo/2022/07/26/03/35/jogger-7344979__340.jpg"
alt="image slide 3"
/>
</SwiperSlide>
</Swiper>
</>
);
}
Example 2
React js with tailwind css pagination carousel slider using swiperjs.
import React from "react";
// Import Swiper React components
import { Swiper, SwiperSlide } from "swiper/react";
// Import Swiper styles
import "swiper/css";
import "swiper/css/pagination";
// import required modules
import { Pagination } from "swiper";
export default function SliderComponent() {
return (
<>
<Swiper
pagination={true}
modules={[Pagination]}
className="mySwiper"
>
<SwiperSlide>
<img
className="object-fill w-full h-96"
src="https://cdn.pixabay.com/photo/2022/03/20/15/40/nature-7081138__340.jpg"
alt="image slide 1"
/>
</SwiperSlide>
<SwiperSlide>
<img
className="object-fill w-full h-96"
src="https://cdn.pixabay.com/photo/2022/07/24/17/55/wind-energy-7342177__340.jpg"
alt="image slide 2"
/>
</SwiperSlide>
<SwiperSlide>
<img
className="object-fill w-full h-96"
src="https://cdn.pixabay.com/photo/2022/07/26/03/35/jogger-7344979__340.jpg"
alt="image slide 3"
/>
</SwiperSlide>
</Swiper>
</>
);
}
Example 3
Create an Autoplay Slider with Arrows and Pagination in React JS using Tailwind CSS and SwiperJS.
import React from "react";
// Import Swiper React components
import { Swiper, SwiperSlide } from "swiper/react";
// Import Swiper styles
import "swiper/css";
import "swiper/css/pagination";
import "swiper/css/navigation";
// import required modules
import { Autoplay, Pagination, Navigation } from "swiper";
export default function SliderComponent() {
return (
<>
<Swiper
spaceBetween={30}
centeredSlides={true}
autoplay={{
delay: 2500,
disableOnInteraction: false,
}}
pagination={{
clickable: true,
}}
navigation={true}
modules={[Autoplay, Pagination, Navigation]}
className="mySwiper"
>
<SwiperSlide>
<img
className="object-fill w-full h-96"
src="https://cdn.pixabay.com/photo/2022/03/20/15/40/nature-7081138__340.jpg"
alt="image slide 1"
/>
</SwiperSlide>
<SwiperSlide>
<img
className="object-fill w-full h-96"
src="https://cdn.pixabay.com/photo/2022/07/24/17/55/wind-energy-7342177__340.jpg"
alt="image slide 2"
/>
</SwiperSlide>
<SwiperSlide>
<img
className="object-fill w-full h-96"
src="https://cdn.pixabay.com/photo/2022/07/26/03/35/jogger-7344979__340.jpg"
alt="image slide 3"
/>
</SwiperSlide>
</Swiper>
</>
);
}
Example 4
React js with tailwind css vertical slider using swiperjs.
import React from "react";
// Import Swiper React components
import { Swiper, SwiperSlide } from "swiper/react";
// Import Swiper styles
import "swiper/css";
import "swiper/css/pagination";
// import required modules
import { Pagination } from "swiper";
export default function SliderComponent() {
return (
<>
<Swiper
direction={"vertical"}
pagination={{
clickable: true,
}}
modules={[Pagination]}
className="mySwiper"
>
<SwiperSlide>
<img
className="object-fill w-full h-96"
src="https://cdn.pixabay.com/photo/2022/03/20/15/40/nature-7081138__340.jpg"
alt="image slide 1"
/>
</SwiperSlide>
<SwiperSlide>
<img
className="object-fill w-full h-96"
src="https://cdn.pixabay.com/photo/2022/07/24/17/55/wind-energy-7342177__340.jpg"
alt="image slide 2"
/>
</SwiperSlide>
<SwiperSlide>
<img
className="object-fill w-full h-96"
src="https://cdn.pixabay.com/photo/2022/07/26/03/35/jogger-7344979__340.jpg"
alt="image slide 3"
/>
</SwiperSlide>
</Swiper>
</>
);
}