Griddy
A CSS grid whose items of different heights pack into columns with no holes: photo galleries, pin boards, cards of notes and reviews. Columns come from the grid utilities you already know, the order stays the reading order, and it packs again as pictures load and screens change. New items can arrive with an animation, and items glide to their new place when the layout changes.
<div class="griddy grid-cols-2 md:grid-cols-3 gap-3" data-moey-griddy>
<a class="gallery-tile" href="lisbon.jpg"><img src="lisbon-small.jpg" alt="Lisbon at dawn" width="800" height="1000"></a>
<a class="gallery-tile" href="lake.jpg"><img src="lake-small.jpg" alt="A lake up north" width="800" height="560"></a>
…
</div>
How it works
- A grid with columns:
.griddyplus the column utilities,grid-cols-2 md:grid-cols-3. Without them it makes as many columns of about 14rem as fit (--mo-griddy-min). - Any items: pictures, cards, notes. Give pictures their
widthandheightso the space is kept before they load. - Moey packs them: each item goes to the column that is shortest so far, at its exact height, so the columns fill with no holes. It packs again when a picture loads, fonts arrive, the window changes, items come and go, or a class changes the columns, the gap or an item's width. It measures each item's real size, so zoom or an entrance animation never makes items overlap.
- Reading order stays: items stay in the page's order, roughly left to right, so the keyboard and screen readers go through them sensibly. In browsers with built-in CSS masonry, Moey just uses that.
Cards, not just pictures
2 × Linen tote, 1 × Wool beanie
Call the courier before 4 pm.
“The linen tote is perfect. It fits my laptop, a book and lunch. Ordering another one in blue.”
- Pack 12 orders
- Photograph new bags
- Update the FAQ
A small leather tag with the customer's initials?
<div class="griddy gap-4" data-moey-griddy>
<div class="card">…</div>
<div class="card">…</div>
</div>
New items arrive
Add data-moey-enter and items added later (infinite scroll, "Load more", a post added by any script) play an entrance, one after another. Pick the effect with its value. Items that only move (sorting, dragging, filters) don't play it, and nothing moves for visitors who turned motion off. Try them:
Call the courier before 4 pm.
A small leather tag with the customer's initials?
<div class="griddy gap-3" data-moey-griddy data-moey-enter="pop">…</div>
<!-- effects: (empty) fade up · fade · rise · drop · zoom · pop · slide · flip · blur · none -->
<!-- data-moey-enter-first: the items already there when the page loads arrive too -->
<!-- your own: [data-moey-enter] > .is-entering { animation: … } (--mo-enter-index: 0, 1, 2… for the stagger) -->
<script>
Moey.enter(list); // play it again on every item
Moey.enter(list, [a, b]); // or on the ones you give
</script>
It works on any list or grid, not only Griddy: a feed, a list group, a row of cards. The items wait --mo-enter-stagger (60ms, 55ms for pop) one after another.
Gliding
Add .griddy-glide and the items already on the page slide to their new place when the layout changes: an item gets wider, the column count or the gap changes, the window is resized, a filter hides some. An item placed for the first time goes straight to its spot, so new items never fly in from a corner. Try it:
Call the courier before 4 pm.
“Arrived in two days and the stitching is lovely. Already ordered a second one for my sister.”
40 linen totes.
- Pack 12 orders
- Photograph new bags
- Update the FAQ
A small leather tag with the customer's initials?
Photos for the winter collection on Monday. Bring the gray backdrop.
1 × Wool beanie, gift wrapped.
<div class="griddy griddy-glide grid-cols-2 md:grid-cols-3 gap-3" data-moey-griddy>…</div>
<script>
item.classList.add('span-2'); // the others glide out of the way
grid.classList.replace('md:grid-cols-3', 'md:grid-cols-4'); // a new column count: everything glides
</script>
Wide items, holes filled, drag to arrange
span-2 makes an item two columns wide; .griddy-dense lets later items move up into the holes this leaves (like a packing puzzle). Add data-moey-sortable and people can drag the tiles into their own order. Try it:
<div class="griddy griddy-dense grid-cols-2 md:grid-cols-4 gap-3" data-moey-griddy data-moey-sortable>
<figure class="gallery-tile span-2" data-id="lisbon">…</figure>
<figure class="gallery-tile" data-id="lake">…</figure>
…
</div>
With .griddy-dense the order on screen can differ from the page's. That's fine for pictures, but keep it off when the order matters (news, steps).
Reference
| Class / attribute | What it does |
|---|---|
.griddy[data-moey-griddy] | Packs its items into columns with no holes |
grid-cols-* · md:grid-cols-* · gap-* | Columns and spacing (else automatic: --mo-griddy-min, 14rem) |
span-2 · span-3 | An item across two or three columns |
.griddy-dense | Later items fill holes (order on screen may change) |
.griddy-glide | Items already there glide to their new place when the layout changes (.4s) |
data-moey-enter="…" | Items added later arrive, one after another: (empty) fade up · fade · rise · drop · zoom · pop · slide · flip · blur · none. Any list or grid. While they do, they get .is-entering and --mo-enter-index |
data-moey-enter-first | The items already there when the page loads arrive too |
--mo-enter-stagger | The wait between items (60ms; 55ms for pop) |
.gallery-tile + .gallery-caption / figcaption | A rounded picture tile with a caption on hover (always shown on touch) |
Moey.griddy(el).layout() | Pack again (after changing items from code; usually automatic) |
Moey.enter(list, items?) | Play the arrival again, on all items or the ones given |
.masonry · .masonry-dense · data-moey-masonry · Moey.masonry() | Griddy's earlier names: they still work |