How to create a modal image gallery with CSS and JavaScript?

A modal image gallery allows users to view thumbnail images and click on them to open a larger version in a modal overlay. This creates an elegant way to showcase multiple images without cluttering the page layout.

Syntax

/* Modal container */
.modal {
    display: none;
    position: fixed;
    z-index: 1;
    background-color: rgba(0, 0, 0, 0.9);
}

/* Modal image */
.modalImage {
    display: block;
    margin: auto;
}

Example

The following example creates a modal image gallery with thumbnails that open in a modal when clicked −

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
        * {
            box-sizing: border-box;
        }
        
        h1 {
            text-align: center;
            color: #333;
        }
        
        .gallery {
            display: flex;
            gap: 10px;
            justify-content: center;
            flex-wrap: wrap;
            margin: 20px;
        }
        
        .ImgThumbnail {
            border-radius: 8px;
            cursor: pointer;
            transition: transform 0.3s ease;
            height: 200px;
            width: 200px;
            object-fit: cover;
            border: 2px solid #ddd;
        }
        
        .ImgThumbnail:hover {
            transform: scale(1.05);
            border-color: #007bff;
        }
        
        .modal {
            display: none;
            position: fixed;
            z-index: 1000;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.9);
            animation: fadeIn 0.3s ease;
        }
        
        @keyframes fadeIn {
            from { opacity: 0; }
            to { opacity: 1; }
        }
        
        .modalImage {
            display: block;
            margin: 50px auto;
            max-width: 80%;
            max-height: 80%;
            border-radius: 8px;
        }
        
        .close {
            position: absolute;
            top: 20px;
            right: 35px;
            color: #fff;
            font-size: 40px;
            font-weight: bold;
            cursor: pointer;
            transition: color 0.3s ease;
        }
        
        .close:hover {
            color: #ff4444;
        }
        
        @media (max-width: 768px) {
            .ImgThumbnail {
                width: 150px;
                height: 150px;
            }
            .modalImage {
                max-width: 95%;
                margin: 20px auto;
            }
        }
    </style>
</head>
<body>
    <h1>Image Modal Gallery</h1>
    
    <div class="gallery">
        <img class="ImgThumbnail" src="https://picsum.photos/400/300?random=1" alt="Nature 1">
        <img class="ImgThumbnail" src="https://picsum.photos/400/300?random=2" alt="Nature 2">
        <img class="ImgThumbnail" src="https://picsum.photos/400/300?random=3" alt="Nature 3">
    </div>
    
    <div class="modal">
        <span class="close">&times;</span>
        <img class="modalImage">
    </div>
    
    <script>
        const modal = document.querySelector('.modal');
        const modalImage = document.querySelector('.modalImage');
        const closeBtn = document.querySelector('.close');
        const thumbnails = document.querySelectorAll('.ImgThumbnail');
        
        // Open modal when thumbnail is clicked
        thumbnails.forEach(thumbnail => {
            thumbnail.addEventListener('click', function() {
                modal.style.display = 'block';
                modalImage.src = this.src;
                modalImage.alt = this.alt;
            });
        });
        
        // Close modal when X is clicked
        closeBtn.addEventListener('click', function() {
            modal.style.display = 'none';
        });
        
        // Close modal when clicking outside the image
        modal.addEventListener('click', function(e) {
            if (e.target === modal) {
                modal.style.display = 'none';
            }
        });
        
        // Close modal with Escape key
        document.addEventListener('keydown', function(e) {
            if (e.key === 'Escape' && modal.style.display === 'block') {
                modal.style.display = 'none';
            }
        });
    </script>
</body>
</html>
Three thumbnail images are displayed in a centered row. When you click on any thumbnail, a modal overlay appears with a dark background showing the clicked image in a larger size. A white 'X' button in the top-right corner allows closing the modal. The modal can also be closed by clicking outside the image or pressing the Escape key.

Key Features

  • Responsive Design: The gallery adapts to different screen sizes
  • Smooth Transitions: Hover effects and fade-in animation enhance user experience
  • Multiple Close Methods: Close button, outside click, and Escape key
  • Flexible Layout: Uses flexbox for easy thumbnail arrangement

Conclusion

This modal image gallery provides an elegant solution for displaying multiple images. The combination of CSS animations and JavaScript event handling creates a smooth, professional user experience that works well on both desktop and mobile devices.

Updated on: 2026-03-15T14:23:39+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements