JSFiddle - React, Tailwind, and code Playground

by Alphonsio

HTML

<div class="container">
  <div class="item">1</div>
  <div class="item">2</div>
  <div class="item">3</div>
  <div class="item">4</div>
  <div class="item">5</div>
</div>

CSS

* { background-color: #eee; }

.container {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  grid-auto-rows: 10px; /* Controls vertical spacing */
  gap: 10px;
}

.item {
  background: lightblue;
  border-radius: 8px;
  padding: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Dynamic height for masonry effect */
.item:nth-child(1) { grid-row: span 10; }
.item:nth-child(2) { grid-row: span 15; }
.item:nth-child(3) { grid-row: span 12; }
.item:nth-child(4) { grid-row: span 18; }
.item:nth-child(5) { grid-row: span 8; }