Create A Draggable Card Slider in HTML CSS & JavaScript Last Updated : 21 May, 2025 Comments Improve Suggest changes Like Article Like Report In this article, we will demonstrate how to create a draggable card slider using HTML, CSS, and JavaScript. We'll use a GeeksforGeeks card slider as an example and implement the functionality to slide cards left and right using arrow buttons. Additionally, we'll incorporate the draggable option, allowing users to move cards horizontally by clicking and dragging them with the mouse. This draggable card slider will provide a user-friendly way to navigate through card content.Prerequisite: HTMLCSSJAVASCRIPTPreview: Step By Step Guide to Create a Draggable Card SliderStep 1: Create a folder and add HTML, CSS, and JavaScript files.Step 2: Link the CSS and JS files to your HTML file.Step 3: Build the card layout in the HTML file with images and text.Step 4: Style the cards and layout using CSS.Step 5: Write JavaScript to enable left/right card movement using buttons.Step 6: Add drag functionality for horizontal card sliding.Step 7: Test and ensure smooth integration across all files.Example :The below code example implements the HTML, CSS and JavaScript to create A Draggable Card Slider with interactive Sliding operations. index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <link rel="stylesheet" href="/style.css"> <script src="/script.js" defer></script> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css" /> </head> <body> <div class="wrapper"> <i id="left" class="fa-solid fas fa-angle-left"></i> <ul class="carousel"> <li class="card"> <div class="img"><img src="https://media.geeksforgeeks.org/wp-content/uploads/20240213150115/ppp.png" alt="" draggable="false"> </div> <h2 style="color: green; font-weight:bold;"> GeeksforGeeks </h2> <span>Coding Platform</span> </li> <li class="card"> <div class="img"><img src="https://media.geeksforgeeks.org/wp-content/uploads/20240213150115/ppp.png" alt="" draggable="false"> </div> <h2 style="color: green; font-weight:bold;">GeeksforGeeks</h2> <span>Coding Platform</span> </li> <li class="card"> <div class="img"><img src="https://media.geeksforgeeks.org/wp-content/uploads/20240213150115/ppp.png" alt="" draggable="false"> </div> <h2 style="color: green; font-weight:bold;">GeeksforGeeks</h2> <span>Coding Platform</span> </li> <li class="card"> <div class="img"><img src="https://media.geeksforgeeks.org/wp-content/uploads/20240213150115/ppp.png" alt="" draggable="false"> </div> <h2 style="color: green; font-weight:bold;">GeeksforGeeks</h2> <span>Coding Platform</span> </li> <li class="card"> <div class="img"><img src="https://media.geeksforgeeks.org/wp-content/uploads/20240213150115/ppp.png" alt="" draggable="false"> </div> <h2 style="color: green; font-weight:bold;">GeeksforGeeks</h2> <span>Coding Platform</span> </li> <li class="card"> <div class="img"><img src="https://media.geeksforgeeks.org/wp-content/uploads/20240213150115/ppp.png" alt="" draggable="false"> </div> <h2 style="color: green; font-weight:bold;">GeeksforGeeks</h2> <span>Coding Platform</span> </li> </ul> <i id="right" class="fa-solid fas fa-angle-right"></i> </div> </body> </html> style.css * { margin: 0; padding: 0; box-sizing: border-box; font-family: 'Times New Roman', Times, serif; } body { display: flex; align-items: center; justify-content: center; min-height: 100vh; padding: 0 35px; background: rgb(228, 220, 220); } .wrapper { max-width: 1100px; width: 100%; position: relative; } .wrapper i { height: 50px; width: 50px; background: rgb(118, 233, 118); text-align: center; line-height: 50px; border-radius: 50%; cursor: pointer; position: absolute; top: 50%; font-size: 1.25 rem; transform: translateY(-50%); box-shadow: 0 3px 6px rgba(0, 0, 0, 0.23); } .wrapper i:first-child { left: -22px; } .wrapper i:last-child { right: -22px; } .wrapper .carousel { display: grid; grid-auto-flow: column; grid-auto-columns: calc((100% / 3) - 12px); gap: 16px; overflow-x: auto; scroll-snap-type: x mandatory; scroll-behavior: smooth; scrollbar-width: 0; } .carousel::-webkit-scrollbar { display: none; } .carousel :where(.card, .img) { display: flex; align-items: center; justify-content: center; } .carousel.dragging { scroll-snap-type: none; scroll-behavior: auto; } .carousel.no-transition { scroll-behavior: auto; } .carousel.dragging .card { cursor: grab; user-select: none; } .carousel .card { scroll-snap-align: start; height: 340px; list-style: none; background: #fff; border-radius: 8px; display: flex; cursor: pointer; width: 98%; padding-bottom: 15px; align-items: center; justify-content: center; flex-direction: column; } .card .img { background: green; width: 145px; height: 145px; border-radius: 50%; } .card .img img { width: 140px; height: 140px; object-fit: cover; border-radius: 50%; border: 4px solid #fff; } .card h2 { font-weight: 500; font-size: 1.56rem; margin: 30px 0 5px; } .card span { color: #6a6d78; font-size: 1.31rem; } @media screen and (max-width: 900px) { .wrapper .carousel { grid-auto-columns: calc((100% / 2) - 9px); } } @media screen and (max-width: 600px) { .wrapper .carousel { grid-auto-columns: 100%; } } index.js document.addEventListener("DOMContentLoaded", function() { const carousel = document.querySelector(".carousel"); const arrowBtns = document.querySelectorAll(".wrapper i"); const wrapper = document.querySelector(".wrapper"); const firstCard = carousel.querySelector(".card"); const firstCardWidth = firstCard.offsetWidth; let isDragging = false, startX, startScrollLeft, timeoutId; const dragStart = (e) => { isDragging = true; carousel.classList.add("dragging"); startX = e.pageX; startScrollLeft = carousel.scrollLeft; }; const dragging = (e) => { if (!isDragging) return; // Calculate the new scroll position const newScrollLeft = startScrollLeft - (e.pageX - startX); // Check if the new scroll position exceeds // the carousel boundaries if (newScrollLeft <= 0 || newScrollLeft >= carousel.scrollWidth - carousel.offsetWidth) { // If so, prevent further dragging isDragging = false; return; } // Otherwise, update the scroll position of the carousel carousel.scrollLeft = newScrollLeft; }; const dragStop = () => { isDragging = false; carousel.classList.remove("dragging"); }; const autoPlay = () => { // Return if window is smaller than 800 if (window.innerWidth < 800) return; // Calculate the total width of all cards const totalCardWidth = carousel.scrollWidth; // Calculate the maximum scroll position const maxScrollLeft = totalCardWidth - carousel.offsetWidth; // If the carousel is at the end, stop autoplay if (carousel.scrollLeft >= maxScrollLeft) return; // Autoplay the carousel after every 2500ms timeoutId = setTimeout(() => carousel.scrollLeft += firstCardWidth, 2500); }; carousel.addEventListener("mousedown", dragStart); carousel.addEventListener("mousemove", dragging); document.addEventListener("mouseup", dragStop); wrapper.addEventListener("mouseenter", () => clearTimeout(timeoutId)); wrapper.addEventListener("mouseleave", autoPlay); // Add event listeners for the arrow buttons to // scroll the carousel left and right arrowBtns.forEach(btn => { btn.addEventListener("click", () => { carousel.scrollLeft += btn.id === "left" ? -firstCardWidth : firstCardWidth; }); }); }); Output: Comment More infoAdvertise with us Next Article Create A Draggable Card Slider in HTML CSS & JavaScript K kasoti2002 Follow Improve Article Tags : Project JavaScript Web Technologies Dev Scripter JavaScript-Projects Dev Scripter 2024 +2 More Similar Reads Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co 11 min read JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav 11 min read Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De 5 min read Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance 10 min read React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications 15+ min read Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact 12 min read Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We 9 min read JavaScript Interview Questions and Answers JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as 15+ min read React Tutorial React is a JavaScript Library known for front-end development (or user interface). It is popular due to its component-based architecture, Single Page Applications (SPAs), and Virtual DOM for building web applications that are fast, efficient, and scalable.Applications are built using reusable compon 8 min read Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and 9 min read Like