Heads up, this content is in beta. Use at your own risk!
In the realm of web design, grid layouts are a staple for creating well-structured and responsive layouts. Tailwind CSS offers powerful utilities to build grid systems that are flexible and easy to adjust. Let’s dive into creating a basic responsive grid layout.
Creating a Basic Grid
Here’s a simple grid with three equal-width columns:
<div class="grid grid-cols-3 gap-4">
<div class="p-4 text-center text-white bg-blue-500">1</div>
<div class="p-4 text-center text-white bg-blue-500">2</div>
<div class="p-4 text-center text-white bg-blue-500">3</div>
</div>
In this example, we create a grid container using the grid
class, then specify that we want three columns using grid-cols-3
. We also add gap-4
to create some space between the grid items. Each grid item has a blue background (bg-blue-500
) and padding (p-4
).
Creating a Responsive Grid
Now, let’s make our grid responsive. We want one column on small screens, two columns on medium screens, and four columns on large screens:
<div class="grid grid-cols-1 gap-4 md:grid-cols-2 lg:grid-cols-4">
<div class="p-4 text-center text-white bg-blue-500">1</div>
<div class="p-4 text-center text-white bg-blue-500">2</div>
<div class="p-4 text-center text-white bg-blue-500">3</div>
<div class="p-4 text-center text-white bg-blue-500">4</div>
</div>
Tailwind uses a mobile-first breakpoint system, meaning grid-cols-1
applies to all screen sizes by default, then md:grid-cols-2
applies from the md
breakpoint and up, and lg:grid-cols-4
applies from the lg
breakpoint and up.