A Flickr-style justified photo gallery built with Tailwind CSS: photos of any orientation are packed into rows that all share the same height and fill the full width, without cropping away the shape of each image. The layout itself needs no JavaScript; a small script only powers the optional row-height slider. It suits portfolios, photo journals, travel blogs and product shots.
How it works
The root is section.jgl-gallery with a header (title, a range input .jgl-range and an output element .jgl-value) and the .jgl-grid container. Each photo is a figure.jgl-item holding an img and a figcaption.jgl-caption.
The trick is in style.css. .jgl-grid is a wrapping flex container. Every item gets its aspect ratio as the custom property --jgl-r, set with a Tailwind arbitrary-property class such as [--jgl-r:1.5] or [--jgl-r:0.667]. The item uses flex: var(--jgl-r) 1 calc(var(--jgl-r) * var(--jgl-row)) and aspect-ratio: var(--jgl-r): its starting width equals the target row height times its ratio, and the leftover space in each row is shared in proportion to the ratio. Because width and growth are both proportional to the ratio, every photo in a row grows to the same height. The .jgl-grid::after pseudo-element is a filler with a huge flex-grow, so the last row keeps its natural height instead of stretching.
--jgl-row is 100px on phones and 200px from the sm breakpoint. On DOMContentLoaded the script syncs the slider to the current value and, on every input event, writes the new --jgl-row onto the gallery, which re-justifies all rows instantly. Titles stay visible; subtitles appear on hover and are always shown on touch screens via @media (hover: none).
Customization
[--jgl-r:…]class on each.jgl-item: set it to the photo's width divided by its height (for example1.778for 16:9) so the image is shown uncropped.--jgl-rowon.jgl-galleryinstyle.css: change the target row height for phones and for wider screens.--jgl-gapon.jgl-gallery: widen or tighten the space between photos.minandmaxon#jgl-height: change the slider range, or delete the whole slider block andscript.jsfor a fixed layout.rounded-lgon.jgl-item: userounded-nonefor a seamless mosaic orrounded-2xlfor softer tiles.group-hover:scale-105on the images: remove it to disable the hover zoom.bg-stone-950andtext-amber-400: swap forbg-whiteand a dark text color to build a light version.
Use cases
- Photographer portfolios: photos of any orientation fill each row at the same height without cropping
- Travel blogs: captions show titles with subtitles on hover and always on touch screens
- Photo journals: a row-height slider lets visitors resize the whole gallery live
- Product shot pages: rows re-justify automatically so every photo fits neatly at any screen width
Accessibility
Every photo has a descriptive alt text, explicit width and height to prevent layout shift, decoding="async" and loading="lazy" after the first four images. Captions are real figcaption elements, so screen readers announce them even when the subtitle is visually hidden. The slider has a visible label, an output linked with aria-describedby, and an aria-valuetext in pixels. The hover zoom and caption animation are turned off under prefers-reduced-motion: reduce.
Usage notes
The photos come from Unsplash; replace them with your own images and keep width, height and --jgl-r in sync. Several galleries can live on one page because the script initializes every .jgl-gallery separately.