CSS Grid Photo Gallery Generator
A responsive image grid with featured tiles that span two cells, and no media queries needed for the columns.
Named areas. The CSS is a picture of the layout, and only the properties that actually change get repeated inside each media query.
.gallery {
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: auto auto auto;
gap: 8px;
grid-template-areas:
"feature feature"
"shot-a shot-b"
"shot-c shot-d";
}
.gallery > .feature { grid-area: feature; }
.gallery > .shot-a { grid-area: shot-a; }
.gallery > .shot-b { grid-area: shot-b; }
.gallery > .shot-c { grid-area: shot-c; }
.gallery > .shot-d { grid-area: shot-d; }
@media (min-width: 800px) {
.gallery {
grid-template-columns: 1fr 1fr 1fr 1fr;
grid-template-rows: 180px 180px;
gap: 12px;
grid-template-areas:
"feature feature shot-a shot-b"
"feature feature shot-c shot-d";
}
}
How this layout works
The column count here is not set by a breakpoint at all: repeat(auto-fill, minmax(180px, 1fr)) fits as many 180px-or-wider columns as the container allows. Resize the window and the count changes on its own.
Featured tiles span two columns and two rows. Keep the spans modest — a tile that spans more columns than exist at a narrow width will force an overflow, so pair large spans with a breakpoint that guarantees the room.
Set aspect-ratio on the images and object-fit: cover so mismatched source sizes still produce clean rows. Grid controls the cells; the image element controls what happens to the photo inside one.
Every property used here is listed in the CSS Grid cheat sheet, with the reason to reach for it.
Frequently asked questions
How do I make a photo gallery grid without media queries?
repeat(auto-fill, minmax(180px, 1fr)). The browser fits as many 180px-or-wider columns as the container allows and reflows on its own, so the column count follows the container rather than the window.How do I keep gallery images the same size when the sources differ?
aspect-ratio on the image and object-fit: cover. Grid sizes the cell; the image element decides how the photo fills it. Without object-fit the images stretch.How do I center a grid in CSS?
justify-content: center on the grid container to centre the whole grid inside it, and margin-inline: auto with a max-width to centre the container in the page. These are different jobs: the first moves the tracks within the container, the second moves the container itself.How do I center items inside a CSS grid?
place-items: center on the container. That is shorthand for align-items plus justify-items, and it centres every item inside its own cell on both axes. For a single item, use place-self: center on that item instead.How do I make a CSS grid responsive?
grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)) and it reflows at every width with no media queries at all. Add breakpoints only when the exact column count matters, or when the layout has to change shape rather than just wrap.When should I use CSS Grid instead of Flexbox?
How to use it
-
Open the pattern
The photo gallery grid loads into the editor already responsive, with every breakpoint set up.
-
Change the tracks
Edit the column and row sizes in the Tracks panel. Anything valid in a track list works.
-
Redraw at another width
Move the ruler past a breakpoint marker and rearrange. Only the placement changes.
-
Copy the code
Take CSS, HTML, Tailwind or a shadcn component. The media queries are already written.
More in Editorial
- Magazine Layout
- Blog Post with Sticky Contents
- Article With Sidebar
- News Homepage
- Portfolio Grid
- Alternating Timeline