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

CSS grid-gap Property

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

The grid-gap property sets the size of the gap between 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: 

  • grid-column-gap property
  • grid-row-gap property

Syntax: 

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

Property Values:

  • grid-row-gap: Sets the size of the gap between the rows in a grid layout. The default value is 0
  • grid-column-gap: Sets the size of the gap between the columns in a grid layout. The default value is 0.

Different Examples of CSS Grid Gap Property

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

html
<!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.

html
<!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: 

The CSS grid-gap property is useful for creating visually appealing and well-spaced grid layouts. By understanding and utilizing the grid-gap, grid-row-gap, and grid-column-gap properties, developers can achieve precise control over the spacing between grid items, enhancing the overall design and usability of their web pages. Ensure cross-browser compatibility and consider the supported browser versions when implementing this property to maintain a consistent user experience across different platforms.

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

  • Google Chrome 57.0 and above
  • Edge 16.0 and above
  • Mozilla Firefox 52.0 and above
  • Safari 10.1 and above
  • Opera 44.0 and above

CSS grid-gap Property – FAQs

What does the grid-gap property do in CSS?

The grid-gap property sets the space between both rows and columns in a grid container. It’s a shorthand property that simplifies setting spacing between grid tracks.

How do I use grid-gap to control spacing in a grid layout?

You can define the space between rows and columns using grid-gap, like this: grid-gap: 20px 30px; , which sets 20px row gaps and 30px column gaps.

What is the difference between grid-gap and gap?

Grid-gap was the original property for spacing in grid layouts, but gap is the more modern and preferred property because it can be used for both grid and flexbox layouts.

Can I use grid-gap with only one value?

Yes, if you provide only one value for grid-gap, it will be applied to both rows and columns equally, for example: grid-gap: 10px;.

How does grid-gap work with grid-template-areas?

Grid-gap works seamlessly with grid-template-areas by automatically applying the defined spacing between named grid areas, enhancing the layout’s clarity and spacing.



Previous Article
Next Article

Similar Reads

CSS grid-column-gap Property
The grid-column-gap property in CSS is used to set the size of the gap between the columns in a grid layout. Syntax: grid-column-gap: none | length | initial |inherit;Property Values: Property Value Definition none It is used to set the grid-column-gap property to its default value. The default value of grid-column-gap is 0. length The size of the
3 min read
CSS grid-row-gap Property
The grid-row-gap property in CSS is used to define the size of the gap between the grid elements. The user can specify the width of the gap separating the rows by providing a value to the grid-row-gap. Syntax: grid-row-gap: length | percentage | global-values;Property Values: length: The user can set the grid-row-gap value to be a fixed length meas
3 min read
How to Specify Divider gap in Square Grid using Bootstrap ?
The CSS grid-row-gap property is used to set the size of the gap between the grid row elements. Similarly, the CSS grid-column-gap property is used to set the size of the gap (gutter) between the column elements. Syntax: CSS grid-column-gap propertygrid-column-gap: none|length|percentage|initial|inherit;CSS grid-row-gap propertygrid-row-gap: length
2 min read
How to make a grid without calling CSS Grid property ?
In this article, we will see how to make a grid without calling CSS Grid property, along with understanding its implementation through the examples. It is essential to understand how CSS grid property works but what if we do not use the CSS grid property to make a grid. This is the generic question asked by the interviewer, which can give you a bet
4 min read
CSS gap Property
The CSS gap property defines the size of the gap between rows and columns in flexbox, grid, or multi-column layouts. It combines row-gap and column-gap into one property, allowing spacing to be set in length units (e.g., px, em) or percentages. Syntax gap: &lt;row-gap&gt; &lt;column-gap&gt;Property valuesProperty ValueDescription&lt;length&gt;Speci
2 min read
CSS column-gap Property
The column-gap property in CSS is used to specify the amount of gap between the columns in which a given text is divided using the column-count property. Syntax: column-gap: length | normal | initial | inherit;Default Value: normal Property Values: Property Value Description length This value specifies the length that will set the gap between the c
3 min read
Pure CSS Regular Grid vs Responsive Grid
Pure CSS is a free and open-source CSS framework. It is a collection of tools for creating online programs and responsive web pages. This was developed by Yahoo and is used to make websites that load faster, look better, and are easier to use. Bootstrap is effectively replaced by it. When developing Pure CSS, the responsive design was taken into ac
4 min read
Line-Based Placement of Grid Items with CSS Grid Layout
CSS Grid Layout is a flexible tool for designing web pages. One of its key features is line-based placement, which allows you to position items exactly where you want them using grid lines. This guide will explain different ways to use line-based placement in CSS. CSS Grid Layout divides a container into rows and columns, which are defined by grid
5 min read
How to Set the Gap Between Text and Underlining using CSS ?
Introduction: CSS is used to make the website visually more appealing and more readable. It can be used to format the text on the website in various ways like color, font-size, font-family, etc. In this article, we are going to see how in the case of underlined text, increase the gap between text and underline.Approach: The trick that we are going
1 min read
Tailwind CSS Gap
This class accepts more than one value in tailwind CSS all the properties are covered as in class form. It is the alternative to the CSS gap property. This class is used to set the spacing also caller gutter between the rows and columns. As like column-gap and row-gap using separately both so that one can use simply gap property that can given colu
3 min read
How to set length to set the gap between the columns in CSS ?
In this article, we will learn how to set the length that will set the gap between the columns. A web page's text content can be organized into many columns using CSS, which is a strong tool for web development. Long paragraphs of text can be divided into smaller, easier-to-read sections by using columns. Approach: The column-gap property is used t
3 min read
What is the purpose of using the Gap Classes in Tailwind CSS ?
Tailwind CSS Gap Classes such as gap-[size], are used to define the gap or spacing between grid or flex container child elements. These classes simplify the management of spacing, creating consistent layouts by controlling the space between elements horizontally (with gap-x-[size]) and vertically (with gap-y-[size]). Syntax:&lt;div class="grid grid
1 min read
How to use grid element using the grid auto placement rules?
In this article, you will learn to use grid elements using the grid auto-placement rules. You can use auto as a property value on the grid to auto-enable the container to customize its size on the basis of the content of the items in the row or columns. Syntax: grid-template-rows : auto grid-template-columns: auto Property Value: auto : The size of
2 min read
Bootstrap 5 Grid system Grid tiers
Bootstrap Grid system is one of the most versatile layouts which we can use and it has different responsive variants which can be used to add custom responsiveness. Across the six default breakpoints, the grid works smoothly, and all these breakpoints can also be called Grid Tiers which can be changed using the SASS version. For this, we can use th
5 min read
How To Use Grid-Row-Align And Grid-Column-Align?
The grid-row-align and grid-column-align properties don't exist in CSS. Instead, CSS Grid uses properties like "align-items" and "justify-items" for controlling alignment across rows and columns, allowing precise positioning of grid items within their cells. so we will go to see how can we align the grid items. These are the following ways: Table o
3 min read
Bulma Default Gap
In this article, we'll be seeing the Bulma Default gap. Bulma default gap is used in columns in which each column has the same or equal gap as of variable that has a value of 0.75rem. Bulma Default gap Class: There is no such specific class used but the column class can be used to give the gap between different columns. Syntax: &lt;div class="colum
1 min read
How to keep gap between columns using Bootstrap?
We can keep gap between columns by using normal CSS but here we will use the Bootstrap framework for that. In this article, we will keep a measured gap between columns by the following methods. Using "div" tag: Simply adding a "div" with padding in between two "div" tags gives spacing between the "div". Example: &lt;!DOCTYPE html&gt; &lt;html lang=
2 min read
Bulma Variable Gap
Bulma Variable Gap class is used to create defined gap between columns, there are 9 modifiers of options in this class. All the classes are mentioned and described below. This class can be used as column width. Bulma Variable Gap: is-0: This class removes the gap between classes. It is similar to the Gapless class.is-1: This class creates a gap bet
2 min read
Bulma Gap
The Bulma Gap class is used to customize the gap between columns, there are lots of classes to create gaps between columns by using Bulma. It is the alternative to the CSS gap property. Bulma Gap Class: Bulma Default gap: This class is used to set the default gap between columns which has a default value of 0.75rem, so this gap will be placed on bo
2 min read
How to Bridge the Gap Between Web Designing and Development?
This has been one of the major concerns for IT firms between these two domains. As we know, a designer and a developer, both play a diverse role in a product. Thus, it becomes necessary to align them on the same path. For instance, elements such as arranging the menu or color palettes are being taken care of by designers whereas, the back-end code,
6 min read
Bootstrap 5 Spacing Gap
The spacing utility is used to assign responsive-friendly margin or padding values to an element or a subgroup of its sides. When the display is set to grid, you can use gap utilities on the parent grid container. With responsive gap utilities, you don’t have to add margin utilities to individual grid items. These are generated via our utility API,
2 min read
How to get rid of the gap under the image ?
The gap under images in HTML often occurs due to default inline or inline-block display properties, which align images along the text baseline, leaving extra space as if accommodating text descenders. To eliminate this gap, you can adjust the image's CSS properties, such as setting display: block; vertical-align: bottom; or reducing margin and padd
3 min read
Difference between CSS Grid and CSS Flexbox
GridCSS Grid Layout, is a two-dimensional grid-based layout system with rows and columns, making it easier to design web pages without having to use floats and positioning. Like tables, grid layout allow us to align elements into columns and rows. To get started you have to define a container element as a grid with display: grid, set the column and
4 min read
How to center a &lt;div&gt; using CSS grid Property ?
In this article, we will learn to center an HTML &lt;div&gt; element using the CSS grid property. We can center any element using the below-mentioned approaches. Here we will learn to center a div in 5 different ways. Table of Content Centering div - CSS Grid and place-selfCentering div - CSS Grid and place-itemsCentering div - CSS Grid and place-c
5 min read
CSS grid Property
It is a CSS property that offers a grid-based layout system, with rows and columns, making it easier to design web pages without floats and positioning.  Try It: .item { border: 1px solid gray; font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif; border-radius: 3px; margin: auto; margin-top: 5px; max-width: 320px; min-widt
4 min read
Auto-Fit vs Auto-Fill Property in CSS Grid
CSS Grid is a powerful layout system that allows you to create complex, responsive layouts with ease. One of its key features is the ability to create a responsive layout without using media queries. Instead, you can use the grid-template-columns property, the repeat() function, and the auto-fit and auto-fill keywords to adjust the layout based on
4 min read
CSS grid-area Property
The grid-area property specifies a grid item's size and location in a grid layout. It allows you to define the starting and ending row and column lines for an item, or to assign a name to the item for easy reference in grid templates. Syntax grid-area: grid-row-start | grid-column-start | grid-row-end | grid-column-end | itemname;Property ValuesPro
3 min read
CSS grid-auto-columns Property
The grid-auto-columns CSS property specifies the size of implicitly-created grid columns using values like auto, lengths, percentages, minmax(), and more, ensuring flexible and controlled grid column sizing. Syntaxgrid-auto-columns: auto | max-content | min-content | length | percentage | minmax(min, max) | initial | inherit;Property ValuesProperty
5 min read
CSS grid-auto-flow Property
The grid-auto-flow property Specifies exactly how auto-placed items get flowed into the grid. Syntax grid-auto-flow: row | column | row dense | column dense;Here we explore one by one Table of Content Using grid-auto-flow: rowUsing grid-auto-flow: columnUsing grid-auto-flow: column denseUsing grid-auto-flow: row denseUsing grid-auto-flow: rowThe gr
4 min read
CSS grid-auto-rows Property
The grid-auto-rows property in CSS is used to specify the size for the rows of implicitly generated grid containers. Syntaxgrid-auto-rows: auto |max-content | min-content | length | percentage | minmax(min, max) | initial | inherit;Property ValuesProperty Value Description auto This is the default value. The size is determined implicitly according
5 min read