Skip to main content

Lesson 6 of 10

Grid Gallery Layout

Build a responsive image gallery using CSS Grid. This mini-build creates a masonry-like gallery with featured images spanning multiple cells - a pattern used on Pinterest, Instagram, and portfolio sites.

15 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Gallery Techniques

  • Auto-fit grid - Creates responsive columns that adapt without media queries
  • grid-row: span 2 - Featured images take two row heights for visual importance
  • grid-column: span 2 - Wide images take two column widths
  • object-fit: cover - Images fill their grid cells while maintaining aspect ratio
  • border-radius - Rounded corners on all gallery items for modern look
  • Hover effects - Scale and shadow transitions on hover for interactive feel

Mini-Build: Image Gallery

Mini-Build: Image GalleryHTML
HTML
โœ“ Valid
HTML|42 lines|โœ“ Valid syntax
UTF-8

Tip

Combine grid-auto-flow: dense with span classes to create Pinterest-style gallery layouts. Dense packing fills holes left by tall/wide items. Pair it with a fixed grid-auto-rows value so all cells have a consistent base height that tall items can multiply.

Diagram
Loading diagramโ€ฆ

Create dynamic image galleries by spanning featured photos across multiple rows and columns

Common Mistake

Forgetting object-fit: cover on gallery images. Without it, images stretch or squish to fill their grid cell, distorting aspect ratios. Always add object-fit: cover with width: 100%; height: 100% on gallery images so they fill their cell beautifully without distortion.

Practice Task

Customize the gallery in the editor above: (1) Change the grid-auto-rows to 150px, (2) Make 2 more items 'featured' (span 2 columns AND rows), (3) Add an overlay effect on hover using position: absolute + background: rgba overlay, (4) Try removing grid-auto-flow: dense and observe the gaps.

Quick Quiz

Key Takeaways

  • Build a responsive image gallery using CSS Grid.
  • Auto-fit grid - Creates responsive columns that adapt without media queries
  • grid-row: span 2 - Featured images take two row heights for visual importance
  • grid-column: span 2 - Wide images take two column widths

Hands-on Practice Tools for this Course

Test what you learn using these free in-browser developer utilities (no signup required).

All 40+ Tools

Topics in This Module

0/10 done