Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Create a Responsive Grid with Gaps Using CSS Grid
📖 Scenario: You are designing a 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 simple grid layout using CSS Grid that shows 6 photos arranged in 3 columns and 2 rows with a gap of 1.5rem between each photo.
📋 What You'll Learn
Use a container with CSS Grid display
Set the grid to have exactly 3 columns
Add a gap of 1.5rem between grid items
Include 6 grid items inside the container
Use semantic HTML and accessible attributes
💡 Why This Matters
🌍 Real World
Grid layouts with gaps are common in photo galleries, product listings, and dashboards to keep content organized and visually pleasing.
💼 Career
Understanding CSS Grid and the gap property is essential for front-end developers to build modern, responsive web layouts efficiently.
Progress0 / 4 steps
1
Create the HTML structure with a grid container and 6 items
Write HTML code to create a <section> with class gallery. Inside it, add exactly 6 <div> elements each with class photo. Do not add any CSS yet.
CSS
Hint
Use a <section> with class gallery and inside it add 6 <div> elements with class photo.
2
Add CSS to make the container a grid with 3 columns
Write CSS to select the class .gallery and set display: grid; and grid-template-columns: repeat(3, 1fr); to create 3 equal columns.
CSS
Hint
Use display: grid; and grid-template-columns: repeat(3, 1fr); inside the .gallery CSS rule.
3
Add a gap of 1.5rem between grid items
Add a CSS property gap: 1.5rem; inside the .gallery CSS rule to create space between the grid items.
CSS
Hint
Use the gap property with value 1.5rem inside the .gallery CSS rule.
4
Style the photos with background color and padding
Add CSS for the class .photo to give each photo a light gray background color #ddd, padding of 1rem, and center the text with text-align: center;.
CSS
Hint
Use background-color: #ddd;, padding: 1rem;, and text-align: center; inside the .photo CSS rule.
Practice
(1/5)
1. What does the CSS property gap do in a grid layout?
easy
A. It sets the size of grid items.
B. It changes the background color of grid items.
C. It hides grid items from view.
D. It adds space between grid items without extra margins.
Solution
Step 1: Understand the purpose of gap
The gap property is designed to add space between grid items easily.
Step 2: Compare with other options
Options A, B, and D describe unrelated effects like size, color, or visibility, which gap does not control.
Final Answer:
It adds space between grid items without extra margins. -> Option D
Quick Check:
Grid gap = space between items [OK]
Hint: Remember: gap = space between grid cells [OK]
Common Mistakes:
Confusing gap with padding or margin
Thinking gap changes item size
Assuming gap affects background color
2. Which of the following is the correct syntax to set a 10px gap between rows and 20px gap between columns in a CSS grid?
easy
A. gap: 10px 20px;
B. gap: 20px 10px;
C. gap: 10px;
D. gap: 10px, 20px;
Solution
Step 1: Understand gap syntax with two values
The first value sets the row gap, the second sets the column gap. So gap: 10px 20px; means 10px row gap and 20px column gap.
Step 2: Check other options for syntax errors or wrong order
gap: 20px 10px; reverses the order (20px row, 10px column). gap: 10px; sets only one value (both gaps equal). gap: 10px, 20px; uses a comma which is invalid syntax.
Final Answer:
gap: 10px 20px; -> Option A
Quick Check:
gap: row column [OK]
Hint: First gap is row, second is column, no commas [OK]
Common Mistakes:
Swapping row and column values
Using commas between values
Using only one value when two are needed
3. Given this CSS code, what is the horizontal space between grid items?
The gap property has two values: 10px and 20. The second value lacks units (like px), which is invalid.
Step 2: Understand CSS unit requirements
CSS requires units for length values except zero. Missing units cause the property to be ignored, so no gap appears.
Final Answer:
The second gap value is missing units. -> Option A
Quick Check:
All length values need units except zero [OK]
Hint: Always add units (px, em) to gap values except zero [OK]
Common Mistakes:
Forgetting units on numeric values
Assuming gap works without units
Confusing gap with margin or padding
5. You want a grid with 4 columns and 3 rows. You want 1rem gap between rows and 2rem gap between columns. Which CSS code correctly sets this with accessibility and responsive design in mind?