Multiple Modal Images Using Thumbnails Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Multiple Modal Images Using Thumbnails without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I’m using the W3 Schools modal example to display a modal of multiple image from clicking on one of them. It’s all working fine but some of the images are rectangular and the client wants square thumbnails.

I’m just getting back into JavaScript.

How do I adjust this code to display the full image from clicking on a thumbnail on the page (ex. images/image1thumb.jpg -> show images/image1.jpg in the modal).

var modal = document.getElementById('myModal');
// to all images -- note I'm using a class!
var images = document.getElementsByClassName('myImages');
// the image in the modal
var modalImg = document.getElementById("img01");
// and the caption in the modal
var captionText = document.getElementById("caption");

// Go through all of the images with our custom class
for (var i = 0; i < images.length; i++) {
  var img = images[i];
  // and attach our click listener for this image.
  img.onclick = function(evt) { = "block";
    modalImg.src = this.src;
    captionText.innerHTML = this.alt;

var span = document.getElementsByClassName("close")[0];

span.onclick = function() { = "none";
#myImg {
  border-radius: 5px;
  cursor: pointer;
  transition: 0.3s;

#myImg:hover {
  opacity: 0.7;

.modal {
  display: none;
  /* Hidden by default */
  position: fixed;
  /* Stay in place */
  z-index: 1;
  /* Sit on top */
  padding-top: 100px;
  /* Location of the box */
  left: 0;
  top: 0;
  width: 100%;
  /* Full width */
  height: 100%;
  /* Full height */
  overflow: auto;
  /* Enable scroll if needed */
  background-color: rgb(0, 0, 0);
  /* Fallback color */
  background-color: rgba(0, 0, 0, 0.9);
  /* Black w/ opacity */

.modal-content {
  margin: auto;
  display: block;
  width: 80%;
  max-width: 700px;

#caption {
  margin: auto;
  display: block;
  width: 80%;
  max-width: 700px;
  text-align: center;
  color: #ccc;
  padding: 10px 0;
  height: 150px;

#caption {
  animation-name: zoom;
  animation-duration: 0.6s;

@keyframes zoom {
  from {
    transform: scale(0)
  to {
    transform: scale(1)

.close {
  position: absolute;
  top: 15px;
  right: 35px;
  color: #f1f1f1;
  font-size: 40px;
  font-weight: bold;
  transition: 0.3s;

.close:focus {
  color: #bbb;
  text-decoration: none;
  cursor: pointer;

@media only screen and (max-width: 700px) {
  .modal-content {
    width: 100%;
<img class="myImages" id="myImg" src="" alt="Midnight sun in Lofoten, Norway" width="300" height="200">
<img class="myImages" id="myImg" src="" alt="Fishermen's cabins in Lofoten, Norway"
width="300" height="200">
<img class="myImages" id="myImg" src="" alt="Gerirangerfjord, Norway" width="300" height="200">
<div id="myModal" class="modal">
  <span class="close">&times;</span>
  <img class="modal-content" id="img01">
  <div id="caption"></div>


I would use CSS on the thumbnail images (regardless of what aspect ratio they are) to transform them into squares without squishing them, similar to this article:

We are here to answer your question about Multiple Modal Images Using Thumbnails - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji