"light box gallery"
Bootstrap 4.1.1 Snippet by Poojathakur

<link href="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css"> <script src="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <!------ Include the above in your HEAD tag ----------> <!DOCTYPE html> <html lang="en" > <head> <meta charset="UTF-8"> <title>CodePen - Responsive Image Gallery with jQuery Lightbox</title> <link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css'> <link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css'><link rel="stylesheet" href="./style.css"> </head> <body> <!-- partial:index.partial.html --> <section id="gallery"> <div class="container"> <div id="image-gallery"> <div class="row"> <div class="grid-image"> <div class="image"> <div class="img-wrapper"> <a href="https://plus.unsplash.com/premium_photo-1676310055316-d73c9d5eeb51?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=870&q=80"><img src="https://plus.unsplash.com/premium_photo-1676310055316-d73c9d5eeb51?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=870&q=80" class="img-responsive"></a> <div class="img-overlay"> <i class="fa-sharp fa-solid fa-plus"></i> </div> </div> </div> <div class="image"> <div class="img-wrapper"> <a href="https://images.unsplash.com/photo-1674574124649-778f9afc0e9c?ixlib=rb-4.0.3&ixid=MnwxMjA3fDF8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=870&q=80"><img src="https://images.unsplash.com/photo-1674574124649-778f9afc0e9c?ixlib=rb-4.0.3&ixid=MnwxMjA3fDF8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=870&q=80" class="img-responsive"></a> <div class="img-overlay"> <i class="fa-sharp fa-solid fa-plus"></i> </div> </div> </div> <div class="image"> <div class="img-wrapper"> <a href="https://images.unsplash.com/photo-1661956600684-97d3a4320e45?ixlib=rb-4.0.3&ixid=MnwxMjA3fDF8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=870&q=80"><img src="https://images.unsplash.com/photo-1661956600684-97d3a4320e45?ixlib=rb-4.0.3&ixid=MnwxMjA3fDF8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=870&q=80 class="img-responsive"></a> <div class="img-overlay"> <i class="fa-sharp fa-solid fa-plus"></i> </div> </div> </div> </div> <div class="grid-image"> <div class="image"> <div class="img-wrapper"> <a href="https://images.unsplash.com/photo-1681160235950-2e802799a54b?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1032&q=80"><img src="https://images.unsplash.com/photo-1681160235950-2e802799a54b?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1032&q=80" class="img-responsive"></a> <div class="img-overlay"> <i class="fa-sharp fa-solid fa-plus"></i> </div> </div> </div> <div class="image"> <div class="img-wrapper"> <a href="https://images.unsplash.com/photo-1681151335137-108527dc24fe?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=876&q=80"><img src="https://images.unsplash.com/photo-1681151335137-108527dc24fe?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=876&q=80" class="img-responsive"></a> <div class="img-overlay"> <i class="fa-sharp fa-solid fa-plus"></i> </div> </div> </div> <div class="image"> <div class="img-wrapper"> <a href="https://plus.unsplash.com/premium_photo-1676325102568-490a767a7c7b?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=870&q=80"><img src="https://plus.unsplash.com/premium_photo-1676325102568-490a767a7c7b?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=870&q=80" class="img-responsive"></a> <div class="img-overlay"> <i class="fa-sharp fa-solid fa-plus"></i> </div> </div> </div> </div> </div><!-- End row --> </div><!-- End image gallery --> </div><!-- End container --> </section> <!-- partial --> <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script> <script src='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js'></script><script src="./script.js"></script> </body> </html>
.grid-image .img-wrapper { position: relative; margin-top: 0px; } .img-wrapper img { width: 100%; } .img-overlay { background: rgba(0, 0, 0, 0.7); width: 100%; height: 100%; position: absolute; top: 0; left: 0; display: flex; justify-content: center; align-items: center; opacity: 0; } .img-overlay i { color: #fff; font-size: 3em; } #overlay { background: rgba(0, 0, 0, 0.7); width: 100%; height: 100%; position: fixed; top: 0; left: 0; display: flex; justify-content: center; align-items: center; z-index: 999; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; } #overlay img { margin: 0; width: 80%; height: auto; -o-object-fit: contain; object-fit: contain; padding: 5%; } .grid-image { display: flex; } .grid-image img { padding: 7.5px; } @media screen and (min-width: 768px) { #overlay img { width: 60%; } } @media screen and (min-width: 1200px) { #overlay img { width: 100%; max-width: fit-content; } } #nextButton { color: #fff; font-size: 2em; transition: opacity 0.8s; } #nextButton:hover { opacity: 0.7; } @media screen and (min-width: 768px) { #nextButton { font-size: 3em; } } #prevButton { color: #fff; font-size: 2em; transition: opacity 0.8s; } #prevButton:hover { opacity: 0.7; } @media screen and (min-width: 768px) { #prevButton { font-size: 3em; } } #exitButton { color: #fff; font-size: 2em; transition: opacity 0.8s; position: absolute; top: 15px; right: 15px; } #exitButton:hover { opacity: 0.7; } @media screen and (min-width: 768px) { #exitButton { font-size: 3em; } }
// Gallery image hover $( ".img-wrapper" ).hover( function() { $(this).find(".img-overlay").animate({opacity: 1}, 600); }, function() { $(this).find(".img-overlay").animate({opacity: 0}, 600); } ); // Lightbox var $overlay = $('<div id="overlay"></div>'); var $image = $("<img>"); var $prevButton = $('<div id="prevButton"><i class="fa fa-chevron-left"></i></div>'); var $nextButton = $('<div id="nextButton"><i class="fa fa-chevron-right"></i></div>'); var $exitButton = $('<div id="exitButton"><i class="fa fa-times"></i></div>'); // Add overlay $overlay.append($image).prepend($prevButton).append($nextButton).append($exitButton); $("#gallery").append($overlay); // Hide overlay on default $overlay.hide(); // When an image is clicked $(".img-overlay").click(function(event) { // Prevents default behavior event.preventDefault(); // Adds href attribute to variable var imageLocation = $(this).prev().attr("href"); // Add the image src to $image $image.attr("src", imageLocation); // Fade in the overlay $overlay.fadeIn("slow"); }); // When the overlay is clicked $overlay.click(function() { // Fade out the overlay $(this).fadeOut("slow"); }); // When next button is clicked $nextButton.click(function(event) { // Hide the current image $("#overlay img").hide(); // Overlay image location var $currentImgSrc = $("#overlay img").attr("src"); // Image with matching location of the overlay image var $currentImg = $('#image-gallery img[src="' + $currentImgSrc + '"]'); // Finds the next image var $nextImg = $($currentImg.closest(".image").next().find("img")); // All of the images in the gallery var $images = $("#image-gallery img"); // If there is a next image if ($nextImg.length > 0) { // Fade in the next image $("#overlay img").attr("src", $nextImg.attr("src")).fadeIn(800); } else { // Otherwise fade in the first image $("#overlay img").attr("src", $($images[0]).attr("src")).fadeIn(800); } // Prevents overlay from being hidden event.stopPropagation(); }); // When previous button is clicked $prevButton.click(function(event) { // Hide the current image $("#overlay img").hide(); // Overlay image location var $currentImgSrc = $("#overlay img").attr("src"); // Image with matching location of the overlay image var $currentImg = $('#image-gallery img[src="' + $currentImgSrc + '"]'); // Finds the next image var $nextImg = $($currentImg.closest(".image").prev().find("img")); // Fade in the next image $("#overlay img").attr("src", $nextImg.attr("src")).fadeIn(800); // Prevents overlay from being hidden event.stopPropagation(); }); // When the exit button is clicked $exitButton.click(function() { // Fade out the overlay $("#overlay").fadeOut("slow"); });

Related: See More


Questions / Comments: