CSS

CSS Overview Selectors Declarations Rule Sets Comments Reset/Normalize Origin Importance Order Specificity Inheritance Cascade Layers Inline Styles Internal Styles External Styles @import Absolute Units Relative Units Dynamic Viewport Angle Units Time Units Frequency Units: Resolution Units Keywords Color Formats Color Functions & Spaces Background Color Background Image Background Repeat Background Position Background Size Background Attachment Background Shorthand Multiple Backgrounds Linear Gradients Radial Gradients Conic Gradients Font Family Font Size Font Weight Font Style Line Height Text Align Text Decoration Text Transform Letter Spacing Word Spacing Text Shadow Text Wrap Variable Fonts Content Box Padding Box Border Box Margin Box Box Sizing Property Margin Padding Border Outline Box Shadow Block Display Inline Display Inline Block Display Static Position Relative Position Absolute Position Fixed Position Sticky Position Top Element Position Right Element Position Bottom Element Position Left Element Position Z Index Flexbox Box Layout Display Flex Flex Direction Flex Wrap Justify Content Align Items Align Content Gap, Row Gap & Column Gap Flex Grow Flex Shrink Flex Basis Flex Shorthand Order Property Align Self Practical Applications CSS Grid Layout Grid Display Grid Template Columns Grid Template Rows Grid Template Areas Justify Items Grid Auto Columns Justify Self Nested Grids Responsive Web Design Min Width Max Width Orientation Logical Operators Pointer Hover Prefers Color Scheme Fluid Images Flexible Typography Viewport Width Advanced CSS Features Defining variables Using variables Scope & Inheritance of CSS Variables Property Value Fallback Practical Applications :Hover :Active :Focus :Focus Visible :Visited :Link :First Child :Last Child :Nth Child :Nth of Type :Empty :Checked :Disabled :Enabled :Valid :Invalid :Required :Has :Not :Is :Where ::Before Pseudo Element ::After Pseudo Element ::First Letter ::First Line ::Selection ::Marker CSS 2D Transformations CSS 3D Transformations Transform Origin Transform Style Backface Visibility Transition Property Transition Duration Transition Timing Function Transition Delay Transition Shorthand Ease Timing Function Linear Timing Function Ease In Timing Function Ease Out Timing Function Ease In Out Timing Function Cubic Bezier Function Animations (@keyframes) Defining @keyframes Animation Name CSS Animation Duration Animation Timing Function CSS Animation Delay CSS Animation Iteration Count Animations Direction CSS Animation Fill Mode CSS Animation Play State CSS Filter Blur CSS Filter Brightness CSS Filter Contrast() CSS Drop Shadow CSS Grayscale CSS Hue Rotate CSS Invert CSS Opacity CSS Saturate CSS Sepia Mix Blend Mode Background Blend Mode Object Fit & Object Position Scroll Snap Type Scroll Snap Align Scroll Padding Scroll Margin Scroll Triggered Animations JS Variables

Gap, Row Gap & Column Gap


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.