Master CSS From Scratch

Clear, interactive, and structured CSS lessons designed for beginners.

CSS Gap

The CSS gap property is used to create space between Flex Items inside a Flex Container.

Instead of adding margins individually to every item, the gap property provides a simple way to control the spacing between Flex Items.

What is Gap?

The gap property defines the amount of space between Flex Items.

It creates space between the items without adding extra space around the outside edge of the Flex Container.

Gap is especially useful when creating navigation menus, card layouts, buttons, forms, and responsive components.

Basic Syntax

.container {
    display: flex;
    gap: 20px;
}

In this example, a 20px gap is created between the Flex Items.

Gap Diagram

Item 1
Item 2
Item 3

20px space is created between each Flex Item.

Example: gap: 20px

The following example creates a 20px space between the three Flex Items.

.container {
    display: flex;
    gap: 20px;
}

Output

HTML
CSS
JavaScript

Different Gap Values

gap: 5px

Creates a small 5px space between Flex Items.

gap: 20px

Creates a 20px space between Flex Items.

gap: 40px

Creates a larger 40px space between Flex Items.

Row Gap and Column Gap

When Flexbox contains multiple rows or columns, you can control horizontal and vertical spacing separately.

CSS provides two properties: row-gap and column-gap.

1. row-gap

The row-gap property controls the space between Flex Lines.

.container {
    display: flex;
    flex-wrap: wrap;
    row-gap: 20px;
}

2. column-gap

The column-gap property controls the space between items along the column direction.

.container {
    display: flex;
    flex-wrap: wrap;
    column-gap: 30px;
}

Using row-gap and column-gap Together

You can use both properties together when you need different horizontal and vertical spacing.

.container {
    display: flex;
    flex-wrap: wrap;
    row-gap: 20px;
    column-gap: 30px;
}

row-gap: 20px; creates 20px of space between Flex Lines.

column-gap: 30px; creates 30px of space between items horizontally.

Gap Shorthand

The gap property can also accept two values.

.container {
    display: flex;
    gap: 20px 30px;
}

In the two-value syntax:

20px = row gap

30px = column gap

Gap Syntax Comparison

CSS Meaning
gap: 20px; 20px gap between rows and columns.
gap: 20px 30px; 20px row gap and 30px column gap.
row-gap: 20px; Controls spacing between rows.
column-gap: 30px; Controls spacing between columns.

Gap vs Margin

Gap Margin
Controls space between Flex Items. Controls space around individual elements.
Applied to the Flex Container. Applied to individual elements.
Easier for consistent item spacing. Useful when individual element spacing is required.

Real-World Example

Suppose CIIT Training Institute displays multiple technology course cards.

Instead of adding margin to every course card, the parent container can use the gap property.

.courses {
    display: flex;
    flex-wrap: wrap;
    gap: 20px;
}

.course {
    background-color: lightblue;
    border: 2px solid blue;
    padding: 20px;
}

The parent .courses container controls the spacing between all course cards.

This keeps the spacing consistent and makes the layout easier to maintain.

Important: The gap property creates space between Flex Items. It does not create the same kind of outer spacing around the Flex Container that margin does.

Summary

The gap property creates consistent spacing between Flex Items. row-gap controls spacing between Flex Lines, while column-gap controls spacing between columns. The shorthand gap: 20px 30px; can be used to define different row and column gaps.