The Wayback Machine - https://web.archive.org/web/20250108192153/https://www.geeksforgeeks.org/css-gap-property/
Open In App

CSS gap Property

Last Updated : 01 Oct, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The CSS gap property defines the spacing (or “gap”) between rows and columns in layouts such as Flexbox, Grid, or multi-column layouts. It simplifies the process of creating consistent spacing by combining row-gap and column-gap into one property. You can specify the gap using length units (e.g., px, em) or percentages.

Syntax

gap: <row-gap> <column-gap>

Property values

Property ValueDescription
<length>Specifies the spacing using length units like em, px, etc. For example: gap: 10px 20px.
<percentage>Specifies the spacing using percentage units. For example: gap: 50%.

CSS gap Property Examples

Here are examples of the CSS gap property to illustrate its use in layouts:

Example 1: Using Length Units

In this example we are using CSS Grid with specified row and column gaps (gap: 20px 50px) for layout spacing between grid items (col divs).

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" 
          content="width=device-width, 
                   initial-scale=1.0">
    <style>
        .row {
            display: grid;
            width: 500px;
            gap: 20px 50px;
        }

        .col {
            background-color: green;
            border: 1px solid black;
            color: #fff;
            width: fit-content;
            height: 20px;
            font-size: 20px;
        }
    </style>
</head>

<body>
    <div class="row">
        <div class="col">
            geeksforgeeks
        </div>
        <div class="col">
            geeksforgeeks
        </div>
        <div class="col">
            geeksforgeeks
        </div>
    </div>
    <div class="row">
        <div class="col">
            geeksforgeeks
        </div>
        <div class="col">
            geeksforgeeks
        </div>
        <div class="col">
            geeksforgeeks
        </div>
    </div>
</body>

</html>

Output:

Example 2: Using Percentage Units

In this example we are using CSS Grid with a row gap specified as a percentage (gap: 20%) for layout spacing between grid items (col divs) within each row (row divs).

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" 
          content="width=device-width, 
                   initial-scale=1.0">
    <style>
        .row {
            display: grid;
            width: fit-content;
            gap: 20%;
            height: 100px;
            background-color: greenyellow;
        }

        .col {
            background-color: green;
            border: 1px solid black;
            color: #fff;
            width: fit-content;
            font-size: 20px;
        }
    </style>
</head>

<body>

    <p>In terms of percentage</p>

    <div class="row">
        <div class="col">
            geeksforgeeks
        </div>
        <div class="col">
            geeksforgeeks
        </div>
        <div class="col">
            geeksforgeeks
        </div>
    </div>
    <br><br><br>
    <div class="row">
        <div class="col">
            geeksforgeeks
        </div>
        <div class="col">
            geeksforgeeks
        </div>
        <div class="col">
            geeksforgeeks
        </div>
    </div>
</body>

</html>

Output:

Supported Browsers

Note: Older browser versions may have limited support, so it’s always recommended to test for compatibility across different browsers.

CSS gap Property – FAQs

What is the CSS gap property?

The gap property in CSS defines the spacing between rows and columns in Flexbox, Grid, and multi-column layouts, helping to create clean, structured layouts.

Why use gap instead of margin?

Using gap is more efficient in Flexbox and Grid layouts because it simplifies managing spacing between items without affecting the outer elements, unlike margin.

How to give a gap between two boxes in CSS?

You can apply the gap property in Grid or Flexbox layouts to create space between items, instead of adding margins to each box individually.

How to add a gap between columns in CSS?

In CSS Grid, the gap or column-gap property can be used to define the space between columns without altering the spacing between rows.

How do you put space between items in CSS?

In Flexbox or Grid layouts, the gap property easily adds space between items, improving layout structure without needing extra margins or padding.



Next Article

Similar Reads

three90RightbarBannerImg