
How to create a Lightbox gallery
Before We Start
A gallery is one of the main components of a website, It contains links to the other sections and a user interface element within a website. The most important one and the first section that the user sees when he/she enters a website.
How to create a Lightbox gallery. So if you are a person who wishes to develop a website, you have to know how to create a gallery properly.
100+ Premium Wordpress Themes & Plugins
Just $2.99 From Template Cafe

How to create a gallery ? We are here to solve your problem.
In this article we discuss how to create a gallery. Before we start please read the below articles which easier you to understand the process, if you are new to development.
Step 1 – Add HTML
It’s too easy and simple. Just copy and paste below code in your HTML editor between <body> </body> tag.
<body>
<div class="row">
<div class="col">
<img src="https://source.unsplash.com/random/200" onclick="openLightbox();toSlide(1)" class="hover-shadow preview" alt="" />
</div>
<div class="col">
<img src="https://source.unsplash.com/random/201" onclick="openLightbox();toSlide(2)" class="hover-shadow preview" alt="" />
</div>
<div class="col">
<img src="https://source.unsplash.com/random/202" onclick="openLightbox();toSlide(3)" class="hover-shadow preview" alt="" />
<img src="https://source.unsplash.com/random/203" onclick="openLightbox();toSlide(4)" class="hover-shadow preview" alt="" />
</div>
<div class="col">
<img src="https://source.unsplash.com/random/204" onclick="openLightbox();toSlide(5)" class="hover-shadow preview" alt="" />
</div>
<div class="col">
<img src="https://source.unsplash.com/random/205" onclick="openLightbox();toSlide(6)" class="hover-shadow preview" alt="" />
</div>
</div>
<div id="Lightbox" class="modal">
<span class="close pointer" onclick="closeLightbox()">×</span>
<div class="modal-content">
<div class="slide">
<img src="https://source.unsplash.com/random/200" class="image-slide" alt="" />
</div>
<div class="slide">
<img src="https://source.unsplash.com/random/201" class="image-slide" alt="" />
</div>
<div class="slide">
<img src="https://source.unsplash.com/random/202" class="image-slide" alt="" />
</div>
<div class="slide">
<img src="https://source.unsplash.com/random/203" class="image-slide" alt="" />
</div>
<div class="slide">
<img src="https://source.unsplash.com/random/204" class="image-slide" alt="" />
</div>
<div class="slide">
<img src="https://source.unsplash.com/random/205" class="image-slide" alt="" />
</div>
<a class="previous" onclick="changeSlide(-1)">❮</a>
<a class="next" onclick="changeSlide(1)">❯</a>
<div class="dots">
<div class="col">
<img src="https://source.unsplash.com/random/200" class="modal-preview hover-shadow" onclick="toSlide(1)" alt="" />
</div>
<div class="col">
<img src="https://source.unsplash.com/random/201" class="modal-preview hover-shadow" onclick="toSlide(2)" alt="" />
</div>
<div class="col">
<img src="https://source.unsplash.com/random/202" class="modal-preview hover-shadow" onclick="toSlide(3)" alt="" />
</div>
<div class="col">
<img src="https://source.unsplash.com/random/203" class="modal-preview hover-shadow" onclick="toSlide(4)" alt="" />
</div>
<div class="col">
<img src="https://source.unsplash.com/random/204" class="modal-preview hover-shadow" onclick="toSlide(5)" alt="" />
</div>
<div class="col">
<img src="https://source.unsplash.com/random/205" class="modal-preview hover-shadow" onclick="toSlide(6)" alt="" />
</div>
</div>
</div>
</div>
</body>
Step 2 – Add CSS
Copy and paste below code in your HTML editor between <style></style> tag.
<style>
html {
box-sizing: border-box;
}
*, *:before, *:after {
box-sizing: inherit;
}
body {
margin: 0;
}
.preview {
width: 100%;
}
.row {
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
margin: 2rem auto;
width: 80vw;
}
.row > .col {
padding: 0 10px;
}
.col {
float: left;
width: 33.3%;
}
.modal {
display: none;
position: fixed;
z-index: 1;
padding: 10px 62px 0px 62px;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: black;
}
.modal-content {
position: relative;
display: flex;
flex-direction: column;
justify-content: center;
margin: auto;
padding: 0 0 0 0;
width: 80vw;
max-width: 1000px;
}
.slide {
display: none;
}
.image-slide {
width: 100%;
}
.modal-preview {
width: 100%;
}
.dots {
display: flex;
flex-direction: row;
justify-content: space-between;
}
img.preview, img.modal-preview {
cursor: pointer;
opacity: 0.7;
}
img.active,
.preview:hover,
.modal-preview:hover {
opacity: 1;
}
img.hover-shadow {
transition: 0.3s;
}
.hover-shadow:hover {
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}
.close {
color: white;
position: absolute;
top: 10px;
right: 25px;
font-size: 35px;
font-weight: bold;
}
.close:hover,
.close:focus {
color: #999;
text-decoration: none;
cursor: pointer;
}
.previous,
.next {
cursor: pointer;
position: absolute;
top: 50%;
width: auto;
padding: 16px;
margin-top: -50px;
color: white;
font-weight: bold;
font-size: 24px;
transition: 0.6s ease;
border-radius: 0 3px 3px 0;
user-select: none;
-webkit-user-select: none;
}
.next {
right: 0;
border-radius: 3px 0 0 3px;
}
.previous:hover,
.next:hover {
background-color: rgba(0, 0, 0, 0.8);
}
</style>
Step 2 – Add JavaScript
We use some external JavaScript link to this code. No need to worry about this, copy and paste below code between <head></head> tag.
<script>
let slideIndex = 1;
showSlide(slideIndex);
function openLightbox() {
document.getElementById('Lightbox').style.display = 'block';
};
function closeLightbox() {
document.getElementById('Lightbox').style.display = 'none';
};
function changeSlide(n) {
showSlide(slideIndex += n);
};
function toSlide(n) {
showSlide(slideIndex = n);
};
function showSlide(n) {
const slides = document.getElementsByClassName('slide');
let modalPreviews = document.getElementsByClassName('modal-preview');
if (n > slides.length) {
slideIndex = 1;
};
if (n < 1) {
slideIndex = slides.length;
};
for (let i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
};
for (let i = 0; i < modalPreviews.length; i++) {
modalPreviews[i].className = modalPreviews[i].className.replace(' active', '');
};
slides[slideIndex - 1].style.display = 'block';
modalPreviews[slideIndex - 1].className += ' active';
};
</script>
At the end we will have something like this. You can change fonts, colors, backgrounds and all things that you want.Enjoy it.

Video Tutorial
Please watch this video to better understand this tutorial and don’t forget to subscribe to our channel.
Help others to find out about this article on Social Media sites. If you have any doubt or any problem, don’t hesitate to contact us. Thereafter we will be able to help you and also make sure you bookmark our site on your browser.