Javatpoint Logo
Javatpoint Logo

Automatic Image Slider in HTML & CSS

Image sliders are a well-liked method in web development for continuously and captivatingly displaying multiple images or material. A slider that alternates images automatically is called an automatic image slider because it doesn't need user input. This tutorial will utilize HTML and CSS to create an automatic picture slider.

Getting Started

Before we begin, we should ensure you have a basic understanding of HTML and CSS. We will also be using a bit of JavaScript for the automatic transition.

HTML Structure

Let's start by setting up the basic HTML structure for our slider.


There's a div with the class slider within another div with the class slides in this HTML layout. Every div element with the class slide and an image is contained inside the slide div.

CSS Styling

Next, let's add some basic CSS to style our slider. Create a file named styles.css and add the following code.


We're setting up our slider's basic styling with this CSS code. The slider is centred on the page thanks to Flexbox, and the slides and photos also have some styles applied to them.

JavaScript for Automatic Transition

Now, let's add the JavaScript code to enable automatic transitioning between the slides. Add the following code to a script.js file that has been created.


In this JavaScript code, we use the setInterval function to call the nextSlide function every 3 seconds. The nextSlide function increments the slideIndex and calls showSlide to display the next slide.


Automatic Image Slider in HTML and CSS
Automatic Image Slider in HTML and CSS
Automatic Image Slider in HTML and CSS

Advantages of an Automatic Image Slider

  1. Engaging User Experience: Image sliders are an engaging and interactive means of showcasing products, services or content.
  2. Space Efficiency: They are perfect for landing pages or homepages with little real estate since they let you present numerous pieces of content in a limited space.
  3. Versatility: Image sliders can be used to showcase aspects of products, user reviews, portfolio pieces or announcements, among other things.
  4. Visual Appeal: A website might have a more visually pleasing visual appeal with well-designed sliders with high-quality images.
  5. Mobile Responsiveness: Image sliders may be made responsive by the right design and coding, providing that they work correctly on various screens and devices.

Disadvantages of an Automatic Image Slider

  1. Accessibility Concerns: Image sliders can be problematic for users with disabilities, especially if the slides transition too quickly or if there are no accessible controls.
  2. Banner Blindness: Users may develop banner blindness, where they subconsciously ignore or overlook the slider because they associate it with advertisements.
  3. Performance Impact: If not optimized, the image sliders can lead to slower page load times, mainly if large images are used.
  4. Information Overload: If too many slides are included, users may not have enough time to absorb the content on each slide, potentially leading to cognitive overload.
  5. Lack of Interaction: Automatic sliders don't allow users to control the slides' pace or sequence, potentially frustrating those who want to explore content at their own pace.
  6. Inconsistent Engagement: Different users may have varying attention spans, so an automatic slider might not provide an optimal viewing experience for everyone.
  7. Complexity in Development: Building a custom automatic image slider can be more complex than using pre-built plugins, especially for developers less experienced with JavaScript.
  8. Performance on Mobile Devices: While image sliders can be responsive, they may not always perform optimally on specific mobile Especially if the browser or device has limitations.


While automatic image sliders can be a powerful tool for showcasing content, they should be used judiciously and with consideration for accessibility and user experience to ensure they enhance rather than hinder the effectiveness of a website.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA