The gap
property is modern shorthand for row-gap
and column-gap
. It provides a simple and powerful way to define the spacing, or "gutters," between flex items, eliminating the need for margin-based hacks.
Example 1: Uniform Gap
HTML
<div class="flex-container uniform-gap">
<div class="item">1</div> <div class="item">2</div> <div class="item">3</div>
<div class="item">4</div> <div class="item">5</div>
</div>
CSS
.flex-container {
display: flex;
flex-wrap: wrap;
border: 2px solid #000;
}
.uniform-gap {
gap: 20px; /* Sets a 20px gap for both rows and columns. */
}
Explanation
Using gap: 20px
applies a 20-pixel space between rows and between columns. It's a clean and concise way to add consistent spacing.
Example 2: Different Row and Column Gaps
HTML
<div class="flex-container different-gaps">
<div class="item">1</div> <div class="item">2</div> <div class="item">3</div>
<div class="item">4</div> <div class="item">5</div>
</div>
CSS
.flex-container {
display: flex;
flex-wrap: wrap;
border: 2px solid #000;
}
.different-gaps {
gap: 30px 10px; /* 30px row-gap, 10px column-gap. */
}
Explanation
When two values are provided to gap
, the first value sets the row-gap
(space between rows) and the second value sets the column-gap
(space between columns).
Example 3: Only Column Gap
HTML
<div class="flex-container column-gap-only">
<div class="item">1</div> <div class="item">2</div> <div class="item">3</div>
</div>
CSS
.flex-container {
display: flex;
border: 2px solid #000;
}
.column-gap-only {
column-gap: 15px; /* Applies a 15px gap only between columns. */
}
Explanation
The column-gap
property specifically targets the spacing between columns of flex items, leaving no space between rows in a wrapped container.
Example 4: Only Row Gap
HTML
<div class="flex-container row-gap-only">
<div class="item">1</div> <div class="item">2</div> <div class="item">3</div>
<div class="item">4</div> <div class="item">5</div>
</div>
CSS
.flex-container {
display: flex;
flex-wrap: wrap;
border: 2px solid #000;
}
.row-gap-only {
row-gap: 25px; /* Applies a 25px gap only between rows. */
}
Explanation
The row-gap
property specifically targets the spacing between the rows of a multi-line flex container.
Example 5: Gap in a Column Layout
HTML
<div class="flex-container column-with-gap">
<div class="item">1</div> <div class="item">2</div> <div class="item">3</div>
</div>
CSS
.flex-container {
display: flex;
flex-direction: column; /* Main axis is vertical. */
border: 2px solid #4CAF50;
}
.column-with-gap {
gap: 10px; /* Creates a 10px space between the stacked items. */
}
Explanation
The gap
property works intuitively in a column layout as well. It adds a vertical space between the items, which is much cleaner than adding margin-bottom
to every item except the last one.
Example 6: Responsive Gap
HTML
<div class="flex-container responsive-gap">
<div class="item">1</div> <div class="item">2</div> <div class="item">3</div>
<div class="item">4</div>
</div>
CSS
.flex-container {
display: flex;
flex-wrap: wrap;
border: 2px solid #d9534f;
}
.responsive-gap {
gap: 10px; /* Small gap for mobile. */
}
/* Increase the gap on larger screens. */
@media (min-width: 768px) {
.responsive-gap {
gap: 25px; /* Larger gap for tablets and desktops. */
}
}
Explanation
You can easily change the gap
property inside media queries to create responsive spacing that adapts to the screen size, providing more breathing room on larger viewports.
Example 7: Gap vs. space-between
HTML
<div class="flex-container with-gap">
<div class="item">1</div> <div class="item">2</div> <div class="item">3</div>
</div>
<div class="flex-container with-space-between">
<div class="item">1</div> <div class="item">2</div> <div class="item">3</div>
</div>
CSS
.with-gap {
justify-content: flex-start; /* Items start at the edge. */
gap: 20px; /* Gap only adds space BETWEEN items. */
}
.with-space-between {
justify-content: space-between; /* space-between pushes items to the edges. */
}
Explanation
This example illustrates a key advantage of gap
. Unlike justify-content: space-between
, gap
does not add space at the edges of the container, only between the items themselves, giving you more precise control over spacing.