CHAPTER 76
CSS Gap
Learn how gap creates consistent spacing between Flexbox and Grid items.
01. What is CSS Gap?
gap creates space between rows and columns.
It can be used with Flexbox and CSS Grid.
row-gap and column-gap provide separate control.
02. Syntax
.container {
display: grid;
gap: 20px;
}
03. Example
CSS / HTML Example
AIFA Coding Lab
<!DOCTYPE html>
<html>
<head>
<style>
.grid {
display: grid;
grid-template-columns:
repeat(3, 1fr);
gap: 20px;
}
.card {
padding: 25px;
background: #dbeafe;
}
</style>
</head>
<body>
<div class="grid">
<div class="card">One</div>
<div class="card">Two</div>
<div class="card">Three</div>
</div>
</body>
</html>
04. Important Points
✓
gap controls spacing between items.
✓
row-gap controls vertical spacing.
✓
column-gap controls horizontal spacing.
✓
gap is cleaner than adding margins to every child.
05. Common Mistakes
⚠
Using inconsistent margins when gap would be simpler.
⚠
Forgetting that gap affects space between items, not outer edges.
⚠
Creating excessive gaps on mobile.
06. Real World Use
Gap is widely used in card grids, navigation bars, Flexbox layouts and responsive interfaces.
Ready to Practice? 🚀
Open the AIFA CSS Editor and practice this chapter live.
Try It Yourself →
Practice Exercise
Practice Grid Gap
Create a three-column grid with 20px horizontal and vertical spacing.