Skip to content

Justified Photo Gallery Rows

Flickr-style Tailwind CSS gallery where photos of any ratio fill each row at equal heights, with a live row-height slider.

Also known as: justified photo grid, flickr style gallery, equal height photo rows, photo gallery mixed aspect ratios, portfolio image gallery

Tailwind CSS Galleries Responsive Dark #flexbox #gallery #images #photos #responsive

Justified Photo Gallery Rows live demo

Open in new tab

Tailwind CSS code (HTML, CSS, JS)

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 example 1.778 for 16:9) so the image is shown uncropped.
  • --jgl-row on .jgl-gallery in style.css: change the target row height for phones and for wider screens.
  • --jgl-gap on .jgl-gallery: widen or tighten the space between photos.
  • min and max on #jgl-height: change the slider range, or delete the whole slider block and script.js for a fixed layout.
  • rounded-lg on .jgl-item: use rounded-none for a seamless mosaic or rounded-2xl for softer tiles.
  • group-hover:scale-105 on the images: remove it to disable the hover zoom.
  • bg-stone-950 and text-amber-400: swap for bg-white and 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.

Justified Photo Gallery Rows at a glance

Framework
Tailwind CSS
Code
HTML, CSS, JavaScript
JavaScript
Vanilla JavaScript, no jQuery
Dependencies
Tailwind CSS
Layout
Responsive, works on mobile, dark design
Category
Galleries
License
Free for personal and commercial use

Questions about this template

Can I use the Justified Photo Gallery Rows code for free?

Yes. You can copy or download the code and use it in personal and commercial projects, including client work. No sign-up is needed. Reselling it as part of a component library or template collection is not allowed; see the license.

How do I add it to my website?

Load Tailwind CSS from a CDN (the links are under Requirements). Paste the HTML where the component should appear, add the CSS to your stylesheet and load the JavaScript before the closing </body> tag. Download HTML gives you a complete page with everything already linked.

Does it need jQuery or a JavaScript framework?

No. The script is plain vanilla JavaScript, so it works without jQuery, React or Vue. You can still use the markup inside those frameworks.

Does it work on mobile?

Yes. The layout adapts to small screens. Use the Mobile and Tablet buttons above the live demo to check it at phone and tablet width.

Requirements

Add these to your page (already included in the download):

  • <script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>

The Tailwind browser build is for prototyping. In production, compile Tailwind with its CLI or your bundler.

Justified Photo Gallery Rows – free Tailwind CSS gallery template
Justified Photo Gallery Rows · free Tailwind CSS gallery template

More Galleries

CSS Grid Bento Photo Gallery – free Pure CSS gallery template

CSS Grid Bento Photo Gallery

Bento-style photo gallery with mixed tile sizes from CSS Grid spans, hover captions and a keyboard-friendly dialog lightbox.

Polaroid Photo Gallery – free Pure CSS gallery template

Polaroid Photo Gallery

Pure CSS polaroid gallery on a cork board: tilted photo frames with tape and handwritten captions that straighten and lift on hover or focus.

Masonry Image Gallery – free Pure CSS gallery template

Masonry Image Gallery

Pure CSS masonry image gallery using multi-column layout, with hover and focus captions that slide up over each photo. No JavaScript needed.

Before and After Image Slider for Shopify – free Shopify gallery template

Before and After Image Slider for Shopify

Drag-to-compare before and after image slider for Shopify with vertical or horizontal divider, touch and keyboard control, and optional side text.

Filterable Portfolio Gallery – free Bootstrap 5 gallery template

Filterable Portfolio Gallery

Dark Bootstrap 5 portfolio gallery with pill filter buttons, automatic category counts, fade-in tiles and a screen reader status message.