Adding space between grid cells makes content easier to see and nicer to look at. It helps separate items clearly.
Gap between grid cells in Tailwind
gap-{size}
// Example: gap-4Use gap-{size} to add equal space between rows and columns.
You can also use gap-x-{size} for horizontal gaps and gap-y-{size} for vertical gaps.
<div class="grid grid-cols-3 gap-4">
<!-- grid items -->
</div><div class="grid grid-cols-2 gap-x-6 gap-y-2">
<!-- grid items -->
</div><div class="grid grid-cols-4 gap-8">
<!-- grid items -->
</div>This example creates a 3-column grid with six blue boxes. The gap-4 class adds space between each box horizontally and vertically.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Grid Gap Example</title> <script src="https://cdn.tailwindcss.com"></script> </head> <body class="p-6"> <h1 class="text-xl font-bold mb-4">Grid with Gap Between Cells</h1> <div class="grid grid-cols-3 gap-4"> <div class="bg-blue-500 text-white p-4 rounded">Cell 1</div> <div class="bg-blue-500 text-white p-4 rounded">Cell 2</div> <div class="bg-blue-500 text-white p-4 rounded">Cell 3</div> <div class="bg-blue-500 text-white p-4 rounded">Cell 4</div> <div class="bg-blue-500 text-white p-4 rounded">Cell 5</div> <div class="bg-blue-500 text-white p-4 rounded">Cell 6</div> </div> </body> </html>
The gap size uses Tailwind's spacing scale, like 1, 2, 4, 6, 8, etc.
Gaps work well with grid and flex layouts for spacing items evenly.
Using gap is better than adding margin to individual items because it keeps spacing consistent.
Use gap-{size} to add space between grid cells easily.
You can control horizontal and vertical gaps separately with gap-x-{size} and gap-y-{size}.
Adding gaps improves the look and readability of grid layouts.
Practice
gap-4 do in a grid layout?Solution
Step 1: Understand the
Thegap-4classgap-4class in Tailwind adds spacing between grid cells both horizontally and vertically.Step 2: Compare with other gap classes
Classes likegap-x-4orgap-y-4add space only horizontally or vertically, butgap-4applies to both directions equally.Final Answer:
Adds equal space between all grid cells horizontally and vertically -> Option DQuick Check:
gap-4 = equal horizontal and vertical gap [OK]
- Confusing gap-4 with gap-x-4 or gap-y-4
- Thinking gap-4 removes space
- Assuming gap-4 affects only rows or columns
Solution
Step 1: Identify horizontal and vertical gap classes
In Tailwind,gap-x-{size}sets horizontal gap andgap-y-{size}sets vertical gap.Step 2: Match sizes to directions
We want horizontal gap 6 and vertical gap 2, sogap-x-6 gap-y-2is correct.Final Answer:
gap-x-6 gap-y-2 -> Option AQuick Check:
gap-x-6 gap-y-2 = horizontal 6, vertical 2 [OK]
- Using gap-6 gap-2 which overrides each other
- Swapping gap-x and gap-y sizes
- Using only gap without direction for different sizes
<div class="grid grid-cols-3 gap-3"> <div>A</div> <div>B</div> <div>C</div> </div>
What visual effect will
gap-3 have on the grid cells?Solution
Step 1: Understand the grid setup
The grid has 3 columns and usesgap-3which adds spacing between all cells horizontally and vertically.Step 2: Visualize the effect of gap-3
Each cell will have moderate space around it, improving readability and separation.Final Answer:
Adds moderate space between all grid cells horizontally and vertically -> Option BQuick Check:
gap-3 = moderate equal gaps [OK]
- Thinking gap-3 adds no space
- Confusing gap-3 with gap-x-3 or gap-y-3
- Assuming gap-3 affects only rows or columns
gap-4 gap-y-8. What is wrong?Solution
Step 1: Understand the class application order
Thegap-4class sets both horizontal and vertical gaps to 1rem. The followinggap-y-8class overrides only the vertical gap to 2rem, leaving the horizontal gap at 1rem.Step 2: Verify against desired outcome
This results in horizontal gap of 4 and vertical gap of 8, exactly as intended.Final Answer:
Both gaps work correctly as intended -> Option AQuick Check:
gap-4 gap-y-8 = x:4 y:8 [OK]
- Thinking gap-4 overrides the subsequent gap-y-8
- Assuming the gaps add together
- Believing this causes a syntax error
Solution
Step 1: Convert rem values to Tailwind spacing scale
Tailwind spacing scale: 1rem = 4, 0.5rem = 2. So horizontal gap 1rem = gap-x-4, vertical gap 0.5rem = gap-y-2.Step 2: Choose correct classes for horizontal and vertical gaps
Usegap-x-4for horizontal andgap-y-2for vertical gaps to set different sizes.Final Answer:
gap-x-4 gap-y-2 -> Option CQuick Check:
1rem horizontal = gap-x-4, 0.5rem vertical = gap-y-2 [OK]
- Using gap-4 gap-2 which overrides gaps
- Swapping horizontal and vertical gap sizes
- Using invalid fractional gap classes
Ready to prove it?
Pass the test and this pattern moves unknown
