Lightbox using Modal

Box_model

Gallery with Modal Lightbox and Carousel.

This snippet was created to help web designers, web developers, Front-end and Back-end developer save time. Use it for free in your project and build your app faster, You can also download the HTML , CSS and JS CODE.

HTML CODE.

This is the html code used to create a snippet, You can copy and paste the following html code inside a page project, to get the result that you can see in the preview selection

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>CodezillaYe Lightbox using Modal</title>
  <link rel="stylesheet" href="style.css">
</head>
<body>

<div class="container">
  <div class="row">
    <h1>CodezillaYe Lightbox using Modal</h1>
    <div class="row">
      <div class="column"><a title="Image 1" href="#" onclick="openModal(1)"><img class="thumbnail" src="https://picsum.photos/id/1011/300/200" alt="Image 1"></a></div>
      <div class="column"><a title="Image 2" href="#" onclick="openModal(2)"><img class="thumbnail" src="https://picsum.photos/id/1012/300/200" alt="Image 2"></a></div>
      <div class="column"><a title="Image 3" href="#" onclick="openModal(3)"><img class="thumbnail" src="https://picsum.photos/id/1013/300/200" alt="Image 3"></a></div>
      <div class="column"><a title="Image 4" href="#" onclick="openModal(4)"><img class="thumbnail" src="https://picsum.photos/id/1014/300/200" alt="Image 4"></a></div>
      <div class="column"><a title="Image 5" href="#" onclick="openModal(5)"><img class="thumbnail" src="https://picsum.photos/id/1015/300/200" alt="Image 5"></a></div>
      <div class="column"><a title="Image 6" href="#" onclick="openModal(6)"><img class="thumbnail" src="https://picsum.photos/id/1016/300/200" alt="Image 6"></a></div>
      <div class="column"><a title="Image 7" href="#" onclick="openModal(7)"><img class="thumbnail" src="https://picsum.photos/id/1016/300/200" alt="Image 7"></a></div>
      <div class="column"><a title="Image 8" href="#" onclick="openModal(8)"><img class="thumbnail" src="https://picsum.photos/id/1018/300/200" alt="Image 8"></a></div>
      <div class="column"><a title="Image 9" href="#" onclick="openModal(9)"><img class="thumbnail" src="https://picsum.photos/id/1019/300/200" alt="Image 9"></a></div>
      <div class="column"><a title="Image 10" href="#" onclick="openModal(10)"><img class="thumbnail" src="https://picsum.photos/id/1020/300/200" alt="Image 10"></a></div>
      <div class="column"><a title="Image 11" href="#" onclick="openModal(11)"><img class="thumbnail" src="https://picsum.photos/id/1021/300/200" alt="Image 11"></a></div>
      <div class="column"><a title="Image 12" href="#" onclick="openModal(12)"><img class="thumbnail" src="https://picsum.photos/id/1022/300/200" alt="Image 12"></a></div>
    </div>
    <hr>
  </div>
</div>

<div class="modal" id="myModal">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button class="close" type="button" onclick="closeModal()">×</button>
        <h3 class="modal-title"></h3>
      </div>
      <div class="modal-body">
        <div id="modalCarousel" class="carousel">
          <div class="carousel-inner"></div>
          <a class="carousel-control left" href="#" onclick="prevSlide()">&#10094;</a>
          <a class="carousel-control right" href="#" onclick="nextSlide()">&#10095;</a>
        </div>
      </div>
      <div class="modal-footer">
        <button class="btn" onclick="closeModal()">Close</button>
      </div>
    </div>
  </div>
</div>

<script src="/app.js"></script>
</body>
</html>

css code

This is the css code used to create a snippet, You can copy and paste the following css code inside a page project, to get the result that you can see in the preview selection.

body {
    font-family: Arial, sans-serif;
  }
  
  .container {
    width: 90%;
    margin: auto;
    overflow: hidden;
  }
  
  .row {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
  }
  
  .column {
    flex: 0 0 22%;
    margin-bottom: 20px;
  }
  
  .column a {
    display: block;
  }
  
  .thumbnail {
    width: 100%;
    height: auto;
    border: 1px solid #ddd;
    border-radius: 4px;
    padding: 5px;
    transition: 0.3s;
  }
  
  .thumbnail:hover {
    border-color: #777;
  }
  
  .modal {
    display: none;
    position: fixed;
    z-index: 1;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    overflow: auto;
    background-color: rgb(0,0,0);
    background-color: rgba(0,0,0,0.9);
  }
  
  .modal-dialog {
    margin: 15% auto;
    width: 80%;
  }
  
  .modal-content {
    background-color: #fff;
    padding: 20px;
    border-radius: 4px;
  }
  
  .modal-header, .modal-footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  
  .modal-header {
    border-bottom: 1px solid #eee;
  }
  
  .modal-title {
    margin: 0;
  }
  
  .close {
    background: none;
    border: none;
    font-size: 30px;
  }
  
  .modal-body {
    margin: 20px 0;
  }
  
  .carousel {
    position: relative;
  }
  
  .carousel-inner {
    display: flex;
    overflow: hidden;
  }
  
  .carousel-inner img {
    width: 100%;
  }
  
  .carousel-control {
    cursor: pointer;
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    color: #000;
    font-size: 30px;
    padding: 0 10px;
  }
  
  .carousel-control.left {
    left: 0;
  }
  
  .carousel-control.right {
    right: 0;
  }
  
  .btn {
    background-color: #ddd;
    border: none;
    padding: 10px 20px;
    cursor: pointer;
  }
  
  .btn:hover {
    background-color: #ccc;
  }
  

javascripts code.

This is the JS code used to create a snippet, You can copy and paste the following JS code inside a page project, to get the result that you can see in the preview selection.

let currentSlide = 0;
let images = document.querySelectorAll('.thumbnail');
let modal = document.getElementById('myModal');
let modalTitle = document.querySelector('.modal-title');
let modalBody = document.querySelector('.modal-body .carousel-inner');

function openModal(index) {
  currentSlide = index - 1;
  modal.style.display = 'block';
  modalTitle.textContent = `Image ${index}`;
  modalBody.innerHTML = `<img src="${images[currentSlide].src}" alt="Image ${index}">`;
}

function closeModal() {
  modal.style.display = 'none';
}

function nextSlide() {
  currentSlide = (currentSlide + 1) % images.length;
  updateSlide();
}

function prevSlide() {
  currentSlide = (currentSlide - 1 + images.length) % images.length;
  updateSlide();
}

function updateSlide() {
  modalTitle.textContent = `Image ${currentSlide + 1}`;
  modalBody.innerHTML = `<img src="${images[currentSlide].src}" alt="Image ${currentSlide + 1}">`;
}

// Close the modal when clicking outside of the modal content
window.onclick = function(event) {
  if (event.target === modal) {
    closeModal();
  }
};