How to create image gallery in html

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 image gallery in html code. 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 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.

  • How to start with HTML?
  • What is CSS?
  • What is JavaScript?

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="container">    <div class="desc">     <div class="tittle">       <h1 class="judul">This is galery</h1>       <p class="text">Lorem ipsum dolor sit amet, consectetur adipiscing elit,Lorem ipsum dolor sit amet, consectetur adipiscing elit,Lorem ipsum dolor sit amet, consectetur adipiscing elit,</p>     </div>      <div class="imgsrc">       <img src="https://images.unsplash.com/photo-1529563021893-cc83c992d75d?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60" class="prev">     </div>   </div>    <div class="thumbnail">     <img src="https://images.unsplash.com/photo-1529563021893-cc83c992d75d?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60" class="thumb">      <img src="https://images.unsplash.com/photo-1512895356176-c49e710676ac?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60" class="thumb">      <img src="https://images.unsplash.com/photo-1518731683836-4e9cce00ba49?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60" class="thumb">      <img src="https://images.unsplash.com/photo-1554205163-2b386a29d718?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60" class="thumb">      <img src="https://images.unsplash.com/photo-1449480881392-716d0ea24a47?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60" class="thumb">      <img src="https://images.unsplash.com/photo-1523539693385-e5e891eb4465?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60" class="thumb">    </div> </div> </body>              

Step 2 – Add CSS

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

<style> * {   margin: 0;   padding: 0;   box-sizing: border-box; }  body {   text-align: center;   background-color: beige;   font-family: "Poppins", sans-serif; }  .container {   margin: 3em auto;   width: 610px;   border: solid white 5px;   border-radius: 30px;   box-shadow: 5px 5px 25px rgba(0, 0, 0, 0.5);   overflow: hidden; } .container .thumbnail {   display: flex;   flex-wrap: wrap; } .container .thumbnail .thumb {   height: 200px;   width: 200px;   transition: all 0.2s;   object-fit: cover;   cursor: pointer; } .container .thumbnail .thumb:hover {   box-shadow: 3px 3px 20px;   opacity: 0.8; }  .desc {   position: relative;   width: 600px;   height: 400px;   overflow: hidden; } .desc:hover .judul {   transform: translateY(80px); } .desc:hover .tittle {   background: linear-gradient(180deg, rgba(0, 0, 0, 0.8) 0%, rgba(0, 0, 0, 0.3) 30%, rgba(0, 0, 0, 0.3) 70%, rgba(0, 0, 0, 0.8) 100%);   opacity: 1; } .desc:hover .text {   transform: translateY(-150px); } .desc .prev {   object-fit: cover;   width: 600px;   height: 400px; } .desc .tittle {   margin: auto;   overflow: hidden;   width: 100%;   height: 100%;   position: absolute;   transition: all 0.3s ease;   opacity: 0;   display: flex;   flex-direction: column; } .desc .tittle .judul {   color: white;   padding: 20px;   margin-top: -40px;   transition: all 0.3s ease; } .desc .tittle .text {   color: white;   margin-top: 350px;   padding: 20px;   transition: all 0.35s ease 0.4s; }  @keyframes fade {   to {     opacity: 1;   } } .effect {   opacity: 0;   animation: fade 0.2s forwards; }  .active {   opacity: 0.5; }  </style>              

Step 2 – Add JavaScript

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

<script> const container = document.querySelector(".container"); const preview = document.querySelector(".prev"); const active = document.querySelectorAll(".thumb");  container.addEventListener("click", function (e) {   if (e.target.className == "thumb") {     preview.src = e.target.src;     preview.classList.add("effect");      setTimeout(function () {       preview.classList.remove("effect");     }, 100);      active.forEach(function (thumb) {       if (thumb.classList.contains("active")) {         thumb.classList.remove("active");       }     });      e.target.classList.add("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.

How to create image gallery in html

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.