← Back to CSS
74

Gallery component: adaptive grid, ratio and states

Build a modern gallery with CSS Grid, stable cropping and accessible micro-interactions, so image collections stay clean across different screen widths and mixed source formats.

📘 Theory

The base layout

Let the grid adapt instead of micromanaging every breakpoint.

1

Use a minimum card width and let the browser calculate how many columns fit in the available space.

2

Keep the gap consistent and avoid fixed image tile widths unless the design really depends on them.

Keeping thumbnails visually consistent

Mixed source images still need one stable visual rhythm.

1

`aspect-ratio` defines the media frame, while `object-fit: cover` tells the browser how to crop the image without distorting it.

2

You can also add an overlay on hover for a title or action affordance without pulling extra JavaScript into a relatively simple pattern.

Galleries that need to fill the whole canvas

Sometimes the goal is not just listing images, but constructing a continuous visual surface.

Some galleries behave almost like a backdrop or a visual mosaic. In those cases, the number of cells and the rhythm of the layout matter as much as the individual images themselves.

Grid can define the structure while `background-size`, `background-position` or `aspect-ratio` maintain consistency. If the gallery must fit an exact set of images across the whole viewport, a tiny amount of JavaScript can calculate the distribution while CSS still owns the cropping, spacing and hover states.

  • Use `aspect-ratio` when every cell should preserve a fixed proportion.
  • Use `background-image` and `background-size: cover` when full-surface fill matters more than preserving the original media frame.
  • Keep the gap small and regular so the grid reads as one system instead of isolated cards.

🧪 Learn by doing

Example Demo: responsive gallery An auto-fit grid with consistent thumbnails and a subtle hover zoom.
Example Interactive demo: square-cell gallery with exact fill Explore a mosaic that calculates rows and columns to occupy the full canvas while keeping square cells and clean overlays.
Example Interactive demo: full-canvas image mosaic See how a gallery can use `background-image` to fill the surface without losing the overall rhythm of the grid.
Example Interactive demo: closed grid of 60 thumbnails Study a fixed-count gallery where the grid searches for a strong row-column distribution without losing square cells or visual rhythm.

🏁 Challenges

Challenge Challenge: improve the thumbnails Apply a 1:1 ratio and clean cropping so the gallery feels uniform instead of accidental.

🧰 Resources

Test

Check your knowledge with a test about CSS.

Test for CSS
CodePen: UI cards with hover and layout patterns
Open in CodePen

What is this?

I'm Cristian Eslava and I sometimes build websites so both you and I can learn and experiment. culTest

I made this in February 2026 to make learning easier for my students. The idea is to learn web development by practicing and to keep expanding the project with new topics, tests and challenges.

It draws inspiration from MDN, W3Schools, CodePen, Manz and many other web development references. I wanted to combine useful theory, runnable examples, challenges and the testing system I had already built for culTest. culTest

If you liked it, if you didn't, or if you want to get in touch, write to me at cristianeslava@gmail.com