CSS Grid Layout
A grid is a set of intersecting horizontal and vertical lines that define the columns and rows of the grid.
CSS provides a grid-based layout system with rows and columns, allowing us to design web pages more easily without using floats and positioning.
The following is a simple web page layout using grid layout, with six columns and three rows:

Browser support
The latest versions of most browsers now support grid layout.
| 57.0 | 16.0 | 52.0 | 10 | 44 |
Grid items
A grid layout consists of a parent element and one or more child elements.
Example
<div class="grid-container">
<div class="grid-item" 1 div>
<div class="grid-item" 2 div>
<div class="grid-item" 3 div>
<div class="grid-item" 4 div>
<div class="grid-item" 5 div>
<div class="grid-item" 6 div>
<div class="grid-item" 7 div>
<div class="grid-item" 8 div>
<div class="grid-item" 9 div div>Try it »
display property
When an HTML element has its display property set to grid or inline-grid, it becomes a grid container, and all of its direct child elements become grid items.
Example
.grid-container
{
display:
grid
;
}
Try it »
Example
.grid-container
{
display:
inline-grid
;
}
Try it »
Grid tracks
We define the columns and rows in a grid using the grid-template-columns and grid-template-rows properties.
These properties define the tracks of the grid. A grid track is the space between any two lines in the grid.
In the figure below you can see a track highlighted with a green box - the first row track of the grid. The second row has three tracks shown as white boxes.

In the following example we use the grid-template-columns property to create four columns in the grid container:
Example
.grid-container
{
display:
grid
;
grid-template-columns:
auto
auto
auto
auto
;
}
Try it »
In the following example we use the grid-template-rows property to set the height of the rows in the grid container:
Example
.grid-container
{
display:
grid
;
grid-template-rows:
100
px
300
px
;
}
Try it »
The fr Unit
Tracks can be defined using any length unit.
Grid introduces the fr unit to help us create flexible grid tracks. One fr unit represents one equal share of the available space in the grid container.
The following example defines a grid that creates three tracks of equal width, which grow and shrink as the available space changes.
Example
.grid-container
{
display:
grid
;
grid-template-columns:
1
fr
1
fr
1
fr
;
}
Try it »
Grid Cells
A grid cell is the smallest unit in a grid element; conceptually it is much like a cell in a table. Looking back at our earlier example, once a grid element is defined inside a parent element, its child elements will be arranged in the predefined grid cells. In the figure below, we highlight the first grid cell.

Grid Areas
A grid element can span one or more cells in the row or column direction, creating a grid area. A grid area must be rectangular in shape - that is, you cannot create an "L"-shaped grid area. The highlighted grid area in the figure below spans two columns and two rows.

Grid Columns
The vertical line direction of a grid element is called a column (Column).

Grid Rows
The horizontal line direction of a grid element is called a row (Row).

Grid Gaps
Grid gap (column gap) refers to the horizontal or vertical spacing between two grid cells.

You can use the following properties to adjust the gap size:
- grid-column-gap
- grid-row-gap
- grid-gap
The following example uses the grid-column-gap property to set the gap between columns:
Example
.grid-container
{
display:
grid
;
grid-column-gap:
50
px
;
}
Try it »
The following example uses the grid-row-gap property to set the gap between rows:
Example
.grid-container
{
display:
grid
;
grid-row-gap:
50
px
;
}
Try it »
The grid-gap property is shorthand for grid-row-gap and grid-column-gap:
Example
.grid-container
{
display:
grid
;
grid-gap:
50
px
100
px
;
}
Try it »
The grid-gap property can set both the row gap and the column gap at the same time:
Example
.grid-container
{
display:
grid
;
grid-gap:
50
px
;
}
Try it »
Grid Lines
The intersections between columns and between rows are the grid lines.
Grid creates numbered grid lines for us so that we can position each grid element.
For example, the grid with three columns and two rows below has four vertical grid lines (marked with grey circles) and three horizontal grid lines (marked with black circles).

You can refer to these line numbers when positioning grid elements.
The figure below defines four vertical grid lines and four horizontal grid lines:

The numbering of grid lines depends on the writing mode of the document. In left-to-right languages, grid line number 1 is on the far left. In right-to-left languages, grid line number 1 is on the far right.
Next we use the grid-column-start, grid-column-end, grid-row-start and grid-row-end properties to demonstrate how to use grid lines.
In the following example we set a grid element to start at the first column line and end at the third column line:
Example
.item1
{
grid-column-start:
1
;
grid-column-end:
3
;
}
Try it »
In the following example we set a grid element to start at the first row line and end at the third row line:
Example
.item1
{
grid-row-start:
1
;
grid-row-end:
3
;
}
Try it »
CSS Grid Properties
| property | Description |
|---|---|
| column-gap | Specifies the gap between columns |
| gap | row-gap and column-gap Shorthand property for |
| grid | grid-template-rows, grid-template-columns, grid-template-areas, grid-auto-rows, grid-auto-columns, and grid-auto-flow Shorthand property for |
| grid-area | Specifies the name of a grid element, or can also be grid-row-start, grid-column-start, grid-row-end, and LinkedHashMap, among others. grid-column-end Shorthand property for |
| grid-auto-columns | Refers to the default column size |
| grid-auto-flow | Specifies how the auto-placement algorithm works, precisely defining how auto-placed elements are arranged in the grid. |
| grid-auto-rows | Refers to the default row size |
| grid-column | grid-column-start and grid-column-end Shorthand property for |
| grid-column-end | Specifies the end position of a grid element in the column direction |
| grid-column-gap | Specifies the size of the gap between grid elements |
| grid-column-start | Specifies the start position of a grid element in the column direction |
| grid-gap | grid-row-gap and grid-column-gap Shorthand property for |
| grid-row | grid-row-start and grid-row-end Shorthand property for |
| grid-row-end | Specifies the end position of a grid element in the row direction |
| grid-row-gap | Specifies the row gap of grid elements |
| grid-row-start | Specifies the start position of a grid element in the row direction |
| grid-template | grid-template-rows, grid-template-columns and grid-areas Shorthand property for |
| grid-template-areas | Specifies how to display rows and columns, using named grid elements |
| grid-template-columns | Specifies the size of columns and sets the number of columns in the grid layout |
| grid-template-rows | Specifies the size of rows in the grid layout |
| row-gap | Specifies the gap between two rows |
Next we can the CSS Grid Layout mini-gameto test what we have learned.