GalleryTutorials

Responsive Grid Image Gallery

How to Create Responsive Grid Image Gallery

A gallery is one of the main components of a website, It contains images and a user interface element within a website. Responsive Grid Image Gallery can be an excellent example for an user friendly UI.

Responsive Grid 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 Grid Image Gallery? We are here to solve your problem. In this article we discuss how to create a grid 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 as below.

<body>
 <div class="container">
  <div class="grid">
   <div class="column-xs-12 column-md-4">
    <figure class="img-container">
     <img src="https://cdn.pixabay.com/photo/2023/10/03/08/31/tree-8290838_1280.jpg" />
     <figcaption class="img-content">
      <h2 class="title">Title 1</h2>
      <h3 class="category">Category 1</h3>
     </figcaption>
     <span class="img-content-hover">
      <h2 class="title">Title 1</h2>
      <h3 class="category">Category 1</h3>
     </span>
    </figure>
   </div>
   <div class="column-xs-12 column-md-4">
    <figure class="img-container">
     <img src="https://cdn.pixabay.com/photo/2023/10/03/08/31/tree-8290838_1280.jpg" />
     <figcaption class="img-content">
      <h2 class="title">Title 2</h2>
      <h3 class="category">Category 2</h3>
     </figcaption>
     <span class="img-content-hover">
      <h2 class="title">Title 2</h2>
      <h3 class="category">Category 2</h3>
     </span>
    </figure>
   </div>
   <div class="column-xs-12 column-md-4">
    <figure class="img-container">
     <img src="https://cdn.pixabay.com/photo/2023/10/03/08/31/tree-8290838_1280.jpg">
     <figcaption class="img-content">
      <h2 class="title">Title 3</h2>
      <h3 class="category">Category 3</h3>
     </figcaption>
     <span class="img-content-hover">
      <h2 class="title">Title 3</h2>
      <h3 class="category">Category 3</h3>
     </span>
    </figure>
   </div>
   <div class="column-xs-12 column-md-6">
    <figure class="img-container">
     <img src="https://cdn.pixabay.com/photo/2023/10/03/08/31/tree-8290838_1280.jpg" />
     <figcaption class="img-content">
      <h2 class="title">Title 4</h2>
      <h3 class="category">Category 4</h3>
     </figcaption>
     <span class="img-content-hover">
      <h2 class="title">Title 4</h2>
      <h3 class="category">Category 4</h3>
     </span>
    </figure>
   </div>
   <div class="column-xs-12 column-md-6">
    <figure class="img-container">
     <img src="https://cdn.pixabay.com/photo/2023/10/03/08/31/tree-8290838_1280.jpg" />
     <figcaption class="img-content">
       <h2 class="title">Title 5</h2>
       <h3 class="category">Category 5</h3>
     </figcaption>
     <span class="img-content-hover">
      <h2 class="title">Title 5</h2>
      <h3 class="category">Category 5</h3>
     </span>
    </figure>
   </div>
   <div class="column-xs-12">
    <figure class="img-container">
     <img src="https://cdn.pixabay.com/photo/2023/10/03/08/31/tree-8290838_1280.jpg" />
     <figcaption class="img-content">
      <h2 class="title">Title 6</h2>
      <h3 class="category">Category 6</h3>
     </figcaption>
     <span class="img-content-hover">
      <h2 class="title">Title 6</h2>
      <h3 class="category">Category 6</h3>
     </span>
    </figure>
   </div>
  </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://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">

Copy and paste below code in your HTML editor between <style></style> tag as below.

<style>
            @import url("https://fonts.googleapis.com/css?family=Barlow:400,500,700");

            .container {
                margin: auto;
                padding: 0 1rem;
                max-width: 71.25rem;
                width: 100%;
            }

            .grid {
                display: flex;
                flex-direction: column;
                flex-flow: row wrap;
            }

            .grid>[class*=column-] {
                display: block;
            }

            .first {
                order: -1;
            }

            .last {
                order: 12;
            }

            .align-top {
                align-items: start;
            }

            .align-center {
                align-items: center;
            }

            .align-bottom {
                align-items: end;
            }

            .column-xs-1 {
                flex-basis: 8.3333333333%;
                max-width: 8.3333333333%;
            }

            .column-xs-2 {
                flex-basis: 16.6666666667%;
                max-width: 16.6666666667%;
            }

            .column-xs-3 {
                flex-basis: 25%;
                max-width: 25%;
            }

            .column-xs-4 {
                flex-basis: 33.3333333333%;
                max-width: 33.3333333333%;
            }

            .column-xs-5 {
                flex-basis: 41.6666666667%;
                max-width: 41.6666666667%;
            }

            .column-xs-6 {
                flex-basis: 50%;
                max-width: 50%;
            }

            .column-xs-7 {
                flex-basis: 58.3333333333%;
                max-width: 58.3333333333%;
            }

            .column-xs-8 {
                flex-basis: 66.6666666667%;
                max-width: 66.6666666667%;
            }

            .column-xs-9 {
                flex-basis: 75%;
                max-width: 75%;
            }

            .column-xs-10 {
                flex-basis: 83.3333333333%;
                max-width: 83.3333333333%;
            }

            .column-xs-11 {
                flex-basis: 91.6666666667%;
                max-width: 91.6666666667%;
            }

            .column-xs-12 {
                flex-basis: 100%;
                max-width: 100%;
            }

            @media (min-width: 48rem) {
                .column-sm-1 {
                    flex-basis: 8.3333333333%;
                    max-width: 8.3333333333%;
                }

                .column-sm-2 {
                    flex-basis: 16.6666666667%;
                    max-width: 16.6666666667%;
                }

                .column-sm-3 {
                    flex-basis: 25%;
                    max-width: 25%;
                }

                .column-sm-4 {
                    flex-basis: 33.3333333333%;
                    max-width: 33.3333333333%;
                }

                .column-sm-5 {
                    flex-basis: 41.6666666667%;
                    max-width: 41.6666666667%;
                }

                .column-sm-6 {
                    flex-basis: 50%;
                    max-width: 50%;
                }

                .column-sm-7 {
                    flex-basis: 58.3333333333%;
                    max-width: 58.3333333333%;
                }

                .column-sm-8 {
                    flex-basis: 66.6666666667%;
                    max-width: 66.6666666667%;
                }

                .column-sm-9 {
                    flex-basis: 75%;
                    max-width: 75%;
                }

                .column-sm-10 {
                    flex-basis: 83.3333333333%;
                    max-width: 83.3333333333%;
                }

                .column-sm-11 {
                    flex-basis: 91.6666666667%;
                    max-width: 91.6666666667%;
                }

                .column-sm-12 {
                    flex-basis: 100%;
                    max-width: 100%;
                }
            }

            @media (min-width: 62rem) {
                .column-md-1 {
                    flex-basis: 8.3333333333%;
                    max-width: 8.3333333333%;
                }

                .column-md-2 {
                    flex-basis: 16.6666666667%;
                    max-width: 16.6666666667%;
                }

                .column-md-3 {
                    flex-basis: 25%;
                    max-width: 25%;
                }

                .column-md-4 {
                    flex-basis: 33.3333333333%;
                    max-width: 33.3333333333%;
                }

                .column-md-5 {
                    flex-basis: 41.6666666667%;
                    max-width: 41.6666666667%;
                }

                .column-md-6 {
                    flex-basis: 50%;
                    max-width: 50%;
                }

                .column-md-7 {
                    flex-basis: 58.3333333333%;
                    max-width: 58.3333333333%;
                }

                .column-md-8 {
                    flex-basis: 66.6666666667%;
                    max-width: 66.6666666667%;
                }

                .column-md-9 {
                    flex-basis: 75%;
                    max-width: 75%;
                }

                .column-md-10 {
                    flex-basis: 83.3333333333%;
                    max-width: 83.3333333333%;
                }

                .column-md-11 {
                    flex-basis: 91.6666666667%;
                    max-width: 91.6666666667%;
                }

                .column-md-12 {
                    flex-basis: 100%;
                    max-width: 100%;
                }
            }

            @media (min-width: 75rem) {
                .column-lg-1 {
                    flex-basis: 8.3333333333%;
                    max-width: 8.3333333333%;
                }

                .column-lg-2 {
                    flex-basis: 16.6666666667%;
                    max-width: 16.6666666667%;
                }

                .column-lg-3 {
                    flex-basis: 25%;
                    max-width: 25%;
                }

                .column-lg-4 {
                    flex-basis: 33.3333333333%;
                    max-width: 33.3333333333%;
                }

                .column-lg-5 {
                    flex-basis: 41.6666666667%;
                    max-width: 41.6666666667%;
                }

                .column-lg-6 {
                    flex-basis: 50%;
                    max-width: 50%;
                }

                .column-lg-7 {
                    flex-basis: 58.3333333333%;
                    max-width: 58.3333333333%;
                }

                .column-lg-8 {
                    flex-basis: 66.6666666667%;
                    max-width: 66.6666666667%;
                }

                .column-lg-9 {
                    flex-basis: 75%;
                    max-width: 75%;
                }

                .column-lg-10 {
                    flex-basis: 83.3333333333%;
                    max-width: 83.3333333333%;
                }

                .column-lg-11 {
                    flex-basis: 91.6666666667%;
                    max-width: 91.6666666667%;
                }

                .column-lg-12 {
                    flex-basis: 100%;
                    max-width: 100%;
                }
            }

            @supports (display: grid) {
                .grid {
                    display: grid;
                    grid-template-columns: repeat(12, 1fr);
                    grid-template-rows: auto;
                }

                .grid>[class*=column-] {
                    margin: 0;
                    max-width: 100%;
                }

                .column-xs-1 {
                    grid-column-start: span 1;
                    grid-column-end: span 1;
                }

                .column-xs-2 {
                    grid-column-start: span 2;
                    grid-column-end: span 2;
                }

                .column-xs-3 {
                    grid-column-start: span 3;
                    grid-column-end: span 3;
                }

                .column-xs-4 {
                    grid-column-start: span 4;
                    grid-column-end: span 4;
                }

                .column-xs-5 {
                    grid-column-start: span 5;
                    grid-column-end: span 5;
                }

                .column-xs-6 {
                    grid-column-start: span 6;
                    grid-column-end: span 6;
                }

                .column-xs-7 {
                    grid-column-start: span 7;
                    grid-column-end: span 7;
                }

                .column-xs-8 {
                    grid-column-start: span 8;
                    grid-column-end: span 8;
                }

                .column-xs-9 {
                    grid-column-start: span 9;
                    grid-column-end: span 9;
                }

                .column-xs-10 {
                    grid-column-start: span 10;
                    grid-column-end: span 10;
                }

                .column-xs-11 {
                    grid-column-start: span 11;
                    grid-column-end: span 11;
                }

                .column-xs-12 {
                    grid-column-start: span 12;
                    grid-column-end: span 12;
                }

                @media (min-width: 48rem) {
                    .column-sm-1 {
                        grid-column-start: span 1;
                        grid-column-end: span 1;
                    }

                    .column-sm-2 {
                        grid-column-start: span 2;
                        grid-column-end: span 2;
                    }

                    .column-sm-3 {
                        grid-column-start: span 3;
                        grid-column-end: span 3;
                    }

                    .column-sm-4 {
                        grid-column-start: span 4;
                        grid-column-end: span 4;
                    }

                    .column-sm-5 {
                        grid-column-start: span 5;
                        grid-column-end: span 5;
                    }

                    .column-sm-6 {
                        grid-column-start: span 6;
                        grid-column-end: span 6;
                    }

                    .column-sm-7 {
                        grid-column-start: span 7;
                        grid-column-end: span 7;
                    }

                    .column-sm-8 {
                        grid-column-start: span 8;
                        grid-column-end: span 8;
                    }

                    .column-sm-9 {
                        grid-column-start: span 9;
                        grid-column-end: span 9;
                    }

                    .column-sm-10 {
                        grid-column-start: span 10;
                        grid-column-end: span 10;
                    }

                    .column-sm-11 {
                        grid-column-start: span 11;
                        grid-column-end: span 11;
                    }

                    .column-sm-12 {
                        grid-column-start: span 12;
                        grid-column-end: span 12;
                    }
                }

                @media (min-width: 62rem) {
                    .column-md-1 {
                        grid-column-start: span 1;
                        grid-column-end: span 1;
                    }

                    .column-md-2 {
                        grid-column-start: span 2;
                        grid-column-end: span 2;
                    }

                    .column-md-3 {
                        grid-column-start: span 3;
                        grid-column-end: span 3;
                    }

                    .column-md-4 {
                        grid-column-start: span 4;
                        grid-column-end: span 4;
                    }

                    .column-md-5 {
                        grid-column-start: span 5;
                        grid-column-end: span 5;
                    }

                    .column-md-6 {
                        grid-column-start: span 6;
                        grid-column-end: span 6;
                    }

                    .column-md-7 {
                        grid-column-start: span 7;
                        grid-column-end: span 7;
                    }

                    .column-md-8 {
                        grid-column-start: span 8;
                        grid-column-end: span 8;
                    }

                    .column-md-9 {
                        grid-column-start: span 9;
                        grid-column-end: span 9;
                    }

                    .column-md-10 {
                        grid-column-start: span 10;
                        grid-column-end: span 10;
                    }

                    .column-md-11 {
                        grid-column-start: span 11;
                        grid-column-end: span 11;
                    }

                    .column-md-12 {
                        grid-column-start: span 12;
                        grid-column-end: span 12;
                    }
                }

                @media (min-width: 75rem) {
                    .column-lg-1 {
                        grid-column-start: span 1;
                        grid-column-end: span 1;
                    }

                    .column-lg-2 {
                        grid-column-start: span 2;
                        grid-column-end: span 2;
                    }

                    .column-lg-3 {
                        grid-column-start: span 3;
                        grid-column-end: span 3;
                    }

                    .column-lg-4 {
                        grid-column-start: span 4;
                        grid-column-end: span 4;
                    }

                    .column-lg-5 {
                        grid-column-start: span 5;
                        grid-column-end: span 5;
                    }

                    .column-lg-6 {
                        grid-column-start: span 6;
                        grid-column-end: span 6;
                    }

                    .column-lg-7 {
                        grid-column-start: span 7;
                        grid-column-end: span 7;
                    }

                    .column-lg-8 {
                        grid-column-start: span 8;
                        grid-column-end: span 8;
                    }

                    .column-lg-9 {
                        grid-column-start: span 9;
                        grid-column-end: span 9;
                    }

                    .column-lg-10 {
                        grid-column-start: span 10;
                        grid-column-end: span 10;
                    }

                    .column-lg-11 {
                        grid-column-start: span 11;
                        grid-column-end: span 11;
                    }

                    .column-lg-12 {
                        grid-column-start: span 12;
                        grid-column-end: span 12;
                    }
                }
            }

            * {
                box-sizing: border-box;
            }

            *::before,
            *::after {
                box-sizing: border-box;
            }

            body {
                font-family: "Barlow", sans-serif;
                font-size: 1.125rem;
                line-height: 1.5;
                color: #787878;
                background: #fff;
                text-rendering: optimizeLegibility;
            }

            ul li {
                margin: 0 1.5rem 0 0;
            }

            a {
                color: #787878;
                text-decoration: none;
                transition: all 0.2s ease;
            }

            a:hover {
                color: #2e2e2e;
            }

            a.active {
                color: #2e2e2e;
            }

            nav {
                padding: 2.5rem 0 2.5rem 0;
            }

            nav ul {
                display: flex;
                justify-content: flex-start;
            }

            nav li {
                font-size: 1.25rem;
            }

            nav li:nth-child(2) {
                margin: 0;
            }

            #highlight {
                color: #ea8478;
                font-size: 1.25rem;
            }

            .gallery {
                padding: 0 0 4rem 0;
            }

            .img-container {
                width: 100%;
                height: 500px;
                cursor: pointer;
                overflow: hidden;
            }

            .img-container:hover .img-content-hover {
                display: block;
            }

            img {
                width: 100%;
                height: 500px;
                -o-object-fit: cover;
                object-fit: cover;
                transform: scale(1);
                transition: all 0.3s ease-in-out;
            }

            img:hover {
                transform: scale(1.05);
            }

            .img-content-hover {
                z-index: 1;
                position: absolute;
                top: 0;
                left: 0;
                white-space: nowrap;
                display: none;
                padding: 1rem;
                background: #fff;
                font-weight: 400;
                margin-top: 1.25rem;
                margin-left: -2rem;
            }

            .title {
                color: #2e2e2e;
                font-size: 1.5rem;
                font-weight: 700;
            }

            .category {
                font-size: 1rem;
                color: #787878;
            }

            .img-content {
                display: none;
            }

            .social,
            .copyright {
                margin: 1rem;
            }

            .social li {
                display: inline-block;
            }

            footer {
                padding: 1rem 0;
                background: #f7f7f7;
                text-align: center;
            }

            @supports (display: grid) {
                .gallery .grid {
                    grid-gap: 1rem;
                }
            }

            @media (min-width: 62rem) {
                nav ul {
                    justify-content: flex-end;
                }
            }

            @media screen and (max-width: 1024px) {
                img:hover {
                    transform: none;
                }

                .img-container {
                    height: 100%;
                }

                .img-container:hover .img-content-hover {
                    display: none;
                }

                .img-content {
                    display: block;
                    padding: 1rem 0;
                }
            }
        </style>

Step 3 – Add JavaScript

We use some external JS links to this code. No need to worry about this, copy and paste code between <script></script> tags as below.

<script src="https://code.jquery.com/jquery-3.7.1.min.js" integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo=" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js" integrity="sha384kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4" crossorigin="anonymous"></script>

Copy and paste below code in your HTML editor between <script></script> tag.

<script>
  const imgContent = document.querySelectorAll('.img-content-hover');

  function showImgContent(e) {
   for (var i = 0; i < imgContent.length; i++) {
    x = e.pageX;
    y = e.pageY;
    imgContent[i].style.transform = `translate3d(${x}px, ${y}px, 0)`;
   }
  };

  document.addEventListener('mousemove', showImgContent);
</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.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button