Open In App
Related Articles

How to make image Gallery using HTML CSS and JavaScript ?

Like Article
Save Article
Report issue

Image Gallery is used to store and display a collection of pictures. In this article,  we will create a responsive Image Gallery using HTML and CSS, and JavaScript. If you are a beginner then this will help you build a small website project using HTML, CSS, and JavaScript by referring to this article. 

Here is the Preview Image of the Project we are going to make:


Image gallery Preview


  • The Image Gallery will display all the image in small size and when you click on the particular image it will expand and you can see it in large size.
  • To create this we will first use the CSS grid: property to make all the images in grid layout by creating a simple HTML structure.
  • After designing the basic HTML structure we will use CSS properties to make images responsive.
  • Now, we will use the CSS transform and cursor: Zoom-in property, to view the image in full-size when you click on the particular image.

Before the code, you just need to import the following code in your CSS file for the fonts:

@import url(';300;400;500;600;700&display=swap

Example Code: Below is the step by step implementation.

Step 1: The HTML code is used to create the basic structure of Image Gallery page. Since it does not contain CSS so it is just a basic structure. We will use some CSS property to make it attractive.


<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="style.css">
    <title>Image Gallery</title>
  <!-- Heading Name -->
  <div class="heading">
    <h1>Image Gallery</h1>
    <!-- Image Gallery section all image in one div -->
    <div class="gallery">
<img src=
alt="Image 2" class="gallery-img" onclick="showImage(src)">
alt="Image 3" class="gallery-img" onclick="showImage(src)">
pexels-pixabay-268533.jpg&fm=jpg" alt="Image 4" class="gallery-img" onclick="showImage(src)">
pexels-pixabay-268533.jpg&fm=jpg" alt="Image 5" class="gallery-img" onclick="showImage(src)">
alt="Image 6" class="gallery-img" onclick="showImage(src)">
alt="Image 7" class="gallery-img" onclick="showImage(src)">
alt="Image 8" class="gallery-img" onclick="showImage(src)">
alt="Image 9" class="gallery-img" onclick="showImage(src)">
alt="Image 10" class="gallery-img" onclick="showImage(src)">        
  <!-- Image containter where image will show in big size -->
      <div class="image-popup-container" id="imagePopup">
        <span class="close-button" onclick="closeImage()">×</span>
        <img src="" alt="Popup Image" id="popupImage">
    <script src="script.js"></script>


Step 2: Now that, we have create the basic structure of our website, we will now use the CSS property to make it more attractive.


/* Import google font */
@import url(
body {
    font-family: Arial, sans-serif;
    margin: 0;
    padding: 0;
    background-color: #f4f4f4;
  /* Styline the Heading of Image Gallery */
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 30px;
    background: #121FCF;
    background: linear-gradient(to right, #0e85e0 0%, #26ff1a 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
/* Styling gallery section where all images are */
  .gallery {
    width: 90%;
    margin:0 auto;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    grid-gap: 20px;
    align-items: center;
  /* Styling Particular Image */
  .gallery-img {
    width: 200px;
    height: 200px;
    cursor: pointer;
    transition: transform 0.2s;
  /* onHover image will expand little bit */
  .gallery-img:hover {
    transform: scale(1.1);
    cursor: zoom-in;
  /* This section will be seen when we click on image */
  .image-popup-container {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.8);
  /* close button when we want to close the bigger image */
  .close-button {
    position: absolute;
    top: 20px;
    right: 50px;
    color: #fff;
    cursor: pointer;
    color: red;
  /* when we click on the image it will expand in bigger size and will displayed
at middle of screen */
  #popupImage {
    display: block;
    max-width: 80%;
    max-height: 80%;
    margin: 0 auto;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
  /* Making images more responsive for smaller size device */
  @media (max-width:670px) {
        grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
        grid-gap: 10px;
        height: 150px;
        font-size: 20px;


Step 3: After styling the website, we will use Javascript to display images and hide the images when we click on them.


// function to show image when we click on a image
function showImage(imageSrc) {
  let popupImage = document.getElementById("popupImage");
  popupImage.src = imageSrc;
  let imagePopup = document.getElementById("imagePopup"); = "block"; = "hidden";
// function to hide the image when we click on cross button
function closeImage() {
  let imagePopup = document.getElementById("imagePopup"); = "none"; = "auto";




Image Gallery using HTML, CSS, and JavaScript

Now, as you can see in the output, we have created a mage gallery using HTML, CSS, and JavaScript which will help you to create some small website projects.

Last Updated : 24 Aug, 2023
Like Article
Save Article
Share your thoughts in the comments
Similar Reads