Grid columns help you organize content in neat vertical sections. This makes your webpage look clean and easy to read.
Defining grid columns in Tailwind
grid grid-cols-{number}Replace {number} with how many columns you want.
Tailwind uses grid-cols-1 for one column, grid-cols-2 for two, and so on.
<div class="grid grid-cols-2">...</div><div class="grid grid-cols-4">...</div><div class="grid grid-cols-1">...</div>This example shows a grid with 3 columns and 6 boxes. The boxes have different blue shades and some have white text for contrast. The gap-4 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>Grid Columns Example</title> <script src="https://cdn.tailwindcss.com"></script> </head> <body class="p-6"> <h1 class="text-xl font-bold mb-4">Grid with 3 Columns</h1> <div class="grid grid-cols-3 gap-4"> <div class="bg-blue-200 p-4 text-center">Box 1</div> <div class="bg-blue-400 p-4 text-center">Box 2</div> <div class="bg-blue-600 p-4 text-center">Box 3</div> <div class="bg-blue-800 p-4 text-center text-white">Box 4</div> <div class="bg-blue-900 p-4 text-center text-white">Box 5</div> <div class="bg-blue-700 p-4 text-center text-white">Box 6</div> </div> </body> </html>
Use gap-{size} to add space between grid items for better look.
Grid column widths automatically adjust to screen size, but the number of columns is fixed unless you add responsive classes like sm:grid-cols-2.
Use semantic HTML and add aria-label if grid content needs accessibility help.
Use grid-cols-{number} to set how many columns your grid has.
Adding gaps between columns makes content easier to see.
Grid layouts help organize content neatly and responsively.
Practice
grid-cols-3 do in a grid container?Solution
Step 1: Understand the class naming
The classgrid-cols-3means setting the grid to have 3 columns.Step 2: Differentiate columns from rows and gaps
Rows are set bygrid-rows-{number}, gaps bygap-{size}. So this class only sets columns.Final Answer:
It creates a grid with 3 columns. -> Option CQuick Check:
grid-cols-3 means 3 columns [OK]
- Confusing columns with rows
- Thinking it sets gaps between columns
- Assuming it limits number of items
Solution
Step 1: Recall Tailwind grid column syntax
The correct syntax to define columns isgrid-cols-{number}.Step 2: Check each option
Onlygrid-cols-4matches the correct pattern. Others are invalid or set rows.Final Answer:
grid-cols-4 -> Option AQuick Check:
grid-cols-4 sets 4 columns [OK]
- Using grid-rows instead of grid-cols
- Mixing order of words in class
- Using non-existent class names
<div class="grid grid-cols-2 gap-4"> <div>Box 1</div> <div>Box 2</div> <div>Box 3</div> </div>
Solution
Step 1: Analyze grid columns and gap
The classgrid-cols-2creates 2 columns.gap-4adds space between grid items.Step 2: Place boxes in grid
Boxes fill columns left to right, top to bottom. Box 1 and Box 2 fill first row, Box 3 goes to second row alone.Final Answer:
Two columns with Box 1 and Box 2 in first row, Box 3 alone in second row. -> Option DQuick Check:
grid-cols-2 means 2 columns, items fill rows [OK]
- Assuming all items fit in one row
- Ignoring gap spacing
- Confusing rows and columns
<div class="grid grid-cols-">Content</div>
Solution
Step 1: Check the class syntax
The classgrid-cols-is incomplete; it needs a number to define how many columns.Step 2: Confirm Tailwind requirements
Tailwind requires a number aftergrid-cols-likegrid-cols-3. Without it, the class is invalid.Final Answer:
Missing number after grid-cols- to specify columns. -> Option AQuick Check:
grid-cols- must have a number [OK]
- Leaving grid-cols- incomplete
- Using wrong class names
- Thinking grid-cols- alone works
Solution
Step 1: Understand responsive prefixes
Tailwind uses prefixes likelg:to apply styles on large screens and above.Step 2: Apply correct classes for responsiveness
Usegrid-cols-1for default (small screens) andlg:grid-cols-4for large screens.Step 3: Check options
Only grid grid-cols-1 lg:grid-cols-4 uses correct syntax and order for responsive grid columns.Final Answer:
grid grid-cols-1 lg:grid-cols-4 -> Option BQuick Check:
Default 1 col, large screens 4 cols [OK]
- Reversing screen size prefixes
- Incorrect class order or syntax
- Using invalid class names
Ready to prove it?
Pass the test and this pattern moves unknown
