You can use a lightbox to do it.

There is a simple example in this link:

http://lokeshdhakar.com/projects/lightbox2/

Answer from Jack on Stack Overflow
🌐
W3Schools
w3schools.com › howto › howto_css_modal_images.asp
How To Create Modal Images
Learn how to create responsive Modal Images with CSS and JavaScript. A modal is a dialog box/popup window that is displayed on top of the current page. This example use most of the code from the previous example, Modal Boxes, only in this example, we use images.
🌐
Unc
opal.ils.unc.edu › ~lblakej › website-helps › 04-image-popup
Image Popup Example
Click image to popup the larger image with the alt text as the caption. Adapted from this example at W3 Schools. Popups are often called a "Modal." To see the code and instructions on the steps you need to take to get this to work, right-click and view page source.
🌐
HubSpot
community.hubspot.com › t5 › Blog-Website-Page-Publishing › Need-to-create-a-popup-on-click-of-an-image › m-p › 808975
Solved: HubSpot Community - Need to create a popup on click of an image - HubSpot Community
July 4, 2023 - Some are just plain HTML+CSS but most of them take advantage of some sort of JavaScript (libraries). Personally I use bootstrap since I implement it in all of my themes and why re-invent the wheel? If you're already using some sort of framework - take a look at the documentation, maybe there are already pop-ups build in. For this example I'll go with the bootstrap approach
🌐
CodePen
codepen.io › Muhnad › pen › dMbXNb
image popup
$(function () { "use strict"; $(".popup img").click(function () { var $src = $(this).attr("src"); $(".show").fadeIn(); $(".img-show img").attr("src", $src); }); $("span, .overlay").click(function () { $(".show").fadeOut(); }); });
🌐
Bqardi
bqardi.dk › tutorials › js › image-popup › index.php
Image Popup
let popupBackground = document.querySelector("#popup-background"); let popupTitle = document.querySelector("#popup-title"); let popupImage = document.querySelector("#popup-image"); Now we need to add event listeners to all the thumbnails, so we can show our popup with the correct content when ...
🌐
Willmaster
willmaster.com › library › features › image-popup.php
Image Popup
Click thumbnail. Get larger-image popup centered on page. Click anywhere to close popup.
🌐
insideTheDiv
insidethediv.com › onlick-popup-full-size-image-in-jquery
Click on the image to view full size in a popup modal
Var src_value = “my-image.jpg”; $("#modal_img ").attr('src',src_value); After clicking on the thumbnail image, we need to make the image active by adding an active class. So that you can understand which thumbnail image is currently viewed.
Find elsewhere
🌐
W3Schools
w3schools.com › howto › howto_js_popup.asp
How To Create Popups
Create a Website Make a Website Make a Static Website Host a Static Website Make a Website (W3.CSS) Make a Website (BS3) Make a Website (BS4) Make a Website (BS5) Create and View a Website Create a Link Tree Website Create a Portfolio Create a Resume Make a Restaurant Website Make a Business Website Make a WebBook Center Website Contact Section About Page Big Header Example Website · 2 Column Layout 3 Column Layout 4 Column Layout Expanding Grid List Grid View Mixed Column Layout Column Cards Zig Zag Layout Blog Layout · Google Charts Google Fonts Google Font Pairings Google Set up Analytics · Convert Weight Convert Temperature Convert Length Convert Speed · Get a Developer Job Become a Front-End Dev. Hire Developers ... Learn how to create popups with CSS and JavaScript. Click me to toggle the popup!
🌐
Groupthought
help.groupthought.com › pipeline documentation › tutorials › how to show an image in a pop-up window
How to show an image in a pop-up window - Pipeline Documentation
<a class="image-link" href="https://cdn.shopify.com/s/files/1/2018/8867/files/matteo-paganelli-39971_530x.jpg">Open popup</a> You can add as many links as you like, make sure you use the class " image-link" in order for the pop-up/modal to work. ... These URL's are the full size image.
🌐
GeeksforGeeks
geeksforgeeks.org › html › how-to-show-images-on-click-using-html
How to Show Images on Click using HTML ? - GeeksforGeeks
<!DOCTYPE html> <html> <head> <title> ... img element without src attribute --> <img id="image" src="" /> </div> <button type="button" onclick="show()" id="btnID"> Show Image </button> <script> function show() { /* Get image and ...
Published   January 10, 2025
Top answer
1 of 2
1

The span elements should be completely removed and its classes placed on the image elements themselves.

Also, you have a nested section element that isn't doing anything for you.

Lastly, do not use HTML heading elements (<h1>...<h6>) because of the way they style the text. Formatting is the job of CSS. Instead of headings, it is more appropriate to surround each image and its accompanying text with figure and figcaption elements.

img { 
  width:200px;
  border:1px solid black; /* This is only added for testing purposes*/
}

.thumbnail:hover {
   width: 500px;
   height:auto;
   position:relative;
   /* push image to the right by 1/2 the screen width and 1/2 the image width */
   margin-left:calc(50% - 250px);
}
<section id="main">
  <div class="inner">
    <div class="box alt">
      <div class="row 50% uniform">
         <div class="4u">
           <figure>
             <img src="https://pbs.twimg.com/profile_images/562466745340817408/_nIu8KHX.jpeg" alt="" class="thumbnail">
             <figcaption>Marble</figcaption>
           </figure>
         </div>

         <div class="4u">
           <figure>
             <img src="http://www.critterbabies.com/wp-content/gallery/kittens/cats-animals-kittens-background-us.jpg" alt="" class="thumbnail">
             <figcaption>Marble</figcaption>
           </figure>
         </div>
         
         <div class="4u">
           <figure>
             <img src="http://www.warrenphotographic.co.uk/photography/bigs/08482-Fluffy-ginger-female-kitten.jpg" alt="" class="thumbnail">
             <figcaption>Marble</figcaption>
           </figure>
         </div>
         
       </div>
     </div>
   </div>
 </section>

2 of 2
1

I've taken Scott Marcus' answer and adapted to click, which was your original request.

The main diffence is the addition of a tags targeting elements on the page and using :target in the css.

img { 
  width:200px;
  border:1px solid black; /* This is only added for testing purposes*/
}

.thumbnail:target {
   width: 500px;
   height:auto;
   position:relative;
   /* push image to the right by 1/2 the screen width and 1/2 the image width */
   margin-left:calc(50% - 250px);
}
<section id="main">
  <div class="inner">
    <div class="box alt">
      <div class="row 50% uniform">
         <div class="4u">
           <figure>
             <a href="#image1">
             <img src="https://pbs.twimg.com/profile_images/562466745340817408/_nIu8KHX.jpeg" alt="" class="thumbnail" id="image1">
             </a>
             <figcaption>Marble</figcaption>
           </figure>
         </div>

         <div class="4u">
           <figure>
             <a href="#image2">
             <img src="http://www.critterbabies.com/wp-content/gallery/kittens/cats-animals-kittens-background-us.jpg" alt="" class="thumbnail" id="image2">
             </a>
             <figcaption>Marble</figcaption>
           </figure>
         </div>
         
         <div class="4u">
           <figure>
             <a href="#image3">
             <img src="http://www.warrenphotographic.co.uk/photography/bigs/08482-Fluffy-ginger-female-kitten.jpg" alt="" class="thumbnail" id="image3">
             </a>
             <figcaption>Marble</figcaption>
           </figure>
         </div>
         
       </div>
     </div>
   </div>
 </section>

🌐
CodeWithRandom
codewithrandom.com › 2024 › 10 › 01 › create-onclick-image-popup-js
How to Create Onclick Image Popup JavaScript - CodeWithRandom
October 1, 2024 - This code is a jQuery script that creates a popup for displaying images when you click on them in a gallery. The first line of code define that when the whole document is ready, then the JS code will run.
🌐
Cloudinary
cloudinary.com › home › mastering javascript image popup
Mastering JavaScript Image Popup | Cloudinary
June 23, 2024 - Finally, an event listener (click event) is added to each image container <div>, which, when clicked, sets the modal element’s display style to “block” to show it, changes modalImg‘s src attribute to the clicked image’s URL and finally changes the captionText‘s inner HTML to the clicked image’s title. With the loadImages function now complete, we retrieve references to the modal (modal), image (modalImg), caption (captionText), and close button (span). We then add an onlick function to the span, which, when clicked, sets the modal’s display style is set to "none" to hide it.
🌐
CodeHim
codehim.com › home › lightbox › onclick image popup jquery lightbox plugin
onclick Image Popup jQuery Lightbox Plugin — CodeHim
January 22, 2024 - A lite plugin / code snippet that helps you to create onclick show image in popup using jQuery. It create HTML & CSS based image popup modal / lightbox.
Top answer
1 of 1
5

I edited your codepen a bit. So what I done:

  • removed your on click handler
  • added one handler for all clicks
  • applied it only for your images
  • added id for modal-img

I don't recommend you to use multiple ids with the same value. Would be better to use classes instead

// Get the modal
var modal = document.getElementById("myModal");

// Get the image and insert it inside the modal - use its "alt" text as a caption
// var img = document.getElementById("myImg");
var modalImg = document.getElementById("modal-img");
var captionText = document.getElementById("caption");
// img.onclick = function(){
//   modal.style.display = "block";
//   modalImg.src = this.src;
//   captionText.innerHTML = this.alt;
// }


document.addEventListener("click", (e) => {
  const elem = e.target;
  if (elem.id==="myImg") {
    modal.style.display = "block";
    modalImg.src = elem.dataset.biggerSrc || elem.src;
    captionText.innerHTML = elem.alt; 
  }
})

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

// When the user clicks on <span> (x), close the modal
span.onclick = function() { 
  modal.style.display = "none";
}
#myImg {

  border-radius: 5px;
  cursor: pointer;
  transition: 0.3s;
}

#myImg:hover {opacity: 0.7;}


#myImg2 {

  border-radius: 5px;
  cursor: pointer;
  transition: 0.3s;
}

#myImg2:hover {opacity: 0.7;}

/* The Modal (background) */
.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 (image) */
.modal-content {
  margin: auto;
  display: block;
  width: 80%;
  max-width: 700px;
}

/* Caption of Modal Image */
#caption {
  margin: auto;
  display: block;
  width: 80%;
  max-width: 700px;
  text-align: center;
  color: #ccc;
  padding: 10px 0;
  height: 150px;
}

/* Add Animation */
.modal-content, #caption {  
  -webkit-animation-name: zoom;
  -webkit-animation-duration: 0.6s;
  animation-name: zoom;
  animation-duration: 0.6s;
}

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

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

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

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

/* 100% Image Width on Smaller Screens */
@media only screen and (max-width: 700px){
  .modal-content {
    width: 100%;
  }
}
<img style="width:30%" id="myImg" class="img-fluid" src="https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885__340.jpg"
data-bigger-src="http://qnimate.com/wp-content/uploads/2014/03/images2.jpg"
>

 <img style="width:28%"  id="myImg" class="img-fluid" src="https://cdn.pixabay.com/photo/2015/06/19/21/24/the-road-815297__340.jpg">
 <img style="width:29%"  id="myImg" class="img-fluid" src="https://image.shutterstock.com/image-photo/mountains-during-sunset-beautiful-natural-260nw-407021107.jpg">

                                                    <!-- The Modal -->
<div id="myModal" class="modal">
  <span class="close">&times;</span>
  <img id="modal-img" class="modal-content"  src="https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcQS2ol73JZj6-IqypxPZXYS3rRiPwKteoD8vezk9QsRdkjt3jEn&usqp=CAU">
    
  
  <div id="caption"></div>
</div>

🌐
W3Schools
www-db.deis.unibo.it › courses › TW › DOCS › w3schools › howto › howto_css_modal_images.asp.html
How To Create Modal Images
HowTo Home Slideshow Google Maps Calendar Animated Buttons Modal Boxes Modal Images JS Animations Progress Bars Tooltips Hover Dropdowns Click Dropdowns Responsive Tables HTML Includes Image Effects Loader Menu Icon Accordion Tabs Contact Chips Pagination Animated Search Top Navigation Fullscreen Navigation Side Navigation Icon Bar Alerts Toggle Switch Scroll Drawing Pricing Table ... Learn how to create responsive Modal Images with CSS and JavaScript. A modal is a dialog box/popup window that is displayed on top of the current page. This example use most of the code from the previous example, Modal Boxes, only in this example, we use images.
🌐
Shoptimized
help.shoptimized.net › en › funnel-buildr › how-to-create-a-clickable-image-popup-2
How to Create a Clickable Image Popup
+P,function(){R()})},getIframe:function(b,c){var d=b.src,e=n.st.iframe;a.each(e.patterns,function(){if(d.indexOf(this.index)>-1)return this.id&&(typeof this.id=="string"?d=d.substr(d.lastIndexOf(this.id)+this.id.length,d.length):d=this.id.call(this,d)),d=this.src.replace("%id%",d),!1});var f={};return e.srcAction&&(f[e.srcAction]=d),n._parseMarkup(c,f,b),n.updateStatus("ready"),c}}});var S=function(a){var b=n.items.length;return a>b-1?a-b:a<0?b+a:a},T=function(a,b,c){return a.replace(/%curr%/gi,b+1).replace(/%total%/gi,c)};a.magnificPopup.registerModule("gallery",{options:{enabled:!1,arrowMark