Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-mini-2024-07-18· OpenAIAi2 WildChat-4.8M (AI2)

I need a simple code for an image gallery on my website

  1. User

    I need a simple code for an image gallery on my website

  2. Assistant

    Creating a simple image gallery for your website can be done using basic HTML, CSS, and a bit of JavaScript. Below is an example of a straightforward image gallery:

    HTML

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Simple Image Gallery</title>
        <link rel="stylesheet" href="styles.css">
    </head>
    <body>
    
        <div class="gallery">
            <img src="image1.jpg" alt="Image 1" class="gallery-item">
            <img src="image2.jpg" alt="Image 2" class="gallery-item">
            <img src="image3.jpg" alt="Image 3" class="gallery-item">
            <img src="image4.jpg" alt="Image 4" class="gallery-item">
            <img src="image5.jpg" alt="Image 5" class="gallery-item">
        </div>
    
        <div id="modal" class="modal">
            <span class="close" id="close">&times;</span>
            <img class="modal-content" id="modalImage">
            <div id="caption"></div>
        </div>
    
        <script src="script.js"></script>
    </body>
    </html>
    

    CSS (styles.css)

    body {
        font-family: Arial, sans-serif;
    }
    
    .gallery {
        display: flex;
        flex-wrap: wrap;
        gap: 10px;
    }
    
    .gallery-item {
        width: calc(33.33% - 10px);
        cursor: pointer;
        filter: brightness(0.9);
        transition: filter 0.3s;
    }
    
    .gallery-item:hover {
        filter: brightness(1);
    }
    
    .modal {
        display: none; /* Hidden by default */
        position: fixed; /* Stay in place */
        z-index: 1000; /* Sit on top */
        left: 0;
        top: 0;
        width: 100%; /* Full width */
        height: 100%; /* Full height */
        overflow: auto; /* Enable scroll if needed */
        background-color: rgba(0, 0, 0, 0.8); /* Black w/ opacity */
        padding: 20px;
    }
    
    .modal-content {
        max-width: 100%;
        max-height: 80%;
        margin: auto;
        display: block;
    }
    
    .close {
        color: white;
        float: right;
        font-size: 28px;
        font-weight: bold;
    }
    
    .close:hover,
    .close:focus {
        color: #bbb;
        text-decoration: none;
        cursor: pointer;
    }
    
    #caption {
        color: white;
        text-align: center;
        margin-top: 10px;
    }
    

    JavaScript (script.js)

    const modal = document.getElementById("modal");
    const modalImage = document.getElementById("modalImage");
    const captionText = document.getElementById("caption");
    const closeBtn = document.getElementById("close");
    
    const galleryItems = document.querySelectorAll(".gallery-item");
    
    galleryItems.forEach(item => {
        item.addEventListener("click", function() {
            modal.style.display = "block";
            modalImage.src = this.src;
            captionText.innerHTML = this.alt;
        });
    });
    
    closeBtn.onclick = function() {
        modal.style.display = "none";
    }
    
    window.onclick = function(event) {
        if (event.target == modal) {
            modal.style.display = "none";
        }
    }
    

    Instructions

    1. File Structure:

      • Create a new directory for your project.
      • Inside that directory, create three files: index.html, styles.css, and script.js.
      • Make sure to replace "image1.jpg", "image2.jpg", etc., with the actual paths to your images in the HTML file.
    2. Open the HTML File:

      • Open index.html in your web browser. You should see a simple image gallery. Clicking an image will open it in a modal, and you can close the modal by clicking the "X" or anywhere outside the modal.

    This example provides a simple and clean gallery layout that you can customize further to meet your website's aesthetic needs.