Skip to Content

How do I make a custom slideshow of pictures?

How do I make a custom slideshow of pictures?

Making a custom slideshow of your pictures is a great way to showcase your photos and share memories with friends and family. With some basic HTML knowledge, you can easily create an automated slideshow that runs through your image files in a customized sequence. This allows you to add transitions, set timing, include captions, and more to make your slideshow unique.

Gather and Prepare Your Image Files

The first step is to gather together all the photos you want to include in your slideshow. You’ll want the files to be in a digital format like JPG, PNG or GIF. It’s best if they are all the same size and resolution for consistency.

Organize the photos into a folder on your computer. Give them simple numeric filenames like “1.jpg”, “2.jpg”, “3.jpg” and so on to control the image order. You can rename batches of files quickly using a bulk renaming tool.

Optionally, you may want to edit or resize some of the photos. Basic image editing software lets you crop, rotate, resize and enhance photos if needed.

Write the HTML File

Now you’re ready to start coding the HTML file that will control the slideshow. This can be done in any text editor. Open a new file, save it with an “.html” extension and start with the basic structure:

<html>

<head>
</head>

<body>

</body>

</html>

In between the <head> tags, link to the jQuery library which we’ll use to add slideshow functionality:

<head>
  <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script> 
</head>

Add Slideshow Styles

Now add some CSS styling between <style> tags in the head section to style and position the slideshow.

For example:

<style>
  #slideshow {
    position: relative;
    width: 500px;
    height: 400px;
  }

  #slideshow IMG {
    position: absolute;
    top: 0;
    left: 0;
    z-index: 8;
  }

  #slideshow IMG.active {
    z-index: 10;
  }

  #slideshow IMG.last-active {
    z-index: 9;
  }
</style>  

This centers the slideshow container, sets dimensions and stacks images on top of each other based on their z-index.

Add Slideshow Container and Images

Inside the <body> section, create a <div> container to hold the slideshow. Give it an ID that matches the CSS selector above.

<div id="slideshow">

</div>

Next, add <img> tags for each of your photos. Set the SRC attribute to the image filenames. Optionally add a caption inside a <p> tag below each one.

<div id="slideshow">

  <img src="1.jpg" />
  <p>Caption text 1</p>

  <img src="2.jpg" />
  <p>Caption text 2</p>

  <img src="3.jpg" />

</div>

Add JavaScript Slideshow Code

Now for the dynamic functionality. Under the slideshow container div, add a <script> tag to contain the jQuery/JavaScript code.

First store a variable for the slideshow element:

var $slideshow = $('#slideshow');

Next set variables to track the active slide index and time between transitions:

var slideIndex = 0;
var slideInterval = 3000; //3 seconds

Then a function to advance to the next slide:

function nextSlide() {
  goToSlide(slideIndex+1);
}

And the goToSlide() function accepts the index to display:

function goToSlide(n) {
  // handle index wrap-around
  if (n = $slideshow.find('img').length) { 
    slideIndex = 0;
  } else {
    slideIndex = n;
  }

  // set active classes
  $slideshow.find('img').removeClass('active').addClass('last-active');
  $slideshow.find('img:eq('+slideIndex+')').removeClass('last-active').addClass('active');
}

Finally call the nextSlide() function on a timed loop:

  
setInterval(nextSlide, slideInterval);

The full script looks like this:

<script>

  var $slideshow = $('#slideshow'); 
  var slideIndex = 0;
  var slideInterval = 3000;
  
  function nextSlide() {
    goToSlide(slideIndex+1);
  }

  function goToSlide(n) {
    if (n = $slideshow.find('img').length) { 
      slideIndex = 0;
    } else {
      slideIndex = n;
    }

    $slideshow.find('img').removeClass('active').addClass('last-active');
    $slideshow.find('img:eq('+slideIndex+')').removeClass('last-active').addClass('active');
  }

  setInterval(nextSlide, slideInterval);

</script>

Customize the Transitions

To make the slideshow more visually interesting, you can add CSS transitions. This makes the images fade, slide or other effects during the change.

For a simple fade, add this transition style:

#slideshow IMG {
  transition: opacity 1s;
  opacity: 0;  
}

#slideshow IMG.active {
  opacity: 1;
} 

For a sliding transition, use transforms:

#slideshow IMG {
  transition: transform 0.5s ease-out;
  transform: translateX(-100%);
}

#slideshow IMG.last-active {
  transform: translateX(100%);  
}

#slideshow IMG.active {
  transform: translateX(0);
}

Play around with different transition durations, easing and transform values to get creative effects!

Add Navigation Buttons

You can optionally add previous and next buttons to control the slideshow manually:

<button id="prevSlide">Prev</button>
<button id="nextSlide">Next</button>

Then attach click handlers:

$('#prevSlide').click(function() {
  goToSlide(slideIndex-1);
});

$('#nextSlide').click(function() {
  goToSlide(slideIndex+1);
});

Putting It All Together

Here’s the full HTML and JavaScript source code for a working fade transition slideshow with controls:

<html>

<head>
  <style>
    #slideshow {
      position: relative;
      width: 500px;
      height: 400px; 
    }

    #slideshow IMG {
      position: absolute;
      top: 0;
      left: 0;
      transition: opacity 1s;
      opacity: 0;
    }

    #slideshow IMG.active {
      opacity: 1;
    }
  </style>
  
  <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>

</head>

<body>

  <div id="slideshow">
    <img src="1.jpg">
    <img src="2.jpg"> 
    <img src="3.jpg">
  </div>

  <button id="prevSlide">Prev</button>
  <button id="nextSlide">Next</button>

  <script>

    var $slideshow = $('#slideshow');
    var slideIndex = 0;
    var slideInterval = 3000;
    
    $('#prevSlide').click(function() {
      goToSlide(slideIndex-1);
    });

    $('#nextSlide').click(function() {
      goToSlide(slideIndex+1); 
    });
    
    function nextSlide() {
      goToSlide(slideIndex+1);
    }

    function goToSlide(n) {
      if (n = $slideshow.find('img').length) { 
        slideIndex = 0;
      } else {
        slideIndex = n;  
      }

      $slideshow.find('img').removeClass('active').addClass('last-active');
      $slideshow.find('img:eq('+slideIndex+')').removeClass('last-active').addClass('active');
    }

    setInterval(nextSlide, slideInterval);

  </script>

</body>
</html>

With just a bit of HTML, CSS and JavaScript you can create an automated, dynamic photo slideshow! The core functionality is there, but you can further customize the slide transitions and controls to get it looking just right.

Some ideas for enhancements:

  • Add fancy CSS3 transitions like rotations, scales, animations.
  • Include autoplay/pause toggle button.
  • Add thumbnail navigation dots.
  • Randomize or shuffle slide order on each run.
  • Support swipe gestures on touch devices.
  • Fetch images dynamically from a JSON source or database.

The possibilities are endless for making your slideshow stand out. Building it yourself with HTML, CSS and JavaScript gives you complete control over the behavior and presentation.

Now you have the fundamentals to make an awesome photo slideshow for your website! Adjust the code until it works just right, swap in your own pictures, and enjoy sharing your images with spectators.