Open In App

CSS grid-gap Property

The grid-gap property sets the size of the gap between the rows and columns in a grid layout allowing you to easily control the spacing between grid items in both horizontal and vertical directions. 

It is a shorthand property for the following properties: 

Syntax: 

grid-gap: grid-row-gap grid-column-gap;

Property Values:

Different Examples of CSS Grid Gap Property

Example 1:  In this example we are using the CSS grid gap property to give gap between the row and columns.

<!DOCTYPE html>
<html>
<head>
    <title>
        CSS | grid-gap Property
    </title>
    <style>
        body {
            text-align: center;
        }

        h1 {
            color: black;
        }

        .grid-container {
            display: grid;
            grid-template-columns: auto auto auto;
            grid-column-gap: 50px;
            grid-row-gap: 10px;
            background-color: blue;
            padding: 10px;
        }

        .grid-container>div {
            background-color: white;
            text-align: center;
            padding: 20px 0;
            font-size: 30px;
        }
    </style>
</head>

<body>

    <h1>GeeksforGeeks</h1>
    <h2>Grid-gap property</h2>
    <p>This grid has a 50px gap between
        columns and 10px gap between rows. :
    </p>
    <div class="grid-container">
        <div class="item1">G</div>
        <div class="item2">E</div>
        <div class="item3">E</div>
        <div class="item4">K</div>
        <div class="item5">S</div>
    </div>
</body>
</html>

Output: 

Example 2:  In this example we are using the css grid gap property to give gap between the row and columns.

<!DOCTYPE html>
<html>
<head>
    <title>
        CSS | grid-gap Property
    </title>
    <style>
        body {
            text-align: center;
        }

        h1 {
            color: green;
        }

        .grid-container {
            display: grid;
            grid-template-columns: auto auto auto;
            grid-column-gap: 8%;
            grid-row-gap: 5%;
            background-color: black;
            padding: 6%;
        }

        .grid-container>div {
            background-color: yellow;
            text-align: center;
            padding: 20px 0;
            font-size: 30px;
        }
    </style>
</head>

<body>

    <h1>GeeksforGeeks</h1>
    <h2>Grid-gap property</h2>
    <p>This grid has a 8% gap between columns
        and 5% gap between rows:
    </p>
    <div class="grid-container">
        <div class="item1">G</div>
        <div class="item2">E</div>
        <div class="item3">E</div>
        <div class="item4">K</div>
        <div class="item5">S</div>
    </div>
</body>
</html>

Output: 

Supported Browsers: The browsers supported by grid-gap property are listed below: 

Article Tags :