Skip to main content

How to Make a Slider Using HTML and CSS

In this tutorial, we'll guide you through the process of building a slider from scratch, step by step. We'll cover everything you need to know, from setting up the basic HTML structure to adding stunning CSS animations and transitions. By the end, you'll have a fully functional slider that will impress your website visitors and elevate their browsing experience. So, let's dive in and learn how to create an eye-catching slider that will make your website stand out from the crowd!

This is your HTML for Slider:

<div class="slider">
  <div class="slides">
    <div id="slide-1">
      <img src="https://try.siddharthkamra.in/example/green-iceland.jpg" alt="Slider 1 Image">
      <div class="author-info">This is First Slider</div>
    </div>
    <div id="slide-2">
      <img src="https://try.siddharthkamra.in/example/northern-lights.jpg" alt="Slider 2 Image">
      <div class="author-info">This is Second Slider</div>
    </div>
    <div id="slide-3">
      <img src="https://try.siddharthkamra.in/example/island.jpg" alt="Slider 3 Image">
      <div class="author-info">This is Third Slider</div>
    </div>
    <div id="slide-4">
      <img src="https://try.siddharthkamra.in/example/sunset.jpg" alt="Slider 4 Image">
      <div class="author-info">This is Fourth Slider</div>
    </div>
  </div><!-- Closing of .slides -->
</div><!-- Closing of .slider -->

This is your CSS for Slider:

  .slider {
    width: 100%;
    text-align: center;
    overflow: hidden;
  }

  .slides {
    display: flex;
    overflow-x: auto;
    scroll-snap-type: x mandatory;
    scroll-behavior: smooth;
    -webkit-overflow-scrolling: touch;
    border-radius: 5px;
  }

  .slides::-webkit-scrollbar {
    width: 0px;
    height: 0px;
  }
  
  .slides::-webkit-scrollbar-thumb {
    background: transparent;
  }
  
  .slides::-webkit-scrollbar-track {
    background: transparent;
  }
  
  .slides>div {
    scroll-snap-align: start;
    flex-shrink: 0;
    width: 100%;
    min-height: 300px;
    max-height: 600px;
    margin-right: 50px;
    border-radius: 10px;
    background: #eee;
    transform-origin: center center;
    transform: scale(1);
    transition: transform 0.8s;
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 15px;
  }

  .slides>div:target {
    /*   transform: scale(0.8); */
  }

  .author-info {
    background: rgba(0, 0, 0, 0.75);
    color: white;
    padding: 0.75rem;
    text-align: center;
    position: relative;
    bottom: -130;
    left: 0;
    width: 100%;
    margin: 0;
    border-radius: 0px 0px 5px 5px;
  }

  .author-info a {
    color: white;
  }

  img {
    object-fit: cover;
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    border-radius: 5px;
  }

Congratulations! You've successfully created a beautiful and dynamic slider using HTML and CSS. You've learned how to structure the HTML elements, apply CSS styles, and add smooth transitions and animations to create an engaging user experience. Feel free to experiment with different styles, colors, and content to make the slider truly unique to your website. Don't forget to optimize it for responsiveness, ensuring that it looks great on all devices and screen sizes.

Thank you for joining us on this journey, and we can't wait to see the stunning sliders you'll create for your websites. Happy coding!

Comments