How to Build a Responsive Image Gallery with Thumbnails & Navigation Arrows

Creating a visually appealing and fully responsive image gallery is a great way to enhance any website. In this step-by-step tutorial, we’ll walk you through how to build a modern image gallery using HTML, CSS, and a bit of JavaScript for smooth navigation.

With this guide, you’ll learn how to implement thumbnail previews and navigation arrows, making it easy for users to browse through images seamlessly. Whether you’re a beginner or an experienced developer, this tutorial will help you create a stylish gallery that adapts perfectly to different screen sizes.

๐ŸŽฏ What Youโ€™ll Learn

โœ… Designing a responsive image gallery layout
โœ… Adding thumbnail previews for quick navigation
โœ… Implementing next/previous navigation arrows
โœ… Optimizing the gallery for mobile-friendly browsing


๐Ÿ“Œ Step 1: Setting Up the HTML Structure

We’ll start by creating the basic structure of our gallery using HTML. This will include:

  • A main image display area
  • Left and right navigation arrows
  • A thumbnail section for selecting images

Hereโ€™s the HTML code for our gallery:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Responsive Image Gallery</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <div class="gallery">
        <div class="main-image">
            <button class="arrow arrow-left" onclick="prevImage()">&#10094;</button>
            <img id="displayedImage" src="1.jpg" alt="Main Image">
            <button class="arrow arrow-right" onclick="nextImage()">&#10095;</button>
        </div>
        <div class="thumbnails">
            <img src="1.jpg" onclick="changeImage(this.src)" alt="Thumbnail 1">
            <img src="2.jpg" onclick="changeImage(this.src)" alt="Thumbnail 2">
            <img src="3.jpg" onclick="changeImage(this.src)" alt="Thumbnail 3">
            <img src="4.jpg" onclick="changeImage(this.src)" alt="Thumbnail 4">
        </div>
    </div>
    <script src="script.js"></script>
</body>
</html>

๐Ÿ“Œ Step 2: Styling the Gallery with CSS

Next, letโ€™s style our gallery to make it visually appealing and responsive. Weโ€™ll use CSS to:

  • Adjust the layout and positioning
  • Style the navigation arrows
  • Add hover effects to thumbnails

Hereโ€™s the CSS code:

body {
    font-family: Arial, sans-serif;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    background-color: #f5f5f5;
}
.gallery {
    position: relative;
    max-width: 600px;
    width: 100%;
}
.main-image {
    position: relative;
}
.main-image img {
    width: 100%;
    height: 400px;
    object-fit: cover;
    border-radius: 10px;
}
.arrow {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    background: rgba(0, 0, 0, 0.5);
    color: white;
    border: none;
    padding: 10px;
    cursor: pointer;
    border-radius: 50%;
}
.arrow-left {
    left: 10px;
}
.arrow-right {
    right: 10px;
}
.thumbnails {
    display: flex;
    justify-content: center;
    gap: 10px;
    margin-top: 10px;
    flex-wrap: wrap;
}
.thumbnails img {
    width: 22%;
    max-width: 100px;
    height: auto;
    aspect-ratio: 4/3;
    object-fit: cover;
    cursor: pointer;
    border-radius: 5px;
    transition: transform 0.3s;
}
.thumbnails img:hover {
    transform: scale(1.1);
}

๐Ÿ“Œ Step 3: Adding Functionality with JavaScript

Now, letโ€™s add JavaScript to enable navigation through the images. The script will:

  • Change the main image when clicking a thumbnail
  • Navigate images using left and right arrows

Hereโ€™s the JavaScript code:

const images = ["1.jpg", "2.jpg", "3.jpg", "4.jpg"];
let currentIndex = 0;

function changeImage(src) {
    document.getElementById('displayedImage').src = src;
    currentIndex = images.indexOf(src);
}

function prevImage() {
    currentIndex = (currentIndex - 1 + images.length) % images.length;
    document.getElementById('displayedImage').src = images[currentIndex];
}

function nextImage() {
    currentIndex = (currentIndex + 1) % images.length;
    document.getElementById('displayedImage').src = images[currentIndex];
}

๐Ÿš€ Final Thoughts

With just HTML, CSS, and a bit of JavaScript, you’ve built a fully responsive image gallery that allows users to navigate images easily using thumbnails and arrows. You can further customize it by adding animations or integrating a lightbox effect!

๐Ÿ“Œ Want the full source code? Check it out here:
๐Ÿ”— GitHub Repository

๐Ÿ“บ Watch the step-by-step video tutorial:

Donโ€™t forget to LIKE ๐Ÿ‘, COMMENT ๐Ÿ’ฌ, and SUBSCRIBE ๐Ÿ”” for more web development tutorials!

#WebDevelopment #ResponsiveDesign #ImageGallery #HTML #CSS #JavaScript


					

Leave a Reply

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