I solved it like this using TailwindCSS @layer and @variants directives.
The following code provides a 3 column grid layout on LG breakpoint that turns to 2 columns on MD breakpoint and just 1 column on mobile.
Add this your SCSS file:
@layer utilities {
@variants responsive {
.masonry-3-col {
column-count: 3;
column-gap: 1em;
}
.masonry-2-col {
column-count: 2;
column-gap: 1em;
}
.break-inside {
break-inside: avoid;
}
}
}
And the HTML:
<div class="md:masonry-2-col lg:masonry-3-col box-border mx-auto before:box-inherit after:box-inherit">
<div class="break-inside p-8 my-6 bg-gray-100 rounded-lg">
<p>Really long content</p>
</div>
<div class="break-inside p-8 my-6 bg-gray-100 rounded-lg">
<p>Really long content</p>
</div>
<div class="break-inside p-8 my-6 bg-gray-100 rounded-lg">
<p>Really long content</p>
</div>
<div class="break-inside p-8 my-6 bg-gray-100 rounded-lg">
<p>Really long content</p>
</div>
<div class="break-inside p-8 my-6 bg-gray-100 rounded-lg">
<p>Really long content</p>
</div>
</div>
My solution is an evolution of the answer that I have found in this nice article that was not providing the switch 1-2-3 columns on page resize.
https://blog.marclucraft.co.uk/masonry-layout-with-tailwindcss
Update With Tailwind v3
<div class="relative flex min-h-screen flex-col justify-center py-6 sm:py-12">
<div
class="columns-2 2xl:columns-3 gap-10 [column-fill:_balance] box-border mx-auto before:box-inherit after:box-inherit">
<div class="break-inside-avoid p-8 mb-6 bg-gray-100 rounded-lg">
<p>Really long content</p>
</div>
<div class="break-inside-avoid p-8 mb-6 bg-gray-100 rounded-lg">
<p>Really long content</p>
<p>Really long content</p>
<p>Really long content</p>
<p>Really long content</p>
</div>
<div class="break-inside-avoid p-8 mb-6 bg-gray-100 rounded-lg">
<p>Really long content</p>
<p>Really long content</p>
</div>
<div class="break-inside-avoid p-8 mb-6 bg-gray-100 rounded-lg">
<p>Really long content</p>
<p>Really long content</p>
<p>Really long content</p>
<p>Really long content</p>
<p>Really long content</p>
</div>
<div class="break-inside-avoid p-8 mb-6 bg-gray-100 rounded-lg">
<p>Really long content</p>
<p>Really long content</p>
<p>Really long content</p>
</div>
</div>
</div>