Bootstrap 5 Image Gallery with modal Responsive | YourSite

Bootstrap 5 Image Gallery with modal Responsive

Create Modal Using Bootstrap 5 and Javascript

Download this project Click Here

HTML Code


<!DOCTYPE html>
<html lang="en">
<head>
  <title>Image Gallery - Bootstrap 5</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
  <link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
  

  <section class="gallery min-vh-100">
     <div class="container-lg">
        <div class="row gy-4 row-cols-1 row-cols-sm-2 row-cols-md-3">
           <div class="col">
              <img src="img/1.jpg" class="gallery-item" alt="gallery">
           </div>
           <div class="col">
              <img src="img/2.jpg" class="gallery-item" alt="gallery">
           </div>
           <div class="col">
              <img src="img/3.jpg" class="gallery-item" alt="gallery">
           </div>
           <div class="col">
              <img src="img/4.jpg" class="gallery-item" alt="gallery">
           </div>
           <div class="col">
              <img src="img/5.jpg" class="gallery-item" alt="gallery">
           </div>
           <div class="col">
              <img src="img/6.jpg" class="gallery-item" alt="gallery">
           </div>
        </div>
     </div>
  </section>

<!-- Modal -->
<div class="modal fade" id="gallery-modal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered modal-lg">
    <div class="modal-content">
      <div class="modal-header">
        <!-- <h5 class="modal-title" id="exampleModalLabel">Modal title</h5> -->
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="modal-body">
         <img src="img/1.jpg" class="modal-img" alt="modal img">
      </div>
    </div>
  </div>
</div>


<script src="js/bootstrap.bundle.min.js"></script>
<script src="js/main.js"></script>
</body>
</html>

JS Code




 document.addEventListener("click",function (e){
   if(e.target.classList.contains("gallery-item")){
   	  const src = e.target.getAttribute("src");
   	  document.querySelector(".modal-img").src = src;
   	  const myModal = new bootstrap.Modal(document.getElementById('gallery-modal'));
   	  myModal.show();
   }
 })

CSS Code


img{
	max-width: 100%;
}
.gallery{
	background-color: #dbddf1;
	padding: 80px 0;
}
.gallery img{
	background-color: #ffffff;
	padding: 15px;
	width: 100%;
	box-shadow: 0 0 15px rgba(0,0,0,0.3);
	cursor: pointer;
}
#gallery-modal .modal-img{
	width: 100%;
}

🚀 More Blogs You Might Like

Explore more articles and keep learning

Data Science Roadmap: From Complete Beginner to Job-Ready Practitioner
Data-Science
Data Science Roadmap: From Complete Beginner to Job-Ready Practitioner

Data Science Roadmap: From Complete Beginner to Job-Ready Practitioner...

👁 74 2026-07-26
Read More →
How AI Is Changing the Way Software Is Made
Data-Science
How AI Is Changing the Way Software Is Made

How AI Is Changing the Way Software Is Made...

👁 68 2026-07-26
Read More →
8 Mistakes That Quietly Slow Down Talented Developers
Personal-Development
8 Mistakes That Quietly Slow Down Talented Developers

8 Mistakes That Quietly Slow Down Talented Developers...

👁 100 2026-07-12
Read More →
Does religion teach hatred?
islam
Does religion teach hatred?

It is easy to spread hatred in the name of religion, but understanding the true message of religion is much de...

👁 575 2026-06-30
Read More →