Open In App
Related Articles

How to make image Gallery using HTML CSS and JavaScript ?

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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:


Screenshot-2023-08-21-160023

Image gallery Preview


Approach:

  • 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('
https://fonts.googleapis.com/css2?family=Yaldevi:wght@200;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.

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <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>
</head>
<body>
  <!-- Heading Name -->
  <div class="heading">
    <h1>Image Gallery</h1>
  </div>
    <!-- Image Gallery section all image in one div -->
    <div class="gallery">
<img src=
b=1&s=170667a&w=0&k=20&c=0FSPkjeK1u_EksXE1VrATh7MD74dnFiVmMmaMhtSYwI="
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)">
b=1&s=170667a&w=0&k=20&c=0FSPkjeK1u_EksXE1VrATh7MD74dnFiVmMmaMhtSYwI="
alt="Image 6" class="gallery-img" onclick="showImage(src)">
alt="Image 7" class="gallery-img" onclick="showImage(src)">
b=1&s=170667a&w=0&k=20&c=0FSPkjeK1u_EksXE1VrATh7MD74dnFiVmMmaMhtSYwI="
alt="Image 8" class="gallery-img" onclick="showImage(src)">
alt="Image 9" class="gallery-img" onclick="showImage(src)">
b=1&s=170667a&w=0&k=20&c=0FSPkjeK1u_EksXE1VrATh7MD74dnFiVmMmaMhtSYwI="
alt="Image 10" class="gallery-img" onclick="showImage(src)">        
</div>
   
  <!-- 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">
      </div>
    <script src="script.js"></script>
</body>
</html>

                    

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.

style.css

/* Import google font */
@import url(
'https://fonts.googleapis.com/css2?family=Yaldevi:wght@200;300;400;500;
600;700&display=swap');
 
body {
    font-family: Arial, sans-serif;
    margin: 0;
    padding: 0;
    background-color: #f4f4f4;
  }
  /* Styline the Heading of Image Gallery */
  .heading{ 
    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;
    display:grid;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    grid-gap: 20px;
    justify-content:center;
    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;
    font-size:60px;
    color: #fff;
    cursor: pointer;
  }
  .close-button:hover{
    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) {
    .gallery{
        grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
        grid-gap: 10px;
    }
    .gallery-img{
        width:150px;
        height: 150px;
    }
 
    .heading{
        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.

Javascript

// 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");
  imagePopup.style.display = "block";
  document.body.style.overflow = "hidden";
}
// function to hide the image when we click on cross button
function closeImage() {
  let imagePopup = document.getElementById("imagePopup");
  imagePopup.style.display = "none";
 document.body.style.overflow = "auto";
}

                    

Output:


imageGalleryGIF

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
Previous
Next
Share your thoughts in the comments
Similar Reads