Photo grid

Photos as tiles in a named list: equal tiles, masonry columns or justified rows, "+N" past a maximum, and an optional lightbox. Presentation only; the grid itself needs no script.

Anatomy

 ┌──────┐┌──────┐┌──────┐
 │      ││      ││      │   ← ul[role=list][aria-label] in an @container
 └──────┘└──────┘└──────┘
 ┌──────┐┌──────┐┌──────┐
 │      ││      ││  +4  │   ← li > a[href] > img; "+N" over the last
 └──────┘└──────┘└──────┘
PartTokenWhy
Tilesurface-subtle under the image, rounded-controlThe ground shows while the thumbnail loads, at the image's own size.
Gapgap-1 / gap-2 / gap-4 (sm / md / lg)Photos read as a set at a small gap; lg for a sparse gallery.
"+N"bg-surface-sunken/70, text-fg, text-title, under darkA dark scrim with light text over any photo, from the dark tokens rather than a hard-coded white.
Hoverscale-[1.03] on the image, motion-safe, the dialog-enter durationSays the tile opens something, inside its own clip.
Focusoutline-focus-ring, offset 2, on the linkThe clip is the link's (overflow-hidden), not the tile's, so the ring outside it is never cut off.

Layouts

  • grid: CSS grid, tiles of ratio (square, 4:3, 3:2, 16:9, 3:4), object-cover.
  • masonry: CSS columns, break-inside-avoid, each photo at its own shape. Order runs down the columns: right for a gallery, wrong where order matters, and the reason it is not the default.
  • justified: a flex row per line; each tile's flex is its aspect ratio grow, from a basis of ratio × --ds-row (7 / 9 / 12 rem as the container widens), so a row's tiles end one height. An ::after with a huge grow soaks up the last row. Needs width and height; without them a tile is taken as square.

Columns step up with the grid's own width (container queries, @md @2xl @4xl @6xl), from literal-class maps for 1–6, so a grid in a sidebar stays narrow on a wide screen.

Rows

src (required), alt (required unless decorative), thumb, width, height, caption, href, srcset, sizes, decorative. Arrays or objects. width and height go on the tile's img so the page does not shift; loading="lazy" and decoding="async" always.

Missing alt

Under app.debug a photo with no alt that is not decorative throws InvalidArgumentException naming its index; otherwise Log::warning and the tile's link is named "Photo n of m" (or by its caption). A decorative photo has alt="" and its link is named by its caption or position, so a link is never nameless.

max and "+N"

The tiles past max are rendered hidden when there is a lightbox (each is an opener, tabindex=-1, so the lightbox can show it) and not at all without one. The last shown tile carries "+N", aria-hidden, and its link's name gains ", and N more". With moreHref that tile links there.

lightbox: true renders a ds::lightbox named ds-photos-<hash>, with thumbnails when there is more than one photo, and marks every tile x-ds-lightbox with that name, data-ds-lightbox-src (when href is not the image) and data-ds-lightbox-caption. A string wires the tiles to a lightbox of that name the host renders. The root gets x-data="" so the openers are inside an Alpine scope. Without the script the tiles are links.

Accessibility

  • Role and name: ul[role=list] named by label; each tile a link named by its img's alt (or the fallback name above).
  • Keyboard: one Tab stop per shown tile; Enter follows or opens.
  • Focus: the ring outside the tile; hidden tiles are out of the order.
  • Motion: the hover scale is motion-safe.

Do not

  • Clip on the li. The focus ring is drawn outside the link and a clip on the tile cut it off; the link is the clip.
  • Use hidden tiles without a lightbox. Without one they are not rendered at all, so they never cost a download.
  • Take alt from the file name. "IMG_2041.jpg" is worse than the fallback.

From the package repo: edit this page on GitHub.

Was this page helpful?