# Justified Gallery Rows

> A photo gallery where every row fills the width and photos keep their shape, like Flickr or Google Photos.

- Category: [CSS Layouts](https://copy-css.com/category/layouts)
- Tags: masonry, photo gallery, flex-grow, aspect-ratio, flickr
- Live preview: https://copy-css.com/snippets/justified-gallery
- License: Free for personal and commercial use. No signup, no attribution required.

## HTML

```html
<div class="justified">
  <div style="--r: 1.5"></div>
  <div style="--r: 0.75"></div>
  <div style="--r: 1"></div>
  <div style="--r: 1.33"></div>
  <div style="--r: 1.78"></div>
  <div style="--r: 0.8"></div>
  <div style="--r: 1.2"></div>
</div>
```

## CSS

```css
/* --r is each photo's width / height ratio */
.justified {
  --row-height: 180px;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.justified > * {
  flex: calc(var(--r) * 100) 1 calc(var(--r) * var(--row-height));
  aspect-ratio: var(--r);
}

.justified img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

/* Keeps the last row from stretching */
.justified::after {
  content: "";
  flex-grow: 1000000;
}
```

## Notes

- This CSS Justified Gallery Rows is 25 lines of pure CSS (0.4 KB) with no JavaScript and no dependencies. It uses Flexbox, aspect-ratio and custom properties.
- Customize with: `--row-height`.
- In JSX use `className` instead of `class`. In Tailwind projects put the CSS in the global stylesheet.
