Building a Carousel with Vanilla JavaScript Last Updated : 06 Jan, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report A carousel, also known as an image slider, is a popular web component that allows users to cycle through a series of images, texts, or other content. we'll walk you through creating a responsive carousel using Vanilla JavaScript, HTML, and CSS without relying on any external libraries.What We Are Going to CreateBasic Carousel: Use HTML to structure the images and CSS for layout and responsiveness.Navigation Buttons: Add "Previous" and "Next" buttons to manually move through images.Auto Play: Implement JavaScript to automatically cycle through images at intervals.Styling: Use CSS to enhance the appearance with borders, shadows, transitions, and ensure responsiveness.Project PreviewCarousel Application - HTML & CSS Structure The first step in building a carousel is to define its HTML & CSS structure. We'll need a container for the carousel, individual images, and navigation buttons for the user to move between the images. HTML <html> <head> <style> body { font-family: Arial, sans-serif; margin: 0; padding: 0; display: flex; justify-content: center; align-items: center; height: 100vh; background: #f4f4f4; } .car { position: relative; width: 80%; max-width: 600px; overflow: hidden; } .img-wrap { display: flex; transition: transform 0.5s ease; } .img-wrap img { width: 100%; height: auto; } .btn { position: absolute; top: 50%; transform: translateY(-50%); background: rgba(0, 0, 0, 0.5); color: #fff; border: none; padding: 10px; font-size: 16px; cursor: pointer; } .btn:hover { background: rgba(0, 0, 0, 0.8); } .prev { left: 10px; } .next { right: 10px; } </style> </head> <body> <div class="car"> <div class="img-wrap"> <img src="https://media.geeksforgeeks.org/wp-content/uploads/20241228102812942963/0_ilw552fVUGbwIzbE.jpg" alt="1"> <img src="https://media.geeksforgeeks.org/wp-content/uploads/20241128161121752603/what-is-javascript.webp" alt="2"> <img src="https://media.geeksforgeeks.org/wp-content/uploads/20240829155421/Amazing-new-Javascript-features-in-ES15.webp" alt="3"> </div> <button class="btn prev">‹</button> <button class="btn next">›</button> </div> </body> </html> In this exampleBody: Center content both vertically and horizontally, with a light background and no margin/padding.Carousel: Restrict width to 80% with a max of 600px, and hide any overflow.Image Wrapper: Arrange images in a row using flex, with smooth transition on transformation.Images: Set images to take full width, keeping their aspect ratio intact.Buttons: Position buttons (prev/next) in the center vertically, with semi-transparent background.Button Hover: Darken button background on hover for better visibility.Prev Button: Position the previous button to the left.Next Button: Position the next button to the right.Carousel - JavaScript Functionality we add the functionality for navigating between images, as well as autoplay that switches the images automatically. JavaScript const prev = document.querySelector('.prev'); const next = document.querySelector('.next'); const wrap = document.querySelector('.img-wrap'); const imgs = document.querySelectorAll('.img-wrap img'); let idx = 0; function showImg() { if (idx >= imgs.length) idx = 0; if (idx < 0) idx = imgs.length - 1; wrap.style.transform = `translateX(-${idx * 100}%)`; } next.addEventListener('click', () => { idx++; showImg(); }); prev.addEventListener('click', () => { idx--; showImg(); }); setInterval(() => { idx++; showImg(); }, 3000); showImg(); In this exampleprev and next buttons for navigation are selected using querySelector.wrap refers to the container for the images.imgs holds all the individual images inside the wrap.idx is a variable to keep track of the currently displayed image.If idx is greater than or equal to the number of images, reset it to 0.If idx is less than 0, reset it to the last image index.Moves the wrap by updating its transform style to shift images horizontally based on the current idx.Next Button: Increments idx and calls showImg() to display the next image.Previous Button: Decrements idx and calls showImg() to display the previous image.Uses setInterval to increment idx every 3000ms (3 seconds) and updates the displayed image via showImg().Complete Code HTML <html> <head> <title>Vanilla JavaScript Carousel</title> <style> body { font-family: Arial, sans-serif; margin: 0; padding: 0; display: flex; justify-content: center; align-items: center; height: 100vh; background: #f4f4f4; } .car { position: relative; width: 80%; max-width: 600px; overflow: hidden; border-radius: 10px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); } .img-wrap { display: flex; transition: transform 0.5s ease; } .img-wrap img { width: 100%; height: auto; flex-shrink: 0; } .btn { position: absolute; top: 50%; transform: translateY(-50%); background: rgba(0, 0, 0, 0.5); color: #fff; border: none; padding: 10px; font-size: 16px; cursor: pointer; border-radius: 50%; } .btn:hover { background: rgba(0, 0, 0, 0.8); } .prev { left: 10px; } .next { right: 10px; } </style> </head> <body> <div class="car"> <div class="img-wrap"> <img src="https://media.geeksforgeeks.org/wp-content/uploads/20241228102812942963/0_ilw552fVUGbwIzbE.jpg" alt="Image 1"> <img src="https://media.geeksforgeeks.org/wp-content/uploads/20241128161121752603/what-is-javascript.webp" alt="Image 2"> <img src="https://media.geeksforgeeks.org/wp-content/uploads/20240829155421/Amazing-new-Javascript-features-in-ES15.webp" alt="Image 3"> </div> <button class="btn prev">‹</button> <button class="btn next">›</button> </div> <script> const prev = document.querySelector('.prev'); const next = document.querySelector('.next'); const wrap = document.querySelector('.img-wrap'); const imgs = document.querySelectorAll('.img-wrap img'); let idx = 0; function showImg() { if (idx >= imgs.length) idx = 0; if (idx < 0) idx = imgs.length - 1; wrap.style.transform = `translateX(-${idx * 100}%)`; } next.addEventListener('click', () => { idx++; showImg(); }); prev.addEventListener('click', () => { idx--; showImg(); }); setInterval(() => { idx++; showImg(); }, 3000); showImg(); </script> </body> </html> Comment More infoAdvertise with us T tanmxcwi Follow Improve Article Tags : HTML javascript-basics JavaScript-Projects Similar Reads HTML Tutorial HTML stands for HyperText Markup Language. It is the standard language used to create and structure content on the web. It tells the web browser how to display text, links, images, and other forms of multimedia on a webpage. HTML sets up the basic structure of a website, and then CSS and JavaScript 11 min read HTML Interview Questions and Answers HTML (HyperText Markup Language) is the foundational language for creating web pages and web applications. Whether you're a fresher or an experienced professional, preparing for an HTML interview requires a solid understanding of both basic and advanced concepts. Below is a curated list of 50+ HTML 14 min read HTML Introduction HTML stands for Hyper Text Markup Language, which is the core language used to structure content on the web. It organizes text, images, links, and media using tags and elements that browsers can interpret. As of 2025, over 95% of websites rely on HTML alongside CSS and JavaScript, making it a fundam 6 min read Introduction to JavaScript JavaScript is a versatile, dynamically typed programming language used for interactive web applications, supporting both client-side and server-side development, and integrating seamlessly with HTML, CSS, and a rich standard library.JavaScript is a single-threaded language that executes one task at 7 min read HTML Tags - A to Z List HTML Tags are fundamental elements used to structure and format content on web pages. They provide instructions to web browsers on how to render text, images, links, and other media.HTML tags are enclosed in angle brackets < > and usually come in pairs: an opening tag and a closing tag. The cl 15+ min read 30+ Web Development Projects with Source Code [2025] Web development is one of the most in-demand career paths in the IT industry, experiencing consistent growth of around 20â25% annually. Whether you're a student starting out or an experienced professional looking to switch or advance your career, it's essential to go beyond theory and demonstrate yo 4 min read Frontend Developer Interview Questions and Answers Frontend development is an important part of web applications, and it is used to build dynamic and user-friendly web applications with an interactive user interface (UI). Many companies are hiring skilled Frontend developers with expertise in HTML, CSS, JavaScript, and modern frameworks and librarie 15+ min read HTML DOCTYPE Declaration HTML DOCTYPE (Document Type Declaration) is an instruction that appears at the beginning of an HTML document, before the <html> tag.Its primary role is to tell the web browser which version of HTML the page is written in, ensuring that the browser renders the content correctly. It is not an HT 4 min read Top 95+ Javascript Projects For 2025 JavaScript is a lightweight, cross-platform programming language that powers dynamic and interactive web content. From real-time updates to interactive maps and animations, JavaScript brings web pages to life.Here, we provided 95+ JavaScript projects with source code and ideas to provide hands-on ex 4 min read Functions in JavaScript Functions in JavaScript are reusable blocks of code designed to perform specific tasks. They allow you to organize, reuse, and modularize code. It can take inputs, perform actions, and return outputs.JavaScriptfunction sum(x, y) { return x + y; } console.log(sum(6, 9)); // output: 15Function Syntax 5 min read Like