The lightgallery.js is a lightweight JavaScript library that offers a full-featured Lightbox Image Gallery with styled layouts, image transitions, and interactive displays. It’s designed to create an engaging showcase for images, enhancing user interaction on a webpage.
You can use this code to implement a feature-rich Image Gallery on your website, providing an engaging way to showcase images. The major benefit is an enhanced user experience, with features like zoom, next/previous navigation, autoplay, and share options, making your image presentation visually appealing and interactive.
How to Create Full Featured Lightbox Image Gallery
1. First of all, load the lightgallery.js library by adding the following CDN link to the head tag of your webpage.
<link rel='stylesheet' href='https://cdn.rawgit.com/sachinchoolur/lightgallery.js/master/dist/css/lightgallery.css'>
2. After that, create an HTML structure for your image gallery. Ensure you have an element with the ID ‘lightgallery’ to serve as the container for your gallery. Inside this container, include a list of images with appropriate links.
<div class="cont"> <div class="page-head"> <h1>lightgallery <span class="version">V0.0.1</span></h1> <p class="lead">Full featured lightbox gallery. Zero dependencies.</p><a href="https://github.com/sachinchoolur/lightgallery.js" class="btn btn-primary btn-lg">View on github</a></div> <div class="demo-gallery"> <ul id="lightgallery"> <li data-responsive="https://sachinchoolur.github.io/lightgallery.js/static/img/1-375.jpg 375, https://sachinchoolur.github.io/lightgallery.js/static/img/1-480.jpg 480, https://sachinchoolur.github.io/lightgallery.js/static/img/1.jpg 800" data-src="https://sachinchoolur.github.io/lightgallery.js/static/img/1-1600.jpg" data-sub-html="<h4>Fading Light</h4><p>Classic view from Rigwood Jetty on Coniston Water an old archive shot similar to an old post but a little later on.</p>"> <a href=""> <img class="img-responsive" src="https://sachinchoolur.github.io/lightgallery.js/static/img/thumb-1.jpg"> <div class="demo-gallery-poster"> <img src="https://sachinchoolur.github.io/lightgallery.js/static/img/zoom.png"> </div> </a> </li> <li data-responsive="https://sachinchoolur.github.io/lightgallery.js/static/img/2-375.jpg 375, https://sachinchoolur.github.io/lightgallery.js/static/img/2-480.jpg 480, https://sachinchoolur.github.io/lightgallery.js/static/img/2.jpg 800" data-src="https://sachinchoolur.github.io/lightgallery.js/static/img/2-1600.jpg" data-sub-html="<h4>Bowness Bay</h4><p>A beautiful Sunrise this morning taken En-route to Keswick not one as planned but I'm extremely happy I was passing the right place at the right time....</p>"> <a href=""> <img class="img-responsive" src="https://sachinchoolur.github.io/lightgallery.js/static/img/thumb-2.jpg"> <div class="demo-gallery-poster"> <img src="https://sachinchoolur.github.io/lightgallery.js/static/img/zoom.png"> </div> </a> </li> <li data-responsive="https://sachinchoolur.github.io/lightgallery.js/static/img/13-375.jpg 375, https://sachinchoolur.github.io/lightgallery.js/static/img/13-480.jpg 480, https://sachinchoolur.github.io/lightgallery.js/static/img/13.jpg 800" data-src="https://sachinchoolur.github.io/lightgallery.js/static/img/13-1600.jpg" data-sub-html="<h4>Sunset Serenity</h4><p>A gorgeous Sunset tonight captured at Coniston Water....</p>"> <a href=""> <img class="img-responsive" src="https://sachinchoolur.github.io/lightgallery.js/static/img/thumb-13.jpg"> <div class="demo-gallery-poster"> <img src="https://sachinchoolur.github.io/lightgallery.js/static/img/zoom.png"> </div> </a> </li> <li data-responsive="https://sachinchoolur.github.io/lightgallery.js/static/img/4-375.jpg 375, https://sachinchoolur.github.io/lightgallery.js/static/img/4-480.jpg 480, https://sachinchoolur.github.io/lightgallery.js/static/img/4.jpg 800" data-src="https://sachinchoolur.github.io/lightgallery.js/static/img/4-1600.jpg" data-sub-html="<h4>Coniston Calmness</h4><p>Beautiful morning</p>"> <a href=""> <img class="img-responsive" src="https://sachinchoolur.github.io/lightgallery.js/static/img/thumb-4.jpg"> <div class="demo-gallery-poster"> <img src="https://sachinchoolur.github.io/lightgallery.js/static/img/zoom.png"> </div> </a> </li> </ul> <span class="small">Click on any of the images to see lightGallery</span> </div> </div>
3. Copy and paste the following CSS code into your stylesheet. This ensures a visually appealing layout for your image gallery, with defined styles for elements like buttons, headings, and the overall gallery appearance.
body { font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif } .small { font-size: 11px; color: #999; display: block; margin-top: -10px } .cont { text-align: center; } .page-head { padding: 60px 0; text-align: center; } .page-head .lead { font-size: 18px; font-weight: 400; line-height: 1.4; margin-bottom: 50px; margin-top: 0; } .btn { -moz-user-select: none; background-image: none; border: 1px solid transparent; border-radius: 2px; cursor: pointer; display: inline-block; font-size: 14px; font-weight: normal; line-height: 1.42857; margin-bottom: 0; padding: 6px 12px; text-align: center; vertical-align: middle; white-space: nowrap; text-decoration: none; } .btn-lg { border-radius: 2px; font-size: 18px; line-height: 1.33333; padding: 10px 16px; } .btn-primary:hover { background-color: #fff; color: #152836; } .btn-primary { background-color: #152836; border-color: #0e1a24; color: #ffffff; } .btn-primary { border-color: #eeeeee; color: #eeeeee; transition: color 0.1s ease 0s, background-color 0.15s ease 0s; } .page-head h1 { font-size: 42px; margin: 0 0 20px; color: #FFF; position: relative; display: inline-block; } .page-head h1 .version { bottom: 0; color: #ddd; font-size: 11px; font-style: italic; position: absolute; width: 58px; right: -58px; } .demo-gallery > ul { margin-bottom: 0; padding-left: 15px; } .demo-gallery > ul > li { margin-bottom: 15px; width: 180px; display: inline-block; margin-right: 15px; list-style: outside none none; } .demo-gallery > ul > li a { border: 3px solid #FFF; border-radius: 3px; display: block; overflow: hidden; position: relative; float: left; } .demo-gallery > ul > li a > img { -webkit-transition: -webkit-transform 0.15s ease 0s; -moz-transition: -moz-transform 0.15s ease 0s; -o-transition: -o-transform 0.15s ease 0s; transition: transform 0.15s ease 0s; -webkit-transform: scale3d(1, 1, 1); transform: scale3d(1, 1, 1); height: 100%; width: 100%; } .demo-gallery > ul > li a:hover > img { -webkit-transform: scale3d(1.1, 1.1, 1.1); transform: scale3d(1.1, 1.1, 1.1); } .demo-gallery > ul > li a:hover .demo-gallery-poster > img { opacity: 1; } .demo-gallery > ul > li a .demo-gallery-poster { background-color: rgba(0, 0, 0, 0.1); bottom: 0; left: 0; position: absolute; right: 0; top: 0; -webkit-transition: background-color 0.15s ease 0s; -o-transition: background-color 0.15s ease 0s; transition: background-color 0.15s ease 0s; } .demo-gallery > ul > li a .demo-gallery-poster > img { left: 50%; margin-left: -10px; margin-top: -10px; opacity: 0; position: absolute; top: 50%; -webkit-transition: opacity 0.3s ease 0s; -o-transition: opacity 0.3s ease 0s; transition: opacity 0.3s ease 0s; } .demo-gallery > ul > li a:hover .demo-gallery-poster { background-color: rgba(0, 0, 0, 0.5); } .demo-gallery .justified-gallery > a > img { -webkit-transition: -webkit-transform 0.15s ease 0s; -moz-transition: -moz-transform 0.15s ease 0s; -o-transition: -o-transform 0.15s ease 0s; transition: transform 0.15s ease 0s; -webkit-transform: scale3d(1, 1, 1); transform: scale3d(1, 1, 1); height: 100%; width: 100%; } .demo-gallery .justified-gallery > a:hover > img { -webkit-transform: scale3d(1.1, 1.1, 1.1); transform: scale3d(1.1, 1.1, 1.1); } .demo-gallery .justified-gallery > a:hover .demo-gallery-poster > img { opacity: 1; } .demo-gallery .justified-gallery > a .demo-gallery-poster { background-color: rgba(0, 0, 0, 0.1); bottom: 0; left: 0; position: absolute; right: 0; top: 0; -webkit-transition: background-color 0.15s ease 0s; -o-transition: background-color 0.15s ease 0s; transition: background-color 0.15s ease 0s; } .demo-gallery .justified-gallery > a .demo-gallery-poster > img { left: 50%; margin-left: -10px; margin-top: -10px; opacity: 0; position: absolute; top: 50%; -webkit-transition: opacity 0.3s ease 0s; -o-transition: opacity 0.3s ease 0s; transition: opacity 0.3s ease 0s; } .demo-gallery .justified-gallery > a:hover .demo-gallery-poster { background-color: rgba(0, 0, 0, 0.5); } .demo-gallery .video .demo-gallery-poster img { height: 48px; margin-left: -24px; margin-top: -24px; opacity: 0.8; width: 48px; } .demo-gallery.dark > ul > li a { border: 3px solid #04070a; }
4. Now, load the following dependencies by adding the following CDN links just after the HTML structure of your image gallery.
<script src='https://cdn.rawgit.com/sachinchoolur/lightgallery.js/master/dist/js/lightgallery.js'></script> <script src='https://cdn.rawgit.com/sachinchoolur/lg-pager.js/master/dist/lg-pager.js'></script> <script src='https://cdn.rawgit.com/sachinchoolur/lg-autoplay.js/master/dist/lg-autoplay.js'></script> <script src='https://cdn.rawgit.com/sachinchoolur/lg-share.js/master/dist/lg-share.js'></script> <script src='https://cdn.rawgit.com/sachinchoolur/lg-fullscreen.js/master/dist/lg-fullscreen.js'></script> <script src='https://cdn.rawgit.com/sachinchoolur/lg-zoom.js/master/dist/lg-zoom.js'></script> <script src='https://cdn.rawgit.com/sachinchoolur/lg-hash.js/master/dist/lg-hash.js'></script> <script src='https://cdn.jsdelivr.net/picturefill/2.3.1/picturefill.min.js'></script>
5. Finally, place the following JavaScript code just before the closing </body>
tag in your HTML file. This code initializes the lightGallery on the specified container ID (‘lightgallery’), making your gallery interactive.
lightGallery(document.getElementById('lightgallery'))
That’s all! hopefully, you have successfully created a full-featured lightbox image gallery on your website. If you have any questions or suggestions, feel free to comment below.
Similar Code Snippets:
I code and create web elements for amazing people around the world. I like work with new people. New people new Experiences.
I truly enjoy what I’m doing, which makes me more passionate about web development and coding. I am always ready to do challenging tasks whether it is about creating a custom CMS from scratch or customizing an existing system.