How to create Responsive Image 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 Responsive Image Gallery. So if you are a person who wishes to develop a website, you have to know how to create a gallery properly.
How to create Responsive Image 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 id="gallery" class="container">
<div class="col-xs-12 no-pad">
<h3>Image Gallery</h3>
<hr />
</div>
<div id="gallery-box">
<div id="gallery-count"><span id="count">1</span> of <span id="total"></span></div>
<a id="gallery-link" href="https://loremflickr.com/800/600/dog?lock=10" target="_blank">
<div id="gallery-img">
</div>
</a>
</div>
<div class="gallery-thumbnails">
<div class="col-xs-12 no-pad">
<img id="1" class="img-responsive" src="https://loremflickr.com/800/600/dog?lock=10" />
<img id="2" class="img-responsive" src="https://loremflickr.com/800/600/dog?lock=20" />
<img id="3" class="img-responsive" src="https://loremflickr.com/800/600/dog?lock=30" />
<img id="4" class="img-responsive" src="https://loremflickr.com/800/600/dog?lock=40" />
<img id="5" class="img-responsive" src="https://loremflickr.com/800/600/dog?lock=50" />
<img id="6" class="img-responsive" src="https://loremflickr.com/800/600/dog?lock=60" />
<img id="7" class="img-responsive" src="https://loremflickr.com/800/600/dog?lock=70" />
<img id="8" class="img-responsive" src="https://loremflickr.com/800/600/dog?lock=80" />
<img id="9" class="img-responsive" src="https://loremflickr.com/800/600/dog?lock=90" />
<img id="10" class="img-responsive" src="https://loremflickr.com/800/600/dog?lock=100" />
</div>
</div>
<hr />
<div class="row">
<div class="col-xs-12 col-sm-4">
<label>Image Display</label>
<select id="imgDisplay" class="form-control">
<option value="cover" default>cover</option>
<option value="contain">contain</option>
</select>
</div>
<div class="col-xs-12 col-sm-4">
<label>Scroll</label>
<select id="imgScroll" class="form-control">
<option value="yes" default>Yes</option>
<option value="no">No</option>
</select>
</div>
<div class="col-xs-12 col-sm-4">
<label>Scale</label>
<input type="range" min="200" max="1200" value="700" class="slider form-control" id="imgScale">
</div>
</div>
</body>
Step 2 – Add CSS
We use some external CSS link to this code. No need to worry about this, copy and paste below code between <head></head> tag.
<link href='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.2/css/bootstrap.min.css' rel='stylesheet' type='text/css'>
Copy and paste below code in your HTML editor between <style></style> tag.
<style>
body {
width: 100vw;
height: 100vh;
margin: 2% auto;
background-color: #111;
}
label {
color: #5d5dff;
}
#gallery {
color: white;
}
#gallery-box {
display: block;
position: relative;
max-height: 400px;
width: 100%;
overflow: scroll;
width: 100%;
}
#gallery-box::-webkit-scrollbar {
width: 10px;
height: 10px;
}
#gallery-box::-webkit-scrollbar-track {
background: #e5e5e5;
}
#gallery-box::-webkit-scrollbar-thumb {
background: #5d5dff;
}
#gallery-box::-webkit-scrollbar-thumb:hover {
background: #7d7dff;
}
#gallery-count {
display: inline-block;
position: absolute;
top: 0;
right: 0;
padding: 15px;
background-color: rgba(0, 0, 0, 0.8);
color: #fff;
height: 50px;
text-decoration: none;
}
#gallery-img {
display: -webkit-box;
display: flex;
min-height: 400px;
min-width: 600px;
height: auto;
background: #e5e5e5;
text-align: center;
background-image: url("https://loremflickr.com/800/600/dog?lock=10");
background-size: cover;
background-repeat: no-repeat;
background-position: center;
-webkit-transition: 0.3s ease-in-out;
transition: 0.3s ease-in-out;
}
.gallery-thumbnails {
display: -webkit-box;
display: flex;
height: auto;
overflow: hidden;
width: 100%;
background: rgba(255, 255, 255, 0.1);
border-top: 2px solid white;
}
.gallery-thumbnails img {
float: left;
width: 10%;
cursor: pointer;
padding: 0;
-webkit-filter: grayscale(1);
filter: grayscale(1);
min-height: 25px;
}
.gallery-thumbnails img:hover {
-webkit-filter: none !important;
filter: none !important;
}
</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 src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.2/css/bootstrap.min.css">
</script>
Copy and paste below code in your HTML editor between <script></script> tag.
<script $(document).ready(function () {
// Change image on selection
$("#gallery img").click(function () {
// Get current image source
var image = $(this).attr("src");
// Apply grayscale to thumbnails except selected
$("#gallery").find("img").css("filter", "grayscale(1)");
$(this).css("filter", "none");
// Change image
$("#gallery-img").css("background-image", "url(" + image + ")");
// Apply link to image
$("#gallery-link").attr("href", image);
// Use id for count
$("#count").text($(this).attr("id"));
});
// Get total number of images
var gallerySize = $(".gallery-thumbnails img").length;
$("#total").text(gallerySize);
var display = $("#imgDisplay");
var scroll = $("#imgScroll");
var scale = $("#imgScale");
// Image display
display.change(function () {
if (display.val() === "contain") {
$("#gallery-img").css("background-size", "contain");
} else {
$("#gallery-img").css("background-size", "cover");
}
});
// Scroll
scroll.change(function () {
if (scroll.val() === "yes") {
$("#gallery-box").css("overflow", "scroll");
} else {
$("#gallery-box").css("overflow", "hidden");
}
});
// Scale
var changeScale = scale.change(function () {
$("#gallery-img").css("background-size", scale.val() + "px");
});
});
</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.