📖 Scenario: You are building a simple photo gallery webpage. You want to arrange photos in a neat grid with space between each photo so they don't touch each other.
🎯 Goal: Build a grid layout using Tailwind CSS that displays four colored boxes arranged in two columns and two rows with a visible gap between the grid cells.
📋 What You'll Learn
Use a div container with Tailwind CSS grid classes
Create exactly four child div elements inside the grid container
Apply a gap between grid cells using Tailwind CSS gap utilities
Each grid cell should have a distinct background color and fixed height
The grid should have two columns
💡 Why This Matters
🌍 Real World
Grid layouts with gaps are common in photo galleries, product listings, and dashboards to keep content organized and visually clear.
💼 Career
Knowing how to use Tailwind CSS grid and gap utilities helps front-end developers build clean, responsive layouts quickly and accessibly.
Progress0 / 4 steps
1
Set up the HTML grid container and four grid items
Create a div with the class grid and inside it add four child div elements. Each child div should have a fixed height of h-24 and background colors bg-red-400, bg-green-400, bg-blue-400, and bg-yellow-400 respectively.
Tailwind
Hint
Use the grid class on the container and add four child div elements with the specified height and background color classes.
2
Add two columns to the grid
Add the Tailwind CSS class grid-cols-2 to the grid container div to create two columns.
Tailwind
Hint
Add the grid-cols-2 class to the existing grid class on the container.
3
Add a gap between the grid cells
Add the Tailwind CSS class gap-4 to the grid container div to create a gap between the grid cells.
Tailwind
Hint
Add the gap-4 class alongside the existing classes on the grid container.
4
Add accessibility and responsive design
Add the role="list" attribute to the grid container div for accessibility. Also, replace the grid-cols-2 class with grid-cols-1 sm:grid-cols-2 to make the grid have one column on small screens and two columns on larger screens.
Tailwind
Hint
Add the role="list" attribute to the container and replace grid-cols-2 with grid-cols-1 sm:grid-cols-2 in the container's class list.
Practice
(1/5)
1. What does the Tailwind class gap-4 do in a grid layout?
easy
A. Removes all space between grid cells
B. Adds space only between rows in the grid
C. Adds space only between columns in the grid
D. Adds equal space between all grid cells horizontally and vertically
Solution
Step 1: Understand the gap-4 class
The gap-4 class in Tailwind adds spacing between grid cells both horizontally and vertically.
Step 2: Compare with other gap classes
Classes like gap-x-4 or gap-y-4 add space only horizontally or vertically, but gap-4 applies to both directions equally.
Final Answer:
Adds equal space between all grid cells horizontally and vertically -> Option D
Quick Check:
gap-4 = equal horizontal and vertical gap [OK]
Hint: gap-{size} sets equal space in all directions [OK]
Common Mistakes:
Confusing gap-4 with gap-x-4 or gap-y-4
Thinking gap-4 removes space
Assuming gap-4 affects only rows or columns
2. Which Tailwind class correctly adds horizontal gap of 6 and vertical gap of 2 between grid cells?
easy
A. gap-x-6 gap-y-2
B. gap-6 gap-2
C. gap-x-6 gap-2
D. gap-y-6 gap-x-2
Solution
Step 1: Identify horizontal and vertical gap classes
In Tailwind, gap-x-{size} sets horizontal gap and gap-y-{size} sets vertical gap.
Step 2: Match sizes to directions
We want horizontal gap 6 and vertical gap 2, so gap-x-6 gap-y-2 is correct.
Final Answer:
gap-x-6 gap-y-2 -> Option A
Quick Check:
gap-x-6 gap-y-2 = horizontal 6, vertical 2 [OK]
Hint: Use gap-x for horizontal, gap-y for vertical gaps [OK]
Common Mistakes:
Using gap-6 gap-2 which overrides each other
Swapping gap-x and gap-y sizes
Using only gap without direction for different sizes
What visual effect will gap-3 have on the grid cells?
medium
A. No space between grid cells
B. Adds moderate space between all grid cells horizontally and vertically
C. Adds space only between rows
D. Adds space only between columns
Solution
Step 1: Understand the grid setup
The grid has 3 columns and uses gap-3 which 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 B
Quick Check:
gap-3 = moderate equal gaps [OK]
Hint: gap-3 means moderate space all around grid cells [OK]
Common Mistakes:
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
4. You want to add horizontal gap 4 and vertical gap 8 between grid cells, but your code uses gap-4 gap-y-8. What is wrong?
medium
A. Both gaps work correctly as intended
B. gap-4 overrides gap-y-8, so vertical gap stays 4
C. gap-y-8 overrides gap-4, so horizontal gap stays 8
D. This syntax causes a syntax error
Solution
Step 1: Understand the class application order
The gap-4 class sets both horizontal and vertical gaps to 1rem. The following gap-y-8 class 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 A
Quick Check:
gap-4 gap-y-8 = x:4 y:8 [OK]
Hint: gap-{size} followed by gap-x/y-{size2} correctly sets directional gaps [OK]
Common Mistakes:
Thinking gap-4 overrides the subsequent gap-y-8
Assuming the gaps add together
Believing this causes a syntax error
5. You have a grid with 4 columns and want to add 1rem horizontal gap and 0.5rem vertical gap between cells. Which Tailwind classes achieve this correctly?
hard
A. gap-x-2 gap-y-4
B. gap-4 gap-2
C. gap-x-4 gap-y-2
D. gap-1 gap-y-0.5
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
Use gap-x-4 for horizontal and gap-y-2 for vertical gaps to set different sizes.