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

CSS grid Property

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

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:

Basic Grid
Dense Grid
Nested Grid
1
2
3
4

Syntax:

grid: none|grid-template-rows / grid-template-columns|grid-template-areas|
grid-template-rows / [grid-auto-flow] grid-auto-columns|[grid-auto-flow]
grid-auto-rows / grid-template-columns|initial|inherit;

Property values:

ValueDescription
noneIt is default value no specific size mentioned for row and column.
grid-template-rows / grid-template-columnsIt is used to specifies the size of rows and columns.
grid-template-areasIt is used to specifies the grid layout using named items.
grid-template-rows / grid-auto-columnsIt is used to specifies the auto size(height) and sets the auto size columns.
grid-auto-rows / grid-template-columnsIt is used to specifies the auto size and sets the auto grid size columns.
grid-template-rows / grid-auto-flow grid-auto-columnsIt is used to specifies how to place items and auto size row and columns.
grid-auto-flow grid-auto-rows / grid-template-columnsIt is used to specifies how to place items and auto size row and grid-template columns.

Example 1: Grid with 2-rows and 4-column. 

HTML
<!DOCTYPE html> 
<html> 

<head> 
    <title> 
        CSS | grid Property 
    </title> 
    <style> 
        .main { 
            display: grid; 
            grid: auto auto / auto auto auto auto; 
            grid-gap: 10px; 
            background-color: green; 
            padding: 10px; 
        } 
        
        .gfg { 
            background-color: lightgrey; 
            text-align: center; 
            padding: 25px 0; 
            font-size: 30px; 
        } 
    </style> 
</head> 

<body> 

    <h1>Welcome to GFG </h1> 
    <h3>This tutorial is on CSS grid property</h3> 

    <div class="main"> 
        <div class="gfg">1</div> 
        <div class="gfg">2</div> 
        <div class="gfg">3</div> 
        <div class="gfg">4</div> 
        <div class="gfg">5</div> 
        <div class="gfg">6</div> 
        <div class="gfg">7</div> 
        <div class="gfg">8</div> 
    </div> 

</body> 

</html> 

Output :  

This can be used as a shorthand property for : 

  • grid-template-rows : Specifies the size of the rows.
  • grid-template-columns : This specifies the size of the columns.
  • grid-template-areas : This specifies the grid layout using named items.
  • grid-auto-rows : This specifies the auto size of the rows.
  • grid-auto-columns : This specifies the auto size of the columns.
  • grid-auto-flow : This specifies how to place auto-placed items, and the auto size of the row.

Example 2: This is example for the grid-template-rows and grid-template-columns. 

HTML
<!DOCTYPE html> 
<html> 

<head> 
    <title> 
        CSS | grid Property 
    </title> 
    <style> 
        .main { 
            display: grid; 
            grid-template-columns: 156px 156px 156px 156px; 
            grid-template-rows: 100px 200px; 
            grid-gap: 5px; 
            background-color: green; 
            padding: 5px; 
        } 
        
        .gfg { 
            background-color: lightgrey; 
            text-align: center; 
            padding: 20px 0; 
            font-size: 30px; 
        } 
    </style> 
</head> 

<body> 
    <h1>Welcome to GFG </h1> 
    <h3>This tutorial is on CSS grid property</h3> 

    <div class="main"> 
        <div class="gfg">1</div> 
        <div class="gfg">2</div> 
        <div class="gfg">3</div> 
        <div class="gfg">4</div> 
        <div class="gfg">5</div> 
        <div class="gfg">6</div> 
        <div class="gfg">7</div> 
        <div class="gfg">8</div> 
    </div> 

</body> 

</html> 

Output : Height of the first row is set to 100px and height of the second row is set to 200px and width of every column is set to 156px. 

Supported Browsers: The browser supported by CSS | grid Property are listed below:

  • Google Chrome 57
  • Edge 16
  • Firefox 52
  • Opera 44
  • Safari 10.1


Previous Article
Next Article

Similar Reads

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
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 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
How to center a <div> using CSS grid Property ?
In this article, we will learn to center an HTML <div> 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
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
CSS grid-column Property
The CSS grid-column property specifies a grid item's size and location in a grid layout. It controls the placement and span of grid items, allowing the specification of starting and ending column lines. This enables flexible and responsive design without altering the HTML structure. Syntaxgrid-column: grid-column-start | grid-column-end;Property Va
4 min read
CSS grid-column-end Property
The CSS grid-column-end property specifies the ending position of a grid item within a grid layout. The grid-column-end property defines how many columns an item will span, or on which column line the item will end, allowing precise placement within the grid structure. Syntaxgrid-column-end: auto |span n | column-line;Property ValuesProperty Value
4 min read
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-column-start Property
The grid-column-start property in CSS defines the starting column line for a grid item within a grid container, offering significant layout flexibility. This property can utilize various values to control item placement, enhancing the responsive design of web pages. Syntaxgrid-column-start: auto | span n | column-line;Default Value: autoProperty Va
4 min read
CSS grid-gap Property
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 propertygrid-row-gap propertySyntax: grid-gap: grid-row-gap grid-column-gap;Prope
3 min read
CSS grid-row Property
The grid-row property in CSS is used to define the size and position of a grid item within a grid layout. It combines the grid-row-start and grid-row-end properties to specify the item's start and end positions along the row axis. Syntax:grid-row: grid-row-start|grid-row-end;Property Values1. grid-row-start:Specifies the row line where the item wil
3 min read
CSS grid-row-end Property
The grid-row-end property in CSS defines the grid item's end position within a grid row by specifying the inline edge of its grid area. Syntaxgrid-row-end: value;Default Value: auto Property Values: auto: The grid items span for the default value of one row.span int: It specifies the number of rows the item will span.integer: It specifies the row o
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
CSS grid-row-start Property
The grid-row-start property in CSS specifies the starting position of a grid item within a grid row by defining the inline start edge of its grid area. Syntaxgrid-row-start: auto|span|row-line|initial|inherit;Default value: auto Property Values: auto: It sets the grid-row-start property to its default value of one row.span: It specifies the number
5 min read
CSS grid-template Property
The grid-template property in CSS is a shorthand property for defining grid columns, rows, and areas. It allows you to set values for the following longhand properties: grid-template-rows, grid-template-columns, and grid-template-areas. Syntaxgrid-template: none| grid-template-rows/ grid-template-columns | grid-template-areas | initial | inheritPro
4 min read
CSS grid-template-areas Property
The grid-template-areas property in CSS allows you to define named grid areas within a grid layout. This property specifies how grid items are placed into areas using a named grid template. Let's explore its syntax, values, examples, and browser support. Syntaxgrid-template-areas: none | 'name1 name2 ...' | 'name3 name4 ...';none: Default value ind
3 min read
CSS grid-template-rows Property
The grid-template-rows property in CSS defines the number and height of rows in a grid layout. It accepts values like none, auto, max-content, min-content, and specific lengths, allowing for flexible and responsive design customization. Syntaxgrid-template-rows: none|auto|max-content| min-content|length| initial|inherit;Property ValuesValueDescript
3 min read
CSS grid-template-columns Property
The grid-template-columns property in CSS defines the number and size of columns in a grid layout. It allows you to set both fixed and flexible column sizes, making grid-based designs more adaptable to various layouts and responsive needs. iframe { width: 100%; height: 60vh; border: none; border-radius: 10px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1
4 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
jQWidgets jqxSortable grid Property
jQWidgets is a JavaScript framework for making web-based applications for PC and mobile devices. It is a very powerful and optimized framework, platform-independent, and widely supported. The jqxSortable represents a jQuery plugin that allows you to reorder elements in an HTML list or div tags using the mouse. The grid Property is used to display t
2 min read
CSS | Grid Inspector
The Grid Inspector is a powerful tool that enables to verify the grid system by using browser. In the Mozilla Firefox, there is an inbuilt Grid Inspector but in Google Chrome you have to use the extension. Grid Inspector is pretty much helpful grid system is little complex or spanning many tracks. It become hard to visualize the actual grid that's
1 min read
CSS Grid and place-items together
In this article, we will see how can we use the CSS grid and place-items property together to arrange elements on the web page. place-items is not specifically for the CSS grid it can be used with other properties too. Steps: Create three div with class containerEach div will contain three more div with a class box.Add class initial to the first di
3 min read
Image Grid using CSS
In this article, we will see how can we create a Diamond Grid using HTML and CSS. We will use position property to align images in a grid form. HTML part of code: In this section, we will create a structure of our grid. Approach: Create an ordered list using "ul" and add a class container. Create six "li" tags with class name. Include a "div" tag w
4 min read