Responsive Image Slider

How to Create a Responsive Image Slider with HTML, CSS, and JavaScript

In this tutorial, you’ll learn how to create a fully responsive image slider using HTML, CSS, and JavaScript. We’ll walk through the steps to build a modern slider that automatically transitions between slides and adapts to any screen size. Perfect for your website’s galleries, portfolios, or featured images!

🔔 Subscribe for more web development tutorials: https://youtube.com/@codewithubes

📋 Steps Covered in This Video:

  • HTML structure for the slider
  • Adding CSS to style the image slider
  • Writing JavaScript to add functionality
  • Making the slider responsive on all devices

HTML:

Create an HTML file and add the following code:

<div class="container swiper">
        <div class="card-wrapper">
            <ul class="card-list swiper-wrapper">
                <li class="card-item swiper-slide">
                    <a href="#" class="card-link">
                        <img src="https://codewithubes.in/cloud-images/mountain.jpg" class="card-image">
                        <p class="badge">Mountain</p>
                        <h2 class="card-title">An image slider is a web element that displays multiple images in a rotating format, allowing users to navigate through visuals using arrows or indicators.</h2>
                        <button class="card-button"><i class="fa-solid fa-arrow-right"></i></button>
                    </a>
                </li>
                <li class="card-item swiper-slide">
                    <a href="#" class="card-link">
                        <img src="https://codewithubes.in/cloud-images/laptop.jpg" class="card-image">
                        <p class="badge">Laptop</p>
                        <h2 class="card-title">An image slider is a web element that displays multiple images in a rotating format, allowing users to navigate through visuals using arrows or indicators.</h2>
                        <button class="card-button"><i class="fa-solid fa-arrow-right"></i></button>
                    </a>
                </li>
                <li class="card-item swiper-slide">
                    <a href="#" class="card-link">
                        <img src="https://codewithubes.in/cloud-images/lake.jpg" class="card-image">
                        <p class="badge">Lake</p>
                        <h2 class="card-title">An image slider is a web element that displays multiple images in a rotating format, allowing users to navigate through visuals using arrows or indicators.</h2>
                        <button class="card-button"><i class="fa-solid fa-arrow-right"></i></button>
                    </a>
                </li>
                <li class="card-item swiper-slide">
                    <a href="#" class="card-link">
                        <img src="https://codewithubes.in/cloud-images/river.jpg" class="card-image">
                        <p class="badge">River</p>
                        <h2 class="card-title">An image slider is a web element that displays multiple images in a rotating format, allowing users to navigate through visuals using arrows or indicators.</h2>
                        <button class="card-button"><i class="fa-solid fa-arrow-right"></i></button>
                    </a>
                </li>
                <li class="card-item swiper-slide">
                    <a href="#" class="card-link">
                        <img src="https://codewithubes.in/cloud-images/pin-on-board.jpg" class="card-image">
                        <p class="badge">Pin on board</p>
                        <h2 class="card-title">An image slider is a web element that displays multiple images in a rotating format, allowing users to navigate through visuals using arrows or indicators.</h2>
                        <button class="card-button"><i class="fa-solid fa-arrow-right"></i></button>
                    </a>
                </li>
            </ul>

            <div class="swiper-pagination"></div>
            <div class="swiper-slide-button swiper-button-prev"></div>
            <div class="swiper-slide-button swiper-button-next"></div>
        </div>
    </div>

CSS:

Create a CSS file and add the following code:

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

body{
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    background-image: linear-gradient(120deg, #84fab0 0%, #CCCCCC 100%);
}

.card-wrapper{
    max-width: 1100px;
    margin: 0 60px 35px;
    padding: 20px 10px;
    overflow: hidden;
}

.card-list .card-item{
    list-style: none;
}

.card-list .card-item .card-link{
    user-select: none;
    display: block;
    background: #fff;
    padding: 18px;
    border-radius: 12px;
    text-decoration: none;
    border: 2px solid transparent;
    box-shadow: 0 10px 10px rgba(0, 0, 0, 0.05);
    transition: 0.2s ease;
}

.card-list .card-item .card-link:active{
    cursor: grabbing;
}

.card-list .card-item .card-link:hover{
    border-color: #5372f0;
}

.card-list .card-link .card-image{
    width: 100%;
    aspect-ratio: 16 / 9;
    object-fit: cover;
    border-radius: 10px;
}

.card-list .card-link .badge{
    color: blue;
    margin: 16px 0 18px;
    padding: 8px 16px;
    font-weight: 500;
    font-size: 0.95rem;
    background: #dde4ff;
    width: fit-content;
    border-radius: 50px;
}

.card-list .card-link .card-title{
    font-size: 16px;
    color: #000;
    font-weight: 100;
}

.card-list .card-link .card-button{
    height: 35px;
    width: 35px;
    color: #5372f0;
    border-radius: 50%;
    margin: 30px 0 5px;
    background: none;
    cursor: pointer;
    transform: rotate(-45deg);
    border: 2px solid #5382f0;
    transition: 0.4s ease;
}

.card-list .card-link:hover .card-button{
    color: #fff;
    background: #5372f0;
}

.card-wrapper .swiper-pagination-bullet{
    height: 13px;
    width: 13px;
    opacity: 0.5;
    background: #5372f0;
}

.card-wrapper .swiper-pagination-bullet-active{
    opacity: 1;
}

.card-wrapper .swiper-slide-button{
    color: #5372f0;
    margin-top: -35px;
} 

@media screen and (max-width: 768px){
    .card-wrapper{
        margin: 0 10px 25px;
    }

    .card-wrapper .swiper-slide-button{
        display: none;
    }
}

JavaScript:

Create a  JavaScript file and add the following code:

new Swiper('.card-wrapper', {  
  loop: true,  
  speed: 700,  
  spaceBetween: 30,  

  // If we need pagination  
  pagination: {  
    el: '.swiper-pagination',  
    clickable: true,  
    dynamicBullets: true,  
  },  

  // Navigation arrows  
  navigation: {  
    nextEl: '.swiper-button-next',  
    prevEl: '.swiper-button-prev',  
  },  
  
  breakpoints: { 
    0: {  
      slidesPerView: 1  
    },  
    768: {  
      slidesPerView: 2  
    },  
    1024: {  
      slidesPerView: 3  
    },  
  }  
});  

#ResponsiveSlider #ImageSlider #HTMLCSSJS #WebDevelopment #JavaScriptTutorial #CSS #ResponsiveDesign #WebDesign #FrontendDevelopment #SliderTutorial #Tutorial

Happy coding!

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *