How To Make A Responsive Card Slider Using React JS For Beginners


Welcome to my website U-GINE MEDIA. In this website I teach tutorials and share source code on some programming (HTML, CSS, JavaScript, React) tutorials. 

Before we get started, do well to subscribe to my channel to never miss out on any update that I post every single day.

You can also visit my GitHub where I upload all the code I have.

In this tutorial topic "How To Make A Responsive Card Slider Using React JS For Beginners", we will learn how to create it following these simple easy steps.

Share this to your programmer friends. If y'all learnt something today, make sure to let me in the comments. 

Get Source Code.

If interested in watching the video before running the code below, you can click the play button to get started.

Setting Up the Project

Before building the "Responsive Card Slider Using React JS" with React.js and CSS, ensure Node.js is installed on your computer. If not, download and install it from the official Node.js website.

Create a Project Folder:

  1. Make a new folder, for instance, “card-slider”.
  2. Open this folder in your VS Code editor.

Initialize the Project:

Open your terminal by pressing Ctrl + J and then use Vite to create a new React app with this command:

  npm create vite@latest ./ -- --template react


Install necessary dependencies and start the development server:

npm install
npm run dev

If your project is running in your browser, congratulations! You’ve successfully set up your app. Now, let’s move on to modifying folders and files.

Modify folder and CSS Files:

  1. Remove the default assets folder and App.css file.
  2. Replace the content of index.css with the provided CSS code.


@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@100;300;400;500;700&display=swap');

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: "Poppins", sans-serif;
}
  
  
  


Creating the Files

Within the src directory of your project, create the following files:
  1. CardSlider.jsx
  2. CardSlider.css

Adding the Codes

Add the respective code to each newly created file to define the layout.

In src/CardSlider.jsx, add the code.

import React, { useState } from 'react';
import "./CardSlider.css"
import user1 from "./assets/user-1.jpg";
import user2 from "./assets/user-2.jpg";
import user3 from "./assets/user-3.jpg";
import user4 from "./assets/user-4.jpg";
import user5 from "./assets/user-5.jpg";

const cards = [
  {
    title: 'Front-End Developer',
    name: 'Sarah Lind',
    desc: 'An expert in UI/UX and responsive web development, using React and TailwindCSS to bring designs to life.',
    image: user1,
    bg: '#d8b4fe',
    btn: '#a855f7',
  },
  {
    title: 'Junior Developer',
    name: 'Jane Developer',
    desc: 'An up-and-coming developer skilled in HTML, CSS, and JavaScript with a passion for building clean interfaces.',
    image: user2,
    bg: '#fdba74',
    btn: '#f97316',
  },
  {
    title: 'Full Stack Developer',
    name: 'Anna Smith',
    desc: 'Specialized in both frontend and backend, building scalable applications using React and Node.js.',
    image: user3,
    bg: '#f9a8d4',
    btn: '#ec4899',
  },
  {
    title: 'Mobile Frontend Developer',
    name: 'Alan Doe',
    desc: 'Specialized in both frontend and backend, building scalable applications using React and Node.js.',
    image: user4,
    bg: '#b8e0d2',
    btn: '#00bd7b',
  },
   {
    title: 'Blockchain Developer',
    name: 'John Doe',
    desc: 'Specialized in both frontend and backend, building scalable applications using React and Node.js.',
    image: user5,
    bg: '#a0ced9',
    btn: '#00c5f7',
  },
];

const CardSlider = () => {
  const [currentIndex, setCurrentIndex] = useState(2);

  const prevSlide = () => {
    setCurrentIndex((prev) => (prev - 1 + cards.length) % cards.length);
  };

  const nextSlide = () => {
    setCurrentIndex((prev) => (prev + 1) % cards.length);
  };

  const getCardStyle = (index) => {
    const position = index - currentIndex;
    const baseZ = -Math.abs(position);
    const scale = position === 0 ? 1.2 : 0.8;
    const opacity = position === 0 ? 1 : 0.4;
    const translateX = position * 120;
    return {
      transform: `translateX(${translateX}%) scale(${scale})`,
      zIndex: 100 + baseZ,
      opacity,
    };
  };

  return (
    <div className="slider-wrapper">
      <div className="slider-container">
        <div className="card-slider">
          {cards.map((card, index) => (
            <div
              key={index}
              className="card"
              style={getCardStyle(index)}
            >
              <div className="card-header" style={{backgroundColor: `${card.bg}`}}>
                <img src={card.image} />
              </div>
              <div className="card-body">
                <h3>{card.title}</h3>
                <p className='name'>{card.name}</p>
                <p>{card.desc}</p>
                <button style={{backgroundColor: `${card.btn}`}}>About Me</button>
              </div>
            </div>
          ))}
        </div>
        <div className="nav-buttons">
          <button onClick={prevSlide}>{"<"}</button>
          <button onClick={nextSlide}>{">"}</button>
        </div>
      </div>
    </div>
  );
};

export default CardSlider;



In src/CardSlider.css, add the code.

.slider-wrapper {
  display: flex;
  justify-content: center;
  align-items: center;
  background: #111;
  height: 100vh;
  padding: 2rem;
  box-sizing: border-box;
}

.slider-container {
  position: relative;
  width: 100%;
  max-width: 1000px;
  overflow: hidden;
}

.card-slider {
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  width: 100%;
  height: 550px;
  perspective: 1000px;
  transition: transform 0.5s ease-in-out;
}

.card {
  position: absolute;
  width: 100%; 
  background: #fff;
  border-radius: 12px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  font-size: 2rem;
  transition: all 0.5s ease;
  transform-style: preserve-3d;
  overflow: hidden;
}

.card-header {
  padding: 40px 0;
  position: relative;
  border-bottom-left-radius: 100%;
  border-bottom-right-radius: 100%;
  width: 100%;
  height: 150px;
}

.card-header img {
  width: 150px;
  height: 150px;
  border-radius: 50%;
  border: 5px solid #fff;
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  object-fit: cover;
  object-position: center;
}

.card-body {
  padding: 50px 20px 30px;
  text-align: center;
}

.card-body h3 {
  margin-top: 0;
  font-size: 18px;
  color: #111827;
  text-align: center;
  text-transform: uppercase;
}

.card-body .name {
  font-weight: bold;
  font-size: 14px;
  color: #6b7280;
  margin: 6px 0 10px;
}

.card-body p {
  font-size: 13px;
  color: #374151;
  margin-bottom: 20px;
}

.card-body button {
  color: #fff;
  border: none;
  padding: 8px 16px;
  border-radius: 6px;
  font-size: 14px;
  cursor: pointer;
  transition: 0.3s;
}

.card-body button:hover {
  opacity: 0.9;
}

.nav-buttons {
  display: flex;
  justify-content: center;
  margin-top: 2rem;
}

.nav-buttons button {
  margin: 0 10px;
  padding: 0.6rem 1.5rem;
  background-color: #27ae60;
  color: #fff;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-weight: bold;
  font-size: 1.4em;
  transition: all 0.3s ease;
}

.nav-buttons button:first-child {
  background-color: #2c3e50;
}

.nav-buttons button:hover {
  opacity: 0.8;
}


/* Breakpoints */
@media (min-width: 640px) {
  .card { width: 60%; }
}

@media (min-width: 768px) {
  .card { width: 40%; }
}

@media (min-width: 1024px) {
  .card { width: 30%; }
}

@media (max-width: 768px) {
  .slider-container { padding: 40px; }
  .card-header {height: 120px;}
  .card-header img {width: 120px; height: 120px;}
  .card-body h3 {font-size: 16px;}
  .card-body p {margin-bottom: 10px;}
  .nav-buttons  {margin-top: 3rem;}
}





In App.jsx, add the code.

import React from 'react'
import CardSlider from './CardSlider'

const App = () => {
  return <CardSlider />
}

export default App


Conclusion and Final word

Now, we have reached the end of our code, for any information, you can comment any problem or code-not-working issues to resolve it, am always with you. You can also visit my YouTube Channel for more updates on HTML, CSS & JavaScript Designs. I believe you can create "A Responsive Card Slider Using React JS For Beginners". and do so much more with codes. Go extremely far in exploring the world of HTML, CSS, JavaScript React Js & Tailwind Projects, its gonna be fun, trust me 😃.

Keep experimenting! You can add other cool features. Try improving error handling and adding other cool updates to make your chatbot even better.

If you run into any issues, you can download the source code for this AI chatbot project by clicking the “Download” button. Don’t forget to check the README.md file for setup and usage instructions. If you need help, you’ll also find support information there.

Happy Coding Journey 🚀











Post a Comment

0 Comments

Close Menu