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
Create a Project Folder:
- Make a new folder, for instance, “card-slider”.
- Open this folder in your VS Code editor.
Initialize the Project:
npm create vite@latest ./ -- --template react
npm install
npm run dev
Modify folder and CSS Files:
- Remove the default assets folder and App.css file.
- 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
- CardSlider.jsx
- CardSlider.css
Adding the Codes
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;
.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;}
}
import React from 'react'
import CardSlider from './CardSlider'
const App = () => {
return <CardSlider />
}
export default App
0 Comments
We are happy to hear from you.