Open In App

How to create image slider using HTML CSS and JavaScript ?

Last Updated : 22 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report
News Follow

An image slide, or slideshow, is a dynamic display of images that automatically transitions from one to the next, often with animations. To create an image slide, use HTML to structure the images, CSS for styling and animations, and JavaScript to control the timing and transitions between images.

Approach

  • HTML Structure: The HTML structure includes three image slides wrapped in div elements, along with left and right arrow controls for navigation.
  • CSS for Slide Styling: Each slide is styled with full viewport height, background images, and positioning, ensuring smooth transitions and visibility.
  • CSS for Arrow Controls: Arrow controls are styled using absolute positioning, with custom borders to create triangle-like arrows for navigation.
  • JavaScript Logic for Slide Initialization: The startSlide() function resets all slides and shows the first image as the initially visible slide when the page loads.
  • JavaScript Logic for Navigating Left: The slideLeft() function decrements the current index, shows the previous slide, and loops back to the last slide if necessary.
  • JavaScript Logic for Navigating Right: The slideRight() function increments the current index, displays the next slide, and loops back to the first slide when reaching the end.

Example: In this example, we are using the above-explained approach.

HTML
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <title>Image Slider</title>
    <meta name="viewport" content=
        "width=device-width, initial-scale=1" />
    <link rel="stylesheet" href=
"https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" />
    <script src=
        "https://code.jquery.com/jquery-3.6.0.min.js ">
    </script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
</head>

<body>
    <div id="arrow-left" class="arrow"></div>
    <div class="slide slide1"></div>
    <div class="slide slide2"></div>
    <div class="slide slide3"></div>
    <div id="arrow-right" class="arrow"></div>
</body>

</html>
CSS JavaScript

Output: Click here to see live code output

ezgif717cf202e3377

Next Article

Similar Reads

three90RightbarBannerImg