22 CSS Image Galleries02 / 22

Pure CSSMIT licensed

CSS Aspect-Ratio Justified Gallery

The Google-Images / Flickr look: fluid rows where every photo keeps its native aspect ratio (no cropping) yet each row stretches flush to both edges. Achieved with flex-grow weighting and aspect-ratio — no JS measuring.

Published

Live Demo
Try it

The code

<section class="gl-05" aria-label="Justified aspect-ratio gallery">
  <div class="gl-05__gallery">
    <div class="gl-05__row">
      <figure class="gl-05__item" style="--r:1.5"><img src="https://loremflickr.com/600/400/beach?lock=501" alt="Wide sandy beach" loading="lazy" decoding="async"></figure>
      <figure class="gl-05__item" style="--r:0.75"><img src="https://loremflickr.com/450/600/lighthouse?lock=502" alt="Coastal lighthouse" loading="lazy" decoding="async"></figure>
      <figure class="gl-05__item" style="--r:1.33"><img src="https://loremflickr.com/600/450/pier?lock=503" alt="Wooden pier over water" loading="lazy" decoding="async"></figure>
    </div>
    <div class="gl-05__row">
      <figure class="gl-05__item" style="--r:1.78"><img src="https://loremflickr.com/640/360/valley?lock=504" alt="Green valley panorama" loading="lazy" decoding="async"></figure>
      <figure class="gl-05__item" style="--r:1"><img src="https://loremflickr.com/500/500/flower?lock=505" alt="Close-up of a wildflower" loading="lazy" decoding="async"></figure>
      <figure class="gl-05__item" style="--r:1.4"><img src="https://loremflickr.com/560/400/river?lock=506" alt="River through a forest" loading="lazy" decoding="async"></figure>
    </div>
    <div class="gl-05__row">
      <figure class="gl-05__item" style="--r:0.8"><img src="https://loremflickr.com/480/600/waterfall,tall?lock=507" alt="Tall narrow waterfall" loading="lazy" decoding="async"></figure>
      <figure class="gl-05__item" style="--r:1.6"><img src="https://loremflickr.com/640/400/dunes?lock=508" alt="Sunlit sand dunes" loading="lazy" decoding="async"></figure>
      <figure class="gl-05__item" style="--r:1.2"><img src="https://loremflickr.com/540/450/meadow?lock=509" alt="Alpine meadow in bloom" loading="lazy" decoding="async"></figure>
    </div>
  </div>
</section>
.gl-05,
.gl-05 *,
.gl-05 *::before,
.gl-05 *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

.gl-05 ::selection {
  background: oklch(0.6 0.15 160);
  color: #fff;
}

.gl-05 {
  font-family: 'Segoe UI',system-ui,sans-serif;
  width: 100%;
  min-height: 100vh;
  padding: 40px 20px;
  background: #fafafa;
  display: flex;
  align-items: center;
  justify-content: center;
}

.gl-05__gallery {
  width: min(920px,100%);
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.gl-05__row {
  display: flex;
  gap: 8px;
}

.gl-05__item {
  flex: var(--r) 1 0;
  aspect-ratio: var(--r);
  border-radius: 8px;
  overflow: hidden;
  background: #e6e8ea;
}

.gl-05__item img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
  transform: scale(1);
  transition: transform .4s;
}

.gl-05__item:hover img {
  transform: scale(1.05);
}

@media (max-width: 600px) {
  .gl-05__row {
    flex-wrap: wrap;
  }

  .gl-05__item {
    flex: 1 1 40%;
  }
}

@media (prefers-reduced-motion: reduce) {
  .gl-05__item img {
    transition: none;
  }
}
/* No JavaScript — flex-grow weighted by aspect-ratio justifies each row with no cropping. */
Paste this into ChatGPT, Claude, Cursor, or any coding assistant. The block below is pre-framed with everything the AI needs to integrate this demo into your project — markup, styles, scoping notes, and the source URL. Hit Copy and paste straight into your chat.
Here's a working CSS Image Gallerie from CodeFronts. Use it as-is or adapt to your framework. All classes are scoped under a unique prefix so the code won't collide with your existing styles. MIT licensed.
Demo: CSS Aspect-Ratio Justified Gallery
Source: https://codefronts.com/snippets/css-image-gallery/css-aspect-ratio-justified-gallery/

The Google-Images / Flickr look: fluid rows where every photo keeps its native aspect ratio (no cropping) yet each row stretches flush to both edges. Achieved with flex-grow weighting and aspect-ratio — no JS measuring.
## HTML
```html
<section class="gl-05" aria-label="Justified aspect-ratio gallery">
  <div class="gl-05__gallery">
    <div class="gl-05__row">
      <figure class="gl-05__item" style="--r:1.5"><img src="https://loremflickr.com/600/400/beach?lock=501" alt="Wide sandy beach" loading="lazy" decoding="async"></figure>
      <figure class="gl-05__item" style="--r:0.75"><img src="https://loremflickr.com/450/600/lighthouse?lock=502" alt="Coastal lighthouse" loading="lazy" decoding="async"></figure>
      <figure class="gl-05__item" style="--r:1.33"><img src="https://loremflickr.com/600/450/pier?lock=503" alt="Wooden pier over water" loading="lazy" decoding="async"></figure>
    </div>
    <div class="gl-05__row">
      <figure class="gl-05__item" style="--r:1.78"><img src="https://loremflickr.com/640/360/valley?lock=504" alt="Green valley panorama" loading="lazy" decoding="async"></figure>
      <figure class="gl-05__item" style="--r:1"><img src="https://loremflickr.com/500/500/flower?lock=505" alt="Close-up of a wildflower" loading="lazy" decoding="async"></figure>
      <figure class="gl-05__item" style="--r:1.4"><img src="https://loremflickr.com/560/400/river?lock=506" alt="River through a forest" loading="lazy" decoding="async"></figure>
    </div>
    <div class="gl-05__row">
      <figure class="gl-05__item" style="--r:0.8"><img src="https://loremflickr.com/480/600/waterfall,tall?lock=507" alt="Tall narrow waterfall" loading="lazy" decoding="async"></figure>
      <figure class="gl-05__item" style="--r:1.6"><img src="https://loremflickr.com/640/400/dunes?lock=508" alt="Sunlit sand dunes" loading="lazy" decoding="async"></figure>
      <figure class="gl-05__item" style="--r:1.2"><img src="https://loremflickr.com/540/450/meadow?lock=509" alt="Alpine meadow in bloom" loading="lazy" decoding="async"></figure>
    </div>
  </div>
</section>
```
## CSS
```css
.gl-05,
.gl-05 *,
.gl-05 *::before,
.gl-05 *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

.gl-05 ::selection {
  background: oklch(0.6 0.15 160);
  color: #fff;
}

.gl-05 {
  font-family: 'Segoe UI',system-ui,sans-serif;
  width: 100%;
  min-height: 100vh;
  padding: 40px 20px;
  background: #fafafa;
  display: flex;
  align-items: center;
  justify-content: center;
}

.gl-05__gallery {
  width: min(920px,100%);
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.gl-05__row {
  display: flex;
  gap: 8px;
}

.gl-05__item {
  flex: var(--r) 1 0;
  aspect-ratio: var(--r);
  border-radius: 8px;
  overflow: hidden;
  background: #e6e8ea;
}

.gl-05__item img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
  transform: scale(1);
  transition: transform .4s;
}

.gl-05__item:hover img {
  transform: scale(1.05);
}

@media (max-width: 600px) {
  .gl-05__row {
    flex-wrap: wrap;
  }

  .gl-05__item {
    flex: 1 1 40%;
  }
}

@media (prefers-reduced-motion: reduce) {
  .gl-05__item img {
    transition: none;
  }
}
```

## JavaScript
```js
/* No JavaScript — flex-grow weighted by aspect-ratio justifies each row with no cropping. */
```

How this works

Each row is a flflex container. Every item is given flex: var(--r) 1 0 where --r is the image's aspect ratio, so free space is distributed in proportion to width, and the item's own aspect-ratio: var(--r) then sets a matching height. Because all items in a row share the same computed height, the row justifies edge-to-edge with zero cropping — the pure-CSS equivalent of a JS justified-gallery pass.

Images use object-fit: cover only as a safety net; with correct --r values they're shown uncropped at their true proportions.

Make it yours

  • Add photos by dropping in an item with its true --r (width ÷ height).
  • Control row height indirectly by how many items you place per row.
  • Change the gap for tighter or airier justification.
  • For an unknown/mixed feed, generate the --r values server-side from image dimensions.

Gotchas — read before shipping

  • The --r value must match the real image ratio or the photo will letterbox or crop — feed accurate ratios.
  • Pure-CSS justification needs you to group items into rows; a single wrapping flex row can't auto-balance the last line like a JS library.
  • Keep flex-basis: 0 so growth is purely ratio-weighted; a non-zero basis skews the justification.

Browser support

ChromeSafariFirefoxEdge
111+15.4+113+111+

Floor set by oklch() as this demo is written. The core technique itself goes back further — Chrome 88+.

aspect-ratio on flex items: Chrome 88+, Safari 15+, Firefox 89+. Older engines fall back to intrinsic image sizes.

Techniques used in this demo

Search CodeFronts

Loading…