DEV Community

Cover image for Build a Responsive Image Gallery Using Flexbox and Grid
HexShift
HexShift

Posted on

1

Build a Responsive Image Gallery Using Flexbox and Grid

Build a Responsive Image Gallery Using Flexbox and Grid

Looking for a clean, responsive image gallery for your portfolio or project? In this tutorial, we’ll walk through how to build one using just HTML and CSS — no JavaScript, no libraries, just layout magic. We'll combine the power of Flexbox for structure and CSS Grid for responsive image placement.

Step 1: Set Up the Basic HTML

We’ll start with a simple container and image elements. You can replace the image URLs with your own or use placeholders like the ones shown here.

<div class="gallery">
  <img src="https://picsum.photos/seed/1/300" alt="Gallery image">
  <img src="https://picsum.photos/seed/2/300" alt="Gallery image">
  <img src="https://picsum.photos/seed/3/300" alt="Gallery image">
  <img src="https://picsum.photos/seed/4/300" alt="Gallery image">
  <img src="https://picsum.photos/seed/5/300" alt="Gallery image">
  <img src="https://picsum.photos/seed/6/300" alt="Gallery image">
</div>

Step 2: Style the Gallery With CSS Grid

Now let’s apply grid layout styles to the container so the images arrange themselves cleanly across different screen sizes.

.gallery {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 12px;
  padding: 20px;
  max-width: 1000px;
  margin: auto;
}

.gallery img {
  width: 100%;
  display: block;
  border-radius: 8px;
  object-fit: cover;
}

This setup ensures:

  • Images stretch to fit their container
  • The grid adapts to the screen size
  • Spacing and alignment look clean at any width

Step 3: Make It Look Even Better

You can easily add hover effects, light/dark mode tweaks, or even a lightbox script later if you want interactivity. Here’s a quick hover effect:

.gallery img:hover {
  transform: scale(1.03);
  transition: transform 0.2s ease-in-out;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.2);
}

Conclusion

You now have a flexible, elegant image gallery built entirely with CSS Grid and Flexbox principles. This type of layout is ideal for showcasing portfolio pieces, product photos, or blog content blocks.

Want more tutorials like this? Check out the rest of my dev.to posts here.

If this guide helped you, you can support my work with a coffee:
https://buymeacoffee.com/hexshift

Quadratic AI

Quadratic AI – The Spreadsheet with AI, Code, and Connections

  • AI-Powered Insights: Ask questions in plain English and get instant visualizations
  • Multi-Language Support: Seamlessly switch between Python, SQL, and JavaScript in one workspace
  • Zero Setup Required: Connect to databases or drag-and-drop files straight from your browser
  • Live Collaboration: Work together in real-time, no matter where your team is located
  • Beyond Formulas: Tackle complex analysis that traditional spreadsheets can't handle

Get started for free.

Watch The Demo 📊✨

Top comments (0)

Jetbrains Survey

Calling all developers!

Participate in the Developer Ecosystem Survey 2025 and get the chance to win a MacBook Pro, an iPhone 16, or other exciting prizes. Contribute to our research on the development landscape.

Take the survey

AWS Security LIVE!

Hosted by security experts, AWS Security LIVE! showcases AWS Partners tackling real-world security challenges. Join live and get your security questions answered.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️