Introduction
CSS Grid helps arrange items in rows and columns easily. It makes complex page layouts simple and neat without extra code.
CSS Grid helps arrange items in rows and columns easily. It makes complex page layouts simple and neat without extra code.
grid grid-cols-3 grid-rows-2 gap-4
grid to turn on CSS Grid layout.grid-cols-3 to create 3 columns and grid-rows-2 for 2 rows.gap-4 to add space between grid items.<div class="grid grid-cols-2 gap-2"> <div>Item 1</div> <div>Item 2</div> <div>Item 3</div> <div>Item 4</div> </div>
<div class="grid grid-cols-3 grid-rows-1 gap-3">
<div>One</div>
<div>Two</div>
<div>Three</div>
</div>col-span-2 to make an item span across 2 columns.<div class="grid grid-cols-4 gap-4"> <div class="col-span-2">Wide item</div> <div>Small 1</div> <div>Small 2</div> </div>
This example shows a grid with 3 columns and 2 rows. Each colored box fills one cell. The gap adds space between boxes.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>CSS Grid Example</title> <script src="https://cdn.tailwindcss.com"></script> </head> <body class="p-6"> <h1 class="text-xl font-bold mb-4">Simple CSS Grid Layout</h1> <div class="grid grid-cols-3 grid-rows-2 gap-4 border p-4"> <div class="bg-blue-200 p-4">Box 1</div> <div class="bg-green-200 p-4">Box 2</div> <div class="bg-red-200 p-4">Box 3</div> <div class="bg-yellow-200 p-4">Box 4</div> <div class="bg-purple-200 p-4">Box 5</div> <div class="bg-pink-200 p-4">Box 6</div> </div> </body> </html>
CSS Grid works well with Tailwind's utility classes for quick layout.
Grid makes it easy to control spacing and alignment without extra wrappers.
Try resizing the browser to see how grid adapts with responsive classes.
CSS Grid arranges items in rows and columns easily.
Tailwind classes like grid-cols-3 and gap-4 help build layouts fast.
Grid solves complex layouts without complicated code.
grid-cols-3 sets the grid to have 3 columns.grid-rows-3 sets rows, grid-gap-3 sets spacing, grid-flow-col changes flow direction.<div class="grid grid-cols-2 gap-4"> <div>A</div> <div>B</div> <div>C</div> </div>
grid-cols-2 creates 2 columns, and gap-4 adds space between items.<div class="grid grid-cols-3 gap-2"> <div class="col-span-4">Item</div> </div>
grid-cols-3.col-span-4 tries to span 4 columns, which is more than available 3 columns, causing layout error.lg: prefix applies styles on large screens and above.grid-cols-2 sets 2 columns by default (small screens), lg:grid-cols-4 changes to 4 columns on large screens.Pass the test and this pattern moves unknown