Thursday, February 15, 2024
HomeCSSHow to Build a Simple Image Gallery Using HTML, CSS and JavaScript...

How to Build a Simple Image Gallery Using HTML, CSS and JavaScript | Beginner JavaScript Project

Hello Coders, In this article we are going to learn how to create a simple image gallery using HTML, CSS and JavaScript. This tutorial has complete step by step video tutorial as well as source code of this project.

This is going to be a very simple and easy to follow tutorial for beginners. For your reference I have also provided the source code and you can download it at the end of this article.

Learn How to Create a Simple Image Gallery Using HTML, CSS and JavaScript | Step by Step Video Tutorial

Build a Simple Image Gallery Using HTML, CSS and JavaScript | Step by Step Tutorial with Source Code

Lets start by creating three files that are required for this project: index.html, styles.css and script.js. Don’t forget to link you css file and script file to your html.

Step 1: Setting up the HTML

As usual we will add the boiler plate of the html. First create a div with class name main-gallery and inside that add a main-image class to the image which will be displayed bigger. Now Create a div with class thumbnail-container. Inside this div we will add 5 images. For the images we will add the class name thumbnail.

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>5 Rotating Image Gallery</title>
    <link rel="stylesheet" href="styles.css" />
    <div id="image-gallery">
      <img id="main-image" src="" alt="image1" />
      <div id="thumbnail-container">
        <img class="thumbnail" src="" alt="image1" />
        <img class="thumbnail" src="" alt="image2" />
        <img class="thumbnail" src="" alt="image3" />
        <img class="thumbnail" src="" alt="image4" />
        <img class="thumbnail" src="" alt="image5" />

    <script src="script.js"></script>

Step 2: Adding Styles

Inside our css file add the following styles.

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;

body {
  max-width: 900px;
  margin: 0 auto;
  background-color: #ebebeb;

#image-gallery {
  width: 35rem;
  margin: 4rem auto;

#main-image {
  width: 100%;
  background-color: #fff;
  padding: 1rem;
  box-shadow: rgba(0, 0, 0, 0.08) 0px 4px 12px;

#thumbnail-container {
  display: flex;
  justify-content: center;
  margin-top: 2rem;

.thumbnail {
  width: 100px;
  height: 100px;
  margin: 0 10px;
  cursor: pointer;

.thumbnail:hover {
  opacity: 0.7;

Step 3: Adding JavaScript

For JavaScript we will get two DOM elements and store them in thumbnails and mainImage variables. thumbnails will contain all the image elements since we are using querySelectorAll which will return an array of all the elements having the class name thumbnail. mainImage will have the main big image that will be displayed there once clicked.

We will add a click addEventListener to all the thumbnails and add the src attribute of that thumbnail to the main image when the user clicks on them.

var thumbnails = document.querySelectorAll(".thumbnail")
var mainImage = document.querySelector("#main-image")

for (var i = 0; i < thumbnails.length; i++) {
  thumbnails[i].addEventListener("click", function () {
    mainImage.src = this.src

Lets gooo! Congrats on Completing this project. You can get the source code from below. Also don’t forget to check our other JavaScript Project Tutorials. Thank You



Please enter your comment!
Please enter your name here

Latest Posts