\n\n\n 🔍 Explanation: slider-container: A wrapper that holds the image and buttons. img: Displays the current image. prev and next: Buttons to go to the previous and next images. JavaScript file script.js is linked at the bottom of the page for interactivity. 🎨 Step 2: Style the Slider with CSS Create a file named style.css and add the following code: body {\n font-family: Arial, sans-serif;\n text-align: center;\n margin: 0;\n padding: 0;\n background-color: #f5f5f5;\n}\n\n.slider-container {\n position: relative;\n max-width: 600px;\n margin: 50px auto;\n}\n\n#slider-image {\n width: 100%;\n height: auto;\n border-radius: 10px;\n}\n\nbutton {\n position: absolute;\n top: 50%;\n transform: translateY(-50%);\n background-color: rgba(0, 0, 0, 0.5);\n color: white;\n border: none;\n padding: 10px 15px;\n font-size: 24px;\n cursor: pointer;\n border-radius: 50%;\n user-select: none;\n}\n\n#prev {\n left: 10px;\n}\n\n#next {\n right: 10px;\n}\n\nbutton:hover {\n background-color: rgba(0, 0, 0, 0.8);\n}\n 🔍 Explanation: The container is centered and responsive. Buttons are positioned on the left and right of the image. transform: translateY(-50%) centers the button vertically. Hover effect improves the user experience. 🧠 Step 3: Add JavaScript Logic Create a file named script.js and add the following code: // List of image sources\nconst images = [\"image1.jpg\", \"image2.jpg\", \"image3.jpg\"];\nlet currentIndex = 0;\n\n// Get DOM elements\nconst sliderImage = document.getElementById(\"slider-image\");\nconst prevButton = document.getElementById(\"prev\");\nconst nextButton = document.getElementById(\"next\");\n\n// Function to update the image\nfunction updateImage() {\n sliderImage.src = images[currentIndex];\n}\n\n// Event listener for Previous button\nprevButton.addEventListener(\"click\", () => {\n currentIndex--;\n if (currentIndex < 0) {\n currentIndex = images.length - 1; // Go to last image\n }\n updateImage();\n});\n\n// Event listener for Next button\nnextButton.addEventListener(\"click\", () => {\n currentIndex++;\n if (currentIndex >= images.length) {\n currentIndex = 0; // Go back to first image\n }\n updateImage();\n});\n 🔍 Explanation: images[]: An array holding your image file names. currentIndex: Keeps track of which image is currently shown. updateImage(): Updates the src of the img element. If the current index goes below 0 or above the last image, it loops around to make the slider circular. 📝 Important Notes: Image Paths: Make sure image1.jpg, image2.jpg, etc., are in the same folder as your HTML file or update the paths accordingly. Responsive: The slider is responsive by default and will scale with screen size. More Features: You can later add autoplay, dot indicators, or transitions to enhance it. ✅ Final Thoughts Now you’ve created a fully functional image slider with Next and Previous buttons using just HTML, CSS, and JavaScript. This project helps you understand the basics of DOM manipulation, event listeners, and dynamic content updates. It’s also a great beginner project to include in your portfolio! 📁 File Structure Overview project-folder/\n│\n├── index.html\n├── style.css\n├── script.js\n├── image1.jpg\n├── image2.jpg\n└── image3.jpg","@type":"NewsArticle","author":[{"@type":"Person","name":"ttimesnow","url":"https://www.ttimesnow.com/author/11"}],"description":"Learn step-by-step how to create an image slider using JavaScript with next and previous buttons. Perfect for beginners with easy explanations and code examples.","inLanguage":"en","dateModified":"2025-05-16T02:34:38.143Z","mainEntityOfPage":{"@type":"WebPage","@id":"https://www.ttimesnow.com/how-to-build-a-simple-javascript-image-slider-with-navigation-buttons"},"url":"https://www.ttimesnow.com/amp/how-to-build-a-simple-javascript-image-slider-with-navigation-buttons","datePublished":"2025-05-16T02:34:38.143Z","publisher":{"@type":"Organization","name":"TtimesNow","logo":{"@type":"ImageObject","width":180,"url":"https://www.ttimesnow.com/apple-touch-icon.png","height":180},"@id":"https://www.ttimesnow.com#organization","url":"https://www.ttimesnow.com","sameAs":["https://www.facebook.com/donotbelazymotivation"]},"@id":"https://www.ttimesnow.com/how-to-build-a-simple-javascript-image-slider-with-navigation-buttons#article","headline":"How to Build a Simple JavaScript Image Slider with Navigation Buttons","articleSection":"javascript","thumbnailUrl":"https://www.ttimesnow.com/view/gk/free-courses-to-learn-javascript.webp"}],"@context":"https://schema.org"}
Google Advertisement

How to Build a Simple JavaScript Image Slider with Navigation Buttons

Google Advertisement
🔥 Read with Full Features on Our Website

Learn step-by-step how to create an image slider using JavaScript with next and previous buttons. Perfect for beginners with easy explanations and code examples.

Published on 16 May 2025
By ttimesnow

🖼️ Create an Image Slider with Next/Prev Buttons in JavaScript

Image sliders are a great way to showcase multiple images in a limited space. In this tutorial, you will learn how to create a simple image slider with Next and Previous buttons using HTML, CSS, and JavaScript.

🔥 Read with Full Features on Our Website

This project is perfect for beginners who want to learn how JavaScript interacts with HTML and CSS.


🚀 What You Will Learn


🧱 Step 1: Create the HTML Structure

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Simple Image Slider</title>
  <link rel="stylesheet" href="style.css">
</head>
<body>
  <div class="slider-container">
    <button id="prev">❮</button>
    <img id="slider-image" src="image1.jpg" alt="Slider Image">
    <button id="next">❯</button>
  </div>

  <script src="script.js"></script>
</body>
</html>

🔍 Explanation:


🎨 Step 2: Style the Slider with CSS

Create a file named style.css and add the following code:

body {
  font-family: Arial, sans-serif;
  text-align: center;
  margin: 0;
  padding: 0;
  background-color: #f5f5f5;
}

.slider-container {
  position: relative;
  max-width: 600px;
  margin: 50px auto;
}

#slider-image {
  width: 100%;
  height: auto;
  border-radius: 10px;
}

button {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  background-color: rgba(0, 0, 0, 0.5);
  color: white;
  border: none;
  padding: 10px 15px;
  font-size: 24px;
  cursor: pointer;
  border-radius: 50%;
  user-select: none;
}

#prev {
  left: 10px;
}

#next {
  right: 10px;
}

button:hover {
  background-color: rgba(0, 0, 0, 0.8);
}

🔍 Explanation:


🧠 Step 3: Add JavaScript Logic

Create a file named script.js and add the following code:

// List of image sources
const images = ["image1.jpg", "image2.jpg", "image3.jpg"];
let currentIndex = 0;

// Get DOM elements
const sliderImage = document.getElementById("slider-image");
const prevButton = document.getElementById("prev");
const nextButton = document.getElementById("next");

// Function to update the image
function updateImage() {
  sliderImage.src = images[currentIndex];
}

// Event listener for Previous button
prevButton.addEventListener("click", () => {
  currentIndex--;
  if (currentIndex < 0) {
    currentIndex = images.length - 1; // Go to last image
  }
  updateImage();
});

// Event listener for Next button
nextButton.addEventListener("click", () => {
  currentIndex++;
  if (currentIndex >= images.length) {
    currentIndex = 0; // Go back to first image
  }
  updateImage();
});

🔍 Explanation:


📝 Important Notes:

  1. Image Paths: Make sure image1.jpg, image2.jpg, etc., are in the same folder as your HTML file or update the paths accordingly.

  2. Responsive: The slider is responsive by default and will scale with screen size.

  3. More Features: You can later add autoplay, dot indicators, or transitions to enhance it.


✅ Final Thoughts

Now you’ve created a fully functional image slider with Next and Previous buttons using just HTML, CSS, and JavaScript. This project helps you understand the basics of DOM manipulation, event listeners, and dynamic content updates.

It’s also a great beginner project to include in your portfolio!


📁 File Structure Overview

project-folder/
│
├── index.html
├── style.css
├── script.js
├── image1.jpg
├── image2.jpg
└── image3.jpg
❤️ Like 💬 Comment 🔗 Share
Google Advertisement
👉 View Full Version on Main Website ↗
Google Advertisement
👉 Read Full Article on Website