Bird
Raised Fist0
CSSmarkup~20 mins

Grid gap in CSS - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

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
Challenge - 5 Problems
🎖️
Grid Gap Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
What does the gap property do in CSS Grid?
In a CSS Grid layout, what is the main purpose of the gap property?
AIt defines the size of each grid item.
BIt sets the space between grid items both in rows and columns.
CIt controls the padding inside each grid item.
DIt changes the background color of the grid container.
Attempts:
2 left
💡 Hint
Think about the space you see between boxes in a grid layout.
📝 Syntax
intermediate
1:30remaining
Which CSS code correctly sets a 20px gap between grid items?
You want to add a 20px gap between all grid items in a container. Which CSS snippet does this correctly?
CSS
.container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  /* gap here */
}
Agap: 20px;
Bgrid-gap: 20px 20px 20px;
Cgap: 20px 20px;
Dgap: 20px 20px 20px 20px;
Attempts:
2 left
💡 Hint
The gap property accepts one or two values for rows and columns.
rendering
advanced
2:00remaining
What visual difference does gap: 10px 30px; create in a grid?
Given a grid container with gap: 10px 30px;, what will you see?
CSS
.container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px 30px;
}
A10px space around the entire grid container.
B30px vertical space between rows and 10px horizontal space between columns.
C10px vertical space between rows and 30px horizontal space between columns.
DNo space between grid items.
Attempts:
2 left
💡 Hint
Remember the order of values in gap: first is row gap, second is column gap.
selector
advanced
1:30remaining
Which CSS selector targets the gap between grid items?
You want to style the space between grid items using CSS. Which selector or property affects this space?
AThe <code>gap</code> property on the grid container.
BThe <code>:gap</code> pseudo-class on grid items.
CThe <code>margin</code> property on grid items.
DThe <code>padding</code> property on the grid container.
Attempts:
2 left
💡 Hint
Think about which CSS property controls spacing between grid cells.
accessibility
expert
2:30remaining
How does using gap improve accessibility in grid layouts?
Why is it better for accessibility to use the CSS gap property instead of adding margins to grid items?
ABecause margins increase the font size of grid items.
BBecause margins cause grid items to be skipped by screen readers.
CBecause <code>gap</code> automatically adds ARIA labels to grid items.
DBecause <code>gap</code> maintains consistent spacing without affecting keyboard navigation or screen reader order.
Attempts:
2 left
💡 Hint
Think about how spacing affects focus and reading order.

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

  1. Step 1: Understand the purpose of gap

    The gap property is designed to add space between grid items easily.
  2. Step 2: Compare with other options

    Options A, B, and D describe unrelated effects like size, color, or visibility, which gap does not control.
  3. Final Answer:

    It adds space between grid items without extra margins. -> Option D
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    gap: 10px 20px; -> Option A
  4. 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?
  .container {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 15px 30px;
  }
medium
A. 15px
B. 30px
C. 45px
D. No gap

Solution

  1. Step 1: Identify gap values meaning

    The first value (15px) is the row gap (vertical space), the second value (30px) is the column gap (horizontal space).
  2. Step 2: Determine horizontal gap

    Horizontal space between grid items is the column gap, which is 30px.
  3. Final Answer:

    30px -> Option B
  4. Quick Check:

    Horizontal gap = second gap value [OK]
Hint: Second gap value = horizontal space [OK]
Common Mistakes:
  • Mixing row and column gap values
  • Adding gap values instead of choosing correct one
  • Ignoring gap property effects
4. What is wrong with this CSS code if the grid items have no space between them?
  .grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px 20;
  }
medium
A. The second gap value is missing units.
B. The gap property is misspelled.
C. Grid columns are not defined correctly.
D. Display should be flex, not grid.

Solution

  1. Step 1: Check gap property values

    The gap property has two values: 10px and 20. The second value lacks units (like px), which is invalid.
  2. 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.
  3. Final Answer:

    The second gap value is missing units. -> Option A
  4. 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?
hard
A. .grid { display: flex; gap: 1rem 2rem; flex-wrap: wrap; }
B. .grid { display: grid; grid-template-columns: 4fr; grid-template-rows: 3fr; gap: 1rem 2rem; }
C. @media (min-width: 600px) { .grid { display: grid; grid-template-columns: repeat(4, 1fr); grid-template-rows: repeat(3, auto); gap: 1rem 2rem; } } .grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 0.5rem 1rem; }
D. .grid { display: grid; grid-template-columns: repeat(4, 1fr); grid-template-rows: repeat(3, 1fr); gap: 2rem 1rem; }

Solution

  1. Step 1: Check grid structure and gap values

    @media (min-width: 600px) { .grid { display: grid; grid-template-columns: repeat(4, 1fr); grid-template-rows: repeat(3, auto); gap: 1rem 2rem; } } .grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 0.5rem 1rem; } uses 4 columns and 3 rows with correct gap: 1rem row gap, 2rem column gap. It also includes a responsive media query for smaller screens.
  2. Step 2: Evaluate accessibility and responsiveness

    @media (min-width: 600px) { .grid { display: grid; grid-template-columns: repeat(4, 1fr); grid-template-rows: repeat(3, auto); gap: 1rem 2rem; } } .grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 0.5rem 1rem; } adapts layout for smaller screens, improving accessibility and usability. Other options have wrong gap order, wrong display type, or no responsiveness.
  3. Final Answer:

    Code with media query and correct gap values -> Option C
  4. Quick Check:

    Responsive grid with correct gap order [OK]
Hint: Use gap: row column and media queries for responsiveness [OK]
Common Mistakes:
  • Swapping row and column gap values
  • Using flex instead of grid for grid layouts
  • Ignoring responsive design needs