Experience the powerful AI writing right inside WordPress
Show stunning before-and-after transformations with image sliders.
Improve user engagement by showing estimated reading time.
Written by Mahmuda Akter Isha
Showcase Designs Using Before After Slider.
To create an automatic image slider in JavaScript, add images inside a slider container, hide inactive slides with CSS, and use setInterval() in JavaScript to switch images automatically after a set time.
setInterval()
An automatic image slider is one of the most useful elements for websites, landing pages, portfolios, blogs, and product showcases. It lets you display multiple images in the same space and automatically changes the image after a set time.
The good news is that you do not need any heavy library or jQuery to create one. With simple HTML, CSS, and vanilla JavaScript, you can build a clean, responsive, and automatic image slider.
In this guide, you will learn how to create an automatic image slider in JavaScript step by step. You will also learn how to add autoplay, next and previous buttons, dots, hover pause, responsive styling, and common fixes.
An automatic image slider in JavaScript is a slideshow that changes images automatically after a specific time interval. It usually works by showing one image at a time and hiding the others.
JavaScript controls the active image and moves to the next image using functions like setInterval().
A basic automatic image slider usually includes:
An automatic image slider works in three simple steps:
For example, if you set the interval to 3000 milliseconds, the slider will change the image every 3 seconds.
The basic logic looks like this:
setInterval(nextSlide, 3000);
This means the nextSlide() function will run every 3 seconds.
nextSlide()
Now let’s create a simple automatic image slider using HTML, CSS, and JavaScript.
This version includes:
First, create the basic HTML structure for your image slider.
<div class="slider"> <div class="slide active"> <img src="image1.jpg" alt="Slide Image 1"> </div> <div class="slide"> <img src="image2.jpg" alt="Slide Image 2"> </div> <div class="slide"> <img src="image3.jpg" alt="Slide Image 3"> </div> <button class="prev" onclick="changeSlide(-1)">❮</button> <button class="next" onclick="changeSlide(1)">❯</button> </div> <div class="dots"> <span class="dot active" onclick="currentSlide(0)"></span> <span class="dot" onclick="currentSlide(1)"></span> <span class="dot" onclick="currentSlide(2)"></span> </div>
In this structure, each image is inside a slide div. The first slide has an extra active class, so it appears first when the page loads.
slide
active
Now add CSS to style the slider and hide inactive images.
* { box-sizing: border-box; } body { margin: 0; font-family: Arial, sans-serif; } .slider { position: relative; max-width: 900px; height: 500px; margin: 40px auto; overflow: hidden; border-radius: 12px; } .slide { display: none; width: 100%; height: 100%; } .slide.active { display: block; animation: fade 0.8s ease-in-out; } .slide img { width: 100%; height: 100%; object-fit: cover; } .prev, .next { position: absolute; top: 50%; transform: translateY(-50%); background: rgba(0, 0, 0, 0.5); color: white; border: none; padding: 14px 18px; cursor: pointer; font-size: 24px; border-radius: 50%; } .prev { left: 20px; } .next { right: 20px; } .prev:hover, .next:hover { background: rgba(0, 0, 0, 0.8); } .dots { text-align: center; margin-top: 15px; } .dot { height: 12px; width: 12px; margin: 0 5px; display: inline-block; background-color: #bbb; border-radius: 50%; cursor: pointer; } .dot.active { background-color: #333; } @keyframes fade { from { opacity: 0.4; } to { opacity: 1; } } @media screen and (max-width: 768px) { .slider { height: 300px; margin: 20px; } .prev, .next { padding: 10px 14px; font-size: 18px; } }
This CSS makes the image slider responsive and visually clean. The active class controls which image is visible.
Now add JavaScript to make the slider change images automatically.
let slideIndex = 0; const slides = document.querySelectorAll(".slide"); const dots = document.querySelectorAll(".dot"); function showSlide(index) { slides.forEach((slide) => { slide.classList.remove("active"); }); dots.forEach((dot) => { dot.classList.remove("active"); }); if (index >= slides.length) { slideIndex = 0; } if (index < 0) { slideIndex = slides.length - 1; } slides[slideIndex].classList.add("active"); dots[slideIndex].classList.add("active"); } function changeSlide(step) { slideIndex += step; showSlide(slideIndex); } function currentSlide(index) { slideIndex = index; showSlide(slideIndex); } function autoSlide() { slideIndex++; showSlide(slideIndex); } setInterval(autoSlide, 3000);
This JavaScript code changes the image every 3 seconds. It also lets users click the next button, previous button, or dots to change slides manually.
Here is the complete code in one place.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Automatic Image Slider in JavaScript</title> <style> * { box-sizing: border-box; } body { margin: 0; font-family: Arial, sans-serif; } .slider { position: relative; max-width: 900px; height: 500px; margin: 40px auto; overflow: hidden; border-radius: 12px; } .slide { display: none; width: 100%; height: 100%; } .slide.active { display: block; animation: fade 0.8s ease-in-out; } .slide img { width: 100%; height: 100%; object-fit: cover; } .prev, .next { position: absolute; top: 50%; transform: translateY(-50%); background: rgba(0, 0, 0, 0.5); color: white; border: none; padding: 14px 18px; cursor: pointer; font-size: 24px; border-radius: 50%; } .prev { left: 20px; } .next { right: 20px; } .prev:hover, .next:hover { background: rgba(0, 0, 0, 0.8); } .dots { text-align: center; margin-top: 15px; } .dot { height: 12px; width: 12px; margin: 0 5px; display: inline-block; background-color: #bbb; border-radius: 50%; cursor: pointer; } .dot.active { background-color: #333; } @keyframes fade { from { opacity: 0.4; } to { opacity: 1; } } @media screen and (max-width: 768px) { .slider { height: 300px; margin: 20px; } .prev, .next { padding: 10px 14px; font-size: 18px; } } </style> </head> <body> <div class="slider"> <div class="slide active"> <img src="image1.jpg" alt="Slide Image 1"> </div> <div class="slide"> <img src="image2.jpg" alt="Slide Image 2"> </div> <div class="slide"> <img src="image3.jpg" alt="Slide Image 3"> </div> <button class="prev" onclick="changeSlide(-1)">❮</button> <button class="next" onclick="changeSlide(1)">❯</button> </div> <div class="dots"> <span class="dot active" onclick="currentSlide(0)"></span> <span class="dot" onclick="currentSlide(1)"></span> <span class="dot" onclick="currentSlide(2)"></span> </div> <script> let slideIndex = 0; const slides = document.querySelectorAll(".slide"); const dots = document.querySelectorAll(".dot"); function showSlide(index) { slides.forEach((slide) => { slide.classList.remove("active"); }); dots.forEach((dot) => { dot.classList.remove("active"); }); if (index >= slides.length) { slideIndex = 0; } if (index < 0) { slideIndex = slides.length - 1; } slides[slideIndex].classList.add("active"); dots[slideIndex].classList.add("active"); } function changeSlide(step) { slideIndex += step; showSlide(slideIndex); } function currentSlide(index) { slideIndex = index; showSlide(slideIndex); } function autoSlide() { slideIndex++; showSlide(slideIndex); } setInterval(autoSlide, 3000); </script> </body> </html>
You can change the autoplay speed by editing the setInterval() value.
setInterval(autoSlide, 3000);
Here, 3000 means 3000 milliseconds, or 3 seconds.
3000
For example:
setInterval(autoSlide, 5000);
This will change the image every 5 seconds.
Sometimes users want to stop the slider while hovering over it. This improves user experience because visitors can look at the image without it changing too quickly.
Use this JavaScript:
let slideInterval = setInterval(autoSlide, 3000); const slider = document.querySelector(".slider"); slider.addEventListener("mouseenter", () => { clearInterval(slideInterval); }); slider.addEventListener("mouseleave", () => { slideInterval = setInterval(autoSlide, 3000); });
This pauses the slider when the mouse enters the slider area and starts it again when the mouse leaves.
To make your image slider responsive, use percentage-based widths and media queries.
Example:
.slider { width: 100%; max-width: 900px; height: 500px; } .slide img { width: 100%; height: 100%; object-fit: cover; } @media screen and (max-width: 768px) { .slider { height: 300px; } }
The object-fit: cover; property keeps the image properly fitted inside the slider without stretching it.
object-fit: cover;
You can add captions inside each slide like this:
<div class="slide active"> <img src="image1.jpg" alt="Slide Image 1"> <div class="caption">Beautiful Nature View</div> </div>
Then add this CSS:
.caption { position: absolute; bottom: 30px; left: 30px; color: white; background: rgba(0, 0, 0, 0.5); padding: 10px 16px; border-radius: 6px; font-size: 18px; }
Captions are useful for banners, portfolios, product showcases, and travel websites.
A vanilla JavaScript image slider is lightweight and does not require any external library. That means your website can load faster and stay easier to maintain.
You should use vanilla JavaScript when:
For small websites, blogs, portfolios, and simple landing pages, a vanilla JavaScript slider is usually enough.
You can create sliders using both JavaScript and CSS. However, each method has different benefits.
If you need more control, JavaScript is the better choice. If you want a very simple animation, CSS may be enough.
This usually happens when the inactive slides are not hidden.
Make sure you have this CSS:
.slide { display: none; } .slide.active { display: block; }
Check whether your JavaScript file is properly connected. Also make sure setInterval() is added correctly.
Make sure the first slide has the active class.
<div class="slide active"> <img src="image1.jpg" alt="Slide Image 1"> </div>
Check whether the button function names match your JavaScript function names.
<button onclick="changeSlide(-1)">Previous</button> <button onclick="changeSlide(1)">Next</button>
Make sure the number of dots matches the number of slides. If you have 4 images, you need 4 dots.
Use width: 100%;, max-width, and media queries. Also use object-fit: cover; for images.
width: 100%;
max-width
To make your JavaScript image slider better, follow these best practices:
A slider should improve your website design, not slow it down.
If you are using WordPress, writing custom HTML, CSS, and JavaScript may not always be the best option. Beginners often prefer using a slider plugin because it saves time and reduces coding errors.
With a WordPress slider plugin, you can usually:
This is helpful for users who want to add sliders to blog posts, pages, WooCommerce products, portfolios, or homepage banners without editing code manually.
You should use a custom JavaScript slider when you want full control over the design and functionality.
It is a good choice for:
However, if you use WordPress and want a faster setup, a plugin can be easier.
Creating an automatic image slider in JavaScript is simple when you understand the basic logic. You need HTML for the structure, CSS for the design, and JavaScript for the autoplay functionality.
The main idea is to show one image at a time, hide the others, and use JavaScript to move to the next image automatically.
You can also improve your slider by adding next and previous buttons, dots, captions, fade effects, hover pause, and responsive design.
Whether you are building a portfolio, blog, landing page, or product showcase, an automatic image slider can make your website more interactive and visually attractive.
You can create an automatic image slider by placing images inside a slider container, hiding inactive images with CSS, and using JavaScript setInterval() to change the active image automatically.
Yes. You can create an image slider using only HTML, CSS, and vanilla JavaScript. You do not need jQuery for a basic automatic slider.
Use the JavaScript setInterval() function to call your next slide function after a specific time.Example:setInterval(autoSlide, 3000);
Create two buttons in HTML and connect them to a JavaScript function that increases or decreases the slide index.Example:<button onclick="changeSlide(-1)">Previous</button> <button onclick="changeSlide(1)">Next</button>
You can pause the slider by using clearInterval() when the mouse enters the slider area and restarting setInterval() when the mouse leaves.
clearInterval()
Use width: 100%;, max-width, flexible image sizes, and media queries. Also use object-fit: cover; to keep images properly fitted inside the slider.
An image slider usually shows one image at a time and changes automatically or manually. A carousel can show multiple items at once and often includes more advanced scrolling behavior.
This page was last edited on 8 July 2026, at 6:04 pm
Your email address will not be published. Required fields are marked *
Comment *
Name *
Email *
Website
Save my name, email, and website in this browser for the next time I comment.
How many people work in your company?Less than 1010-5050-250250+
By proceeding, you agree to our Privacy Policy