Step-by-Step Guide to Creating a Carousel with HTML, CSS, and JavaScript

In this tutorial, I will guide you through the process of creating a Carousel, also known as an Image Slider, using HTML, CSS, and JavaScript. Carousels are widely used in modern web design to showcase images, content, or products in an interactive and visually appealing way.

The primary goal of this tutorial is to provide you with valuable insights and practical ideas for building an image slider that can be customized and integrated into your own projects. Whether you're designing a portfolio, an e-commerce website, or any application that benefits from dynamic content display, this feature can enhance user engagement and overall aesthetics.

By the end of this tutorial, you'll have a fully functional carousel that you can adapt to suit your future web development needs.

What is Carousel?

The Carousel I am referring to is an image slider commonly used in web applications. This feature is designed to display a series of images, allowing users to navigate between slides seamlessly. With intuitive controls, users can move to the previous or next image slide, enhancing their browsing experience.

Image sliders are a popular element in modern web design, often used in portfolios, product showcases, or landing pages to present visual content in an engaging and interactive way. By implementing this feature, you can create an eye-catching and functional component that improves the overall appeal of your web application.

Let’s learn how to build a carousel step-by-step, ensuring it’s both user-friendly and visually appealing for your projects.

Step 1: Creating the Interface

To get started, let’s design the sample web application interface by creating the main HTML file. Name the file index.html. This file will include the HTML elements required for implementing the carousel feature. Refer to the script provided below for details.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3.     <meta charset="UTF-8">
  4.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  5.     <title>Simple Carousel using HTML, CSS, and JS</title>
  6.     <link rel="stylesheet" href="style.css">
  7.     <script src="https://code.jquery.com/jquery-3.7.1.min.js" crossorigin="anonymous"></script>
  8. </head>
  9.     <div id="main-wrapper">
  10.         <h2 id="page-title">Simple Carousel using HTML, CSS, and JS</h2>
  11.  
  12.         <div class="carousel-container">
  13.             <!-- Carousel Items -->
  14.             <div class="carousel">
  15.                 <div class="carousel-item active">
  16.                     <img src="images/1.jpg" alt="Sample Image 1">
  17.                 </div>
  18.                 <div class="carousel-item">
  19.                     <img src="images/2.jpg" alt="Sample Image 2">
  20.                 </div>
  21.                 <div class="carousel-item">
  22.                     <img src="images/3.jpg" alt="Sample Image 3">
  23.                 </div>
  24.             </div>
  25.             <!-- Carousel Buttons -->
  26.             <div class="carousel_actions">
  27.                 <button class="carousel-btn carousel-prev-button" aria-label="Previous Slide"><</button>
  28.                 <button class="carousel-btn carousel-next-button" aria-label="Next Slide">></button>
  29.             </div>
  30.         </div>
  31.     </div>
  32.     <script src="script.js"></script>
  33. </body>
  34. </html>

Step 2: Creating the CSS

Next, we’ll create the CSS file for the application. Save the file with the name style.css. This file will include the styles and designs for various elements of the application, including the components of the carousel.

Below, I have provided the CSS script to style the carousel and other elements of the application. Feel free to customize the design according to the requirements of your project.

  1. html, body {
  2.     margin: unset;
  3.     padding: unset;
  4.     width: 100%;
  5.     max-width: 100%;
  6.     height: 100%;
  7.     max-height: 100%;
  8. }
  9.  
  10. body {
  11.     display: flex;
  12.     flex-flow: column;
  13.     align-items: center;
  14.     justify-content: center;
  15.     overflow: auto;
  16.     background-image: linear-gradient(to top, #cfd9df 0%, #e2ebf0 100%);
  17. }
  18.  
  19. #main-wrapper{
  20.     display: block;
  21.     width:100%;
  22.     max-width: 500px;
  23.     padding: 15px;
  24. }
  25. #page-title{
  26.     text-align: center;
  27.     font-weight: bolder;
  28. }
  29. .carousel-container{
  30.     position: relative;
  31.     width:100%;
  32.     max-width: 500px;
  33. }
  34.  
  35. .carousel{
  36.     position: relative;
  37.     width: 100%;
  38.     height: 300px;
  39.     overflow: hidden;
  40. }
  41.  
  42. .carousel-item{
  43.     position: absolute;
  44.     width: 100%;
  45.     height:100%;
  46.     display: none;
  47. }
  48. .carousel-item.active{
  49.     display: block;
  50. }
  51. .carousel-item.carousel-prev-transition-from,
  52. .carousel-item.carousel-prev-transition-to,
  53. .carousel-item.carousel-next-transition-from,
  54. .carousel-item.carousel-next-transition-to
  55. {
  56.     display: block;
  57.     transition: all .5s ease-in-out;
  58. }
  59. .carousel-item.carousel-prev-transition-to{
  60.     transform: translateX(-100%);
  61. }
  62.  
  63. .carousel-item.carousel-next-transition-to{
  64.     transform: translateX(100%);
  65. }
  66. .carousel-item img {
  67.     width: 100%;
  68.     height:100%;
  69.     object-fit: cover;
  70.     object-position: center center;
  71. }
  72.  
  73. .carousel_actions {
  74.     position: absolute;
  75.     display: flex;
  76.     width: 100%;
  77.     height: 100%;
  78.     justify-content: space-between;
  79.     top: 0;
  80. }
  81.  
  82. .carousel-btn {
  83.     height: 100%;
  84.     width: 50px;
  85.     font-size: 20px;
  86.     font-weight: 900;
  87.     color: #fff;
  88.     background: #00000008;
  89.     border: unset;
  90.     cursor: pointer;
  91. }
  92.  
  93. .carousel-btn:hover{
  94.     background: #0000001f;
  95. }

Step 3: Creating the JavaScript

Finally, let’s create the main JavaScript (JS) file for the application. Save the new file as script.js. This file will include the necessary code to make the carousel feature fully functional. Refer to the script provided below for implementation details.

  1. $(document).ready(function () {
  2.  
  3.     $('.carousel-container').each(function () {
  4.         var carousel = $(this)
  5.         // Carousel items
  6.         var items = carousel.find(".carousel .carousel-item");
  7.         // Carousel Next Slide Button
  8.         var next_btn = carousel.find(".carousel_actions .carousel-next-button")
  9.         // Carousel Previous Slide Button
  10.         var prev_btn = carousel.find(".carousel_actions .carousel-prev-button")
  11.  
  12.         // Event Listner when Next Slide Button has been triggerd
  13.         next_btn.click(function (e) {
  14.             e.preventDefault();
  15.             // current active slide
  16.             var currentSlide = carousel.find(".carousel .carousel-item.active")
  17.             var currentSlideIndx = currentSlide.index();
  18.             // Identifying next slide index
  19.             var nextSlideIndx = currentSlideIndx + 1;
  20.             if (nextSlideIndx == items.length)
  21.                 nextSlideIndx = 0;
  22.  
  23.             // Next Slide to Show
  24.             var nextSlide = $(items[nextSlideIndx]);
  25.  
  26.             // Animate Slide Transition from current active to the next slide
  27.             currentSlide.addClass("carousel-next-transition-from")
  28.             nextSlide.addClass("carousel-next-transition-to")
  29.             setTimeout(function () {
  30.                 nextSlide.css("transform", "translateX(0%)")
  31.                 currentSlide.css("transform", "translateX(-100%)")
  32.                 setTimeout(function () {
  33.                     currentSlide.removeClass("carousel-next-transition-from")
  34.                     currentSlide.removeClass("active")
  35.                     nextSlide.removeClass("carousel-next-transition-to")
  36.                     nextSlide.addClass("active")
  37.                     currentSlide.removeAttr("style")
  38.                     nextSlide.removeAttr("style")
  39.                 }
  40.                     , 1000)
  41.             }, 10)
  42.         })
  43.  
  44.         // Event Listner when Next Slide Button has been triggerd
  45.         prev_btn.click(function (e) {
  46.             e.preventDefault();
  47.             // current active slide
  48.             var currentSlide = carousel.find(".carousel .carousel-item.active")
  49.             var currentSlideIndx = currentSlide.index();
  50.             // Identifying previous slide index
  51.             var prevSlideIndx = currentSlideIndx - 1;
  52.             if (prevSlideIndx < 0)
  53.                 prevSlideIndx = (items.length - 1);
  54.  
  55.             var prevSlide = $(items[prevSlideIndx]);
  56.  
  57.             // Animate Slide Transition from current active to the next slide
  58.             currentSlide.addClass("carousel-prev-transition-from")
  59.             prevSlide.addClass("carousel-prev-transition-to")
  60.             setTimeout(function () {
  61.                 prevSlide.css("transform", "translateX(0%)")
  62.                 currentSlide.css("transform", "translateX(100%)")
  63.                 setTimeout(function () {
  64.                     currentSlide.removeClass("carousel-prev-transition-from")
  65.                     currentSlide.removeClass("active")
  66.                     prevSlide.removeClass("carousel-prev-transition-to")
  67.                     prevSlide.addClass("active")
  68.                     currentSlide.removeAttr("style")
  69.                     prevSlide.removeAttr("style")
  70.                 }
  71.                     , 1000)
  72.             }, 10)
  73.         })
  74.     })
  75.  
  76. })

And there you have it! Now you test the simple application with carousel that we created on your end and see if it works as we wanted.

Here's a screenshot of the application using the source code that I provided on this tutorial.

Step-by-Step Guide to Creating a Carousel with HTML, CSS, and JavaScript

I have also provided the complete source code zip file on this article and it is free to download. The download button is located below this article content. Feel free to download and modify the codes the way you wanted.

I hope this Step-by-Step Guide to Creating a Carousel with HTML, CSS, and JavaScript will help you and you'll find something useful from the source code for your future web application projects.

Explore more on this website for more Tutorials, Free Source Code, and Articles covering various programming languages.

Happy Coding =)

Add new comment