Create Gallery in HTML, CSS Using Grid

By Maulik Paghdal

05 Aug, 2024

Create Gallery in HTML, CSS Using Grid

Introduction

Creating a responsive gallery is an essential skill for web developers. With the advent of CSS Grid, building complex and flexible layouts has become easier than ever. In this tutorial, we’ll walk you through the steps to create a gallery using HTML and CSS Grid that adapts beautifully across devices.

What is CSS Grid?

CSS Grid is a powerful layout system designed to handle two-dimensional layouts. It allows you to define rows and columns, making it easier to design complex interfaces without relying heavily on float or flexbox.

Setting Up the HTML Structure

We’ll start by creating a simple HTML structure for our gallery. Each image will be wrapped in a div to make it easier to style.


Gallary Preview:

Gallery using css grid


Example

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Responsive Gallery</title>
  <link rel="stylesheet" href="styles.css">
</head>
<body>
  <div class="gallery">
    <div class="gallery-item"><img src="image1.jpg" alt="Gallery Item 1"></div>
    <div class="gallery-item"><img src="image2.jpg" alt="Gallery Item 2"></div>
    <div class="gallery-item"><img src="image3.jpg" alt="Gallery Item 3"></div>
    <div class="gallery-item"><img src="image4.jpg" alt="Gallery Item 4"></div>
    <div class="gallery-item"><img src="image5.jpg" alt="Gallery Item 5"></div>
    <div class="gallery-item"><img src="image6.jpg" alt="Gallery Item 6"></div>
  </div>
</body>
</html>

Now that we have the HTML structure, let’s style it using CSS Grid. We’ll define the layout for desktop screens and ensure it remains responsive for smaller devices.

Example CSS

/* Reset Styles */
body {
  margin: 0;
  font-family: Arial, sans-serif;
}

.gallery {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 16px;
  padding: 16px;
}

.gallery-item {
  overflow: hidden;
  border-radius: 8px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.gallery-item img {
  width: 100%;
  height: auto;
  display: block;
  object-fit: cover;
}

Explanation

  1. **grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));**: This line creates a grid layout that automatically adjusts the number of columns based on available space. Each column will be at least 150px wide but can grow to fill the remaining space.
  2. **gap: 16px;**: Adds spacing between grid items.
  3. **box-shadow** and **border-radius**: Add a polished look to each gallery item.

The CSS Grid layout is inherently responsive, but you can further optimize it for smaller screens by tweaking the minmax value or introducing media queries.

Example Media Query

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

This media query ensures the gallery adapts seamlessly on smaller devices.

Adding Interactivity with CSS

To make the gallery more engaging, let’s add a hover effect that slightly scales the images.

Example CSS

.gallery-item:hover img {
  transform: scale(1.05);
  transition: transform 0.3s ease-in-out;
}

This effect gives users visual feedback when they hover over a gallery item.

Conclusion

CSS Grid simplifies the process of creating responsive and visually appealing galleries. With just a few lines of CSS, you can build layouts that adapt beautifully across devices. Try implementing this gallery in your next project and experiment with different grid configurations to unleash the full potential of CSS Grid!

Happy coding! 🎨