How to create Photo Gallery using HTML and CSS

Photo Gallery | Coding Zemigle

Creating a photo gallery with HTML and CSS is an excellent way to showcase your images in a visually appealing and structured format. Whether you're designing a portfolio, a website for a client, or simply want to display personal photos, understanding the core HTML and CSS techniques involved is essential.

In this tutorial, we’ll walk through the steps to create a responsive and modern photo gallery, complete with hover effects, a grid layout, and responsive design principles.


Step 01: HTML Setup for a Photo Gallery

We’ll start by creating the HTML structure for the gallery. Here’s a simple example:

✲    index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Advanced Grid Photo Gallery</title>
    <link rel="stylesheet" href="style.css">
    <!-- Lightbox CSS -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/lightbox2/2.11.3/css/lightbox.min.css">
</head>
<body>
    <div class="gallery-container">
        <div class="gallery-item wide">
            <a href="image1.jpg" data-lightbox="gallery">
                <img src="/images/img1.png" alt="Image 1">
            </a>
        </div>
        <div class="gallery-item">
            <a href="image2.jpg" data-lightbox="gallery">
                <img src="/images/img2.png" alt="Image 2">
</a> </div> <div class="gallery-item tall"> <a href="image3.jpg" data-lightbox="gallery"> <img src="/images/img3.png" alt="Image 3">
</a> </div> <div class="gallery-item wide"> <a href="image4.jpg" data-lightbox="gallery"> <img src="/images/img4.png" alt="Image 4">
</a> </div> <div class="gallery-item"> <a href="image5.jpg" data-lightbox="gallery"> <img src="/images/img5.png" alt="Image 5">
</a> </div> <div class="gallery-item"> <a href="image6.jpg" data-lightbox="gallery"> <img src="/images/img6.png" alt="Image 6">
</a> </div> <div class="gallery-item"> <a href="image7.jpg" data-lightbox="gallery"> <img src="/images/img7.png" alt="Image 7">
</div> <div class="gallery-item"> <a href="image8.jpg" data-lightbox="gallery"> <img src="/images/img8.png" alt="Image 8">
</div> <!-- Add more images as needed --> </div> <!-- Lightbox JS --> <script src="https://cdnjs.cloudflare.com/ajax/libs/lightbox2/2.11.3/js/lightbox.min.js"></script> </body> </html>



HTML structure: We use a div with the class gallery to contain all the images. Each image is wrapped in a div with the class gallery-item.

Images: Each image has an alt attribute for accessibility.


Step 02: CSS for Styling the Photo Gallery

Now that the HTML structure is ready, let’s move on to styling the gallery with CSS. We’ll apply a grid layout for our gallery, which allows for flexible and responsive designs.

Here’s the basic CSS:

✲    style.css
* {
    box-sizing: border-box;
}

body {
    margin: 0;
    font-family: 'Roboto', sans-serif;
    background-color: #f0f0f0;
}

.gallery-container {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(250px, 2fr));
    grid-gap: 15px;
    padding: 20px;
    max-width: 100%;
    margin: 0 auto;
}

.gallery-item {
    position: relative;
    overflow: hidden;
    cursor: pointer;
    transition: transform 0.3s ease;
}

.gallery-item img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
    transition: transform 0.4s ease, filter 0.3s ease;
}

.gallery-item.wide {
    grid-column: span 1;
}

.gallery-item.tall {
    grid-row: span 2;
}

.gallery-item:hover {
    transform: scale(1.05);
    z-index: 1;
}

.gallery-item img:hover {
    transform: scale(1.1);
    filter: brightness(80%);
}

@media (max-width: 768px) {
    .gallery-item.wide, .gallery-item.tall {
        grid-column: span 1;
        grid-row: span 1;
    }
}

@media (max-width: 480px) {
    .gallery-container {
        grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
    }
}



Grid layout: We use grid-template-columns with the repeat() function to create a responsive grid. The auto-fit property ensures the grid adapts to different screen sizes, while minmax() ensures each image takes up a minimum width of 200px.

Gap: The gap property adds spacing between the images.

Hover effect: When a user hovers over an image, it scales up slightly, adding a simple but attractive animation.


Border radius: This makes the images have rounded corners, giving them a softer, modern look.

Conclusion

Creating a photo gallery using HTML and CSS is straightforward and flexible. By utilizing CSS Grid, you can ensure your gallery adapts beautifully across different screen sizes. The hover effects and responsive design provide a modern user experience, making your gallery visually appealing and easy to navigate.

You can further enhance this by integrating JavaScript for more advanced features, such as a lightbox or lazy loading for images. For now, the simple combination of HTML and CSS provides a solid foundation for displaying your photos in a clean, stylish way.

With these steps, you're well on your way to building a responsive and elegant photo gallery that can be adapted for any website or project. Happy coding!