CSS: Masonry design

PHOTO EMBED

Wed Feb 25 2026 11:55:35 GMT+0000 (Coordinated Universal Time)

Saved by @marcopinero #css #html

.grid {
  columns: 18rem;
  gap: 1rem;
  counter-reset: grid;
}

.item + .item {
  margin-top: 1rem;
}

.item {
  break-inside: avoid;
  aspect-ratio: 4 / 3;
  background: pink;
  padding: 1rem;
  border-radius: 0.75rem;
}

.item::before {
  counter-increment: grid;
  content: counter(grid);
}

.item:nth-child(3n) {
  aspect-ratio: 1;
  background: lavender;
}

.item:nth-child(3n - 1) {
  aspect-ratio: 2 / 3;
  background: lightblue;
}
content_copyCOPY