October 13, 2023
O. Wolfson
Code at github: https://github.com/owolfdev/grid-tailwind-next
Deployed project: https://grid-tailwind-next.vercel.app/
Note that we are using react so we should use the className
attribute. If you are using plain HTML, just replace className
with class
.
First, you need to define an element as a grid container using the grid
utility:
html<div className="grid">
<!-- Your grid items here -->
</div>
Use grid-cols-*
and grid-rows-*
to define the number of columns and rows:
html<!-- 3 columns and 2 rows -->
<div className="grid grid-cols-3 grid-rows-2">
<!-- Your grid items here -->
</div>
If you want an item to span multiple columns or rows, use col-span-*
and row-span-*
:
html<div className="grid grid-cols-3 grid-rows-6 h-screen bg-gray-100">
<div className="col-span-3 bg-green-200">
header - I take up three columns
</div>
<div className="bg-teal-200 row-span-4">content - I take up one column</div>
<div className="bg-orange-200 row-span-4">content - I take up one column</div>
<div className="bg-yellow-200 row-span-4">content - I take up one column</div>
<div className="col-span-3 bg-pink-200 row-span-1">
footer - I take up three column
</div>
</div>
Deployed example: https://grid-tailwind-next.vercel.app/spanning-multiple-rows
Use gap-*
to set consistent gaps between rows and columns:
html<div className="grid grid-cols-3 gap-4">
<div className="bg-pink-400">1</div>
<div className="bg-pink-400">2</div>
<div className="bg-pink-400">3</div>
<div className="bg-pink-400">4</div>
<div className="bg-pink-400">5</div>
</div>
Deployed example: https://grid-tailwind-next.vercel.app/gap
For setting gap only horizontally or vertically:
html<div class="grid grid-cols-3 col-gap-4 row-gap-2">
<!-- Your grid items here -->
</div>
You can combine the grid utilities with Tailwind’s responsive modifiers:
html<div class="grid grid-cols-1 md:grid-cols-3">
<!-- Your grid items here -->
</div>
This gives a single column on small screens and 3 columns on medium (md:
) screens and up.
If you want more advanced grid layouts, you can use grid template areas. Though this may require a mix of both Tailwind and some inline styles:
html<div
class="grid grid-areas-[header header header; sidebar content content; footer footer footer]"
>
<div class="grid-area-header">Header</div>
<div class="grid-area-sidebar">Sidebar</div>
<div class="grid-area-content">Content</div>
<div class="grid-area-footer">Footer</div>
</div>
(Note: the syntax above is a simplified representation, and you might need to adjust it for more complex scenarios.)
Align and justify items using the following utilities:
html<!-- Align items to the center -->
<div class="grid items-center">
<!-- Your grid items here -->
</div>
<!-- Justify items to the end -->
<div class="grid justify-end">
<!-- Your grid items here -->
</div>
Remember, the above is a basic guide. CSS Grid (even in Tailwind) is very powerful, and there's much more you can do with it. Always refer to the official Tailwind documentation for the most updated and comprehensive information.