CSS
Build a Responsive Photo Gallery With CSS Grid, Step by Step
ยท 6 min read
To build a responsive photo gallery, resize the photos to one width first, then lay them out with a single CSS Grid rule: grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)). That line fits as many columns as the screen allows, with no media queries. A shadow and a small hover zoom finish the tiles. On Emojar you can do each step with a free tool: the Image Resizer for the photos, the CSS Grid Playground for the layout, the CSS Box Shadow Generator and the CSS Transform Playground for the tiles, and the Lorem Ipsum Generator for test captions.
The example is a small ceramics studio that wants a page of its work: 12 photos of bowls, mugs and vases, taken on a phone at 4032 ร 3024 pixels. One photo, the studio's best-selling vase, should be shown larger than the others. The page is 1,000 pixels wide on a laptop and about 343 on a phone after padding.
How big should the gallery photos be?
Work out the widest a tile will ever be, then double it for high-density phone and laptop screens. The numbers come from the grid in the next section. With columns of at least 160px and a 16px gap, the laptop page fits five columns, each about 187px wide. The large vase spans two columns plus the gap between them, so it's about 390px wide. At double density that's roughly 780 pixels, so 800 pixels wide covers every tile.
A phone photo at 4032 ร 3024 is five times that. Add all 12 photos to the Image Resizer, keep By pixels selected, type 800 into Width and leave Height empty. With Lock aspect ratio (fit inside width ร height) ticked, the tool works out the height for you: 800 ร 3024 รท 4032 = 600. With several files added, the line under the settings shows it for the first one: First image: 4032ร3024 โ 800ร600 px.
Set Output format to WebP, click Resize 12 images, then use Download ZIP to save them all at once. Each file keeps its name with the new size added, so vase.jpg becomes vase-800x600.webp. The resizer works on the photos inside your browser and doesn't upload them.
How do you make a grid that fits any screen width?
Open the Grid Playground and click the Photo gallery (auto-fill) example. It loads 10 items with repeat(auto-fill, minmax(120px, 1fr)) in grid-template-columns and an empty row template, so rows are created as the items need them.
Change 120px to 160px, drag both row-gap and column-gap to 16px, and set Items to 12 to match the studio's photos. Because the two gaps match, the tool writes one gap line:
.container {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
gap: 16px;
}
Each column needs at least 160px, plus 16px for every gap. On the 1,000px laptop page, five columns need 5 ร 160 + 4 ร 16 = 864px and six would need 1,040px, so you get five. The leftover space is shared out by the 1fr, which makes each column (1,000 โ 64) รท 5 = 187.2px. On the 343px phone, two columns need 336px, so the phone shows two columns about 163px wide.
To watch this without resizing your window, drag Preview width from 100% down to its 30% minimum. The small grid line numbers along the top edge update as columns appear and disappear.
How do you make one photo span two columns?
Click item 1 in the preview to select it. Under Item 1 placement, set grid-column-start to 1, grid-column-end to span 2, grid-row-start to 1 and grid-row-end to span 2. The vase now takes a two-by-two block in the top-left corner, and the other 11 photos flow around it. The CSS panel adds:
.item:nth-child(1) {
grid-column: 1 / span 2;
grid-row: 1 / span 2;
}
span 2 means "cover two tracks from wherever you start". Line 1 is the left edge, so the vase runs from line 1 to line 3.
On the smallest screens, if a phone is narrow enough that only one 160px column fits, the vase still asks for two, and the browser adds an extra column to make room, which can push the gallery wider than the screen. Drag Preview width down slowly and see where it breaks. If your narrowest target is below 336px, put the span rule inside a @media (min-width: 400px) block.
How do you add a soft shadow to each tile?
In the Box Shadow Generator, click the Soft preset. It gives one layer with a 10px Y offset, 30px Blur and 12% Opacity:
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.12);
On a 187px tile that much blur spreads a long way, and twelve of them close together look muddy. Move Y offset to 4 and Blur to 12 and the output becomes box-shadow: 0 4px 12px rgba(0, 0, 0, 0.12);. Zero spread is left out, so the line stays short.
The studio's page background is off-white, so set Background to Light grey and slide Card corners to 8px to see the shadow as it'll look on the page. The corner slider only changes the preview. The CSS panel gives you the box-shadow line, so add border-radius: 8px to the tile yourself.
How do you zoom a photo on hover without moving the grid?
Put each image inside a figure with overflow: hidden, then scale the image, not the figure. The figure keeps its size in the grid and the photo grows inside it.
In the Transform Playground, leave Same scale for X and Y ticked and move Scale to 1.08. The tool writes only the functions you changed, plus the same thing as a matrix:
transform: scale(1.08);
/* Same as one matrix: */
transform: matrix(1.08, 0, 0, 1.08, 0, 0);
Use the first line inside .gallery figure:hover img and add transition: transform 0.3s to the plain img rule. A transform doesn't trigger layout, so the tiles around it stay where they are. The default transform-origin is the centre, so the photo zooms evenly toward all four edges. If you'd rather it grow from the top, pick the middle square of the top row in the origin grid and the tool adds transform-origin: center top.
How do you test captions before the real ones are written?
The studio hasn't named every piece yet. To see how the tiles behave with text under the photo, open the Lorem Ipsum Generator, set Unit to Words, Amount to 5, untick Start with โLorem ipsumโฆโ so each caption looks different, and click Generate. Do it a few times for short captions.
For the worst case, switch Unit to Sentences with Amount 1. The generator builds each sentence from 8 to 17 words, which is longer than most captions and shows whether a two-line caption makes one tile taller than its neighbours. If it does, align-items: start in the Grid Playground stops the shorter tiles from stretching. Copy as HTML wraps the text in <p> tags, ready to paste under each image. Swap in the real captions before the page goes live, because search engines treat placeholder text as low-quality content.
FAQ
What is the difference between auto-fill and auto-fit?
Both make as many columns as fit. auto-fill keeps empty columns when there are fewer items than slots, and auto-fit collapses them so the items stretch across the row. With 12 photos the gallery fills every slot either way, so you'd only see the difference on a page with two or three items.
Will resizing to 800 pixels make the photos blurry?
No. Shrinking keeps the image sharp, and 800 pixels is more than the largest tile needs at double density. Enlarging a photo beyond its original size is what makes it soft, because the browser has to invent the new pixels.
Does the hover zoom work on phones?
Touch screens have no real hover, so on a phone the zoom may show briefly after a tap or not at all. The gallery still works, because the zoom is decoration and the layout doesn't depend on it.
The other CSS generators are in Developer Tools, and more ways to prepare photos are in Image Tools.
