Grid gap helps you add space between items in a grid layout. It makes your design look neat and easy to read.
Grid gap in CSS
Start learning this pattern below
Jump into concepts and practice - no test required
grid-gap: <row-gap> <column-gap>; /* or use the newer syntax */ gap: <row-gap> <column-gap>;
grid-gap is the older name; gap is the modern, preferred property.
You can set one value for equal row and column gaps, or two values for different spacing.
gap: 1rem;
gap: 1rem 2rem;
grid-gap: 10px 20px;
This example creates a 3-column grid with 1.5rem space between rows and 2rem space between columns. Each box is blue with white text and rounded corners. The grid container has a light gray background and some padding around it.
<!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> <style> .grid-container { display: grid; grid-template-columns: repeat(3, 1fr); gap: 1.5rem 2rem; background-color: #f0f0f0; padding: 1rem; } .grid-item { background-color: #4a90e2; color: white; padding: 1rem; text-align: center; border-radius: 0.5rem; font-weight: bold; font-size: 1.2rem; } </style> </head> <body> <main> <section class="grid-container" aria-label="Example grid with gaps"> <div class="grid-item">1</div> <div class="grid-item">2</div> <div class="grid-item">3</div> <div class="grid-item">4</div> <div class="grid-item">5</div> <div class="grid-item">6</div> </section> </main> </body> </html>
Use gap instead of grid-gap for better browser support and future-proof code.
Gap works only if the container has display: grid or display: flex.
Gap values can use any CSS length units like rem, em, px, or percentages.
Grid gap adds space between grid items easily.
Use gap with one or two values for row and column spacing.
It keeps your layout clean without extra margins on items.
Practice
gap do in a grid layout?Solution
Step 1: Understand the purpose of
Thegapgapproperty 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, whichgapdoes not control.Final Answer:
It adds space between grid items without extra margins. -> Option DQuick Check:
Grid gap = space between items [OK]
- Confusing gap with padding or margin
- Thinking gap changes item size
- Assuming gap affects background color
Solution
Step 1: Understand gap syntax with two values
The first value sets the row gap, the second sets the column gap. Sogap: 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 AQuick Check:
gap: row column [OK]
- Swapping row and column values
- Using commas between values
- Using only one value when two are needed
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 15px 30px;
}Solution
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).Step 2: Determine horizontal gap
Horizontal space between grid items is the column gap, which is 30px.Final Answer:
30px -> Option BQuick Check:
Horizontal gap = second gap value [OK]
- Mixing row and column gap values
- Adding gap values instead of choosing correct one
- Ignoring gap property effects
.grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 10px 20;
}Solution
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.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 AQuick Check:
All length values need units except zero [OK]
- Forgetting units on numeric values
- Assuming gap works without units
- Confusing gap with margin or padding
Solution
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.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.Final Answer:
Code with media query and correct gap values -> Option CQuick Check:
Responsive grid with correct gap order [OK]
- Swapping row and column gap values
- Using flex instead of grid for grid layouts
- Ignoring responsive design needs
