Introduction

CSS Grid Layout is the most powerful layout system available in CSS. It is a 2-dimensional system, meaning it can handle both columns and rows, unlike flexbox which is largely a 1-dimensional system. You work with Grid Layout by applying CSS rules both to a parent element (which becomes the Grid Container) and to that elements children (which become Grid Items).

Grid Layout

The CSS Grid Layout Module offers a grid-based layout system, with rows and columns, making it easier to design web pages without having to use floats and positioning.

Grid Elements

A grid layout consists of a parent element, with 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>

Display Property

An HTML element becomes a grid container by setting the display property to grid or inline-grid.

Example: .grid-container {
display: grid;
}

All direct children of the grid container automatically become grid items.

Grid Columns

The vertical line of grid items are called columns.

Sample code.

Grid Rows

The horizontal line of grid items are called rows.

Sample code.

Grid Gaps

The space between each column/row are called gaps.

Sample code.

You can adjust the gap size by using one of the following properties:

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

Grid Lines

  • The line between columns are called column lines.
  • The line between rows are called row lines.
Sample code.

Grid Container

To make an HTML element behave as a grid container, you have to set the display property to grid or inline-grid.
Grid containers consist of grid items, placed inside columns and rows.To make an HTML element behave as a grid container, you have to set the display property to grid or inline-grid. Grid containers consist of grid items, placed inside columns and rows.

The grid-template-columns Property

The grid-template-columns property defines the number of columns in your grid layout, and it can define the width of each column.
The value is a space-separated-list, where each value defines the length of the respective column.
If you want your grid layout to contain 4 columns, specify the width of the 4 columns, or "auto" if all columns should have the same width.

Example: .grid-container{
display: grid;
grid-template-columns: auto auto auto auto;
}

The grid-template-rows Property

The grid-template-rows property defines the height of each row.

The value is a space-separated-list, where each value defines the the height of the respective row:

Example: .grid-container {
display: grid;
grid-template-rows: 80px 200px;
}

The justify-content Property

The justify-content property is used to align the whole grid inside the container.

Example: .grid-container {
display: grid;
justify-content: space-evenly;
}
Note:The grid's total width has to be less than the container's width for the justify-content property to have any effect. Example: .grid-container {
display: grid;
justify-content: space-around;
}
Example: .grid-container {
display: grid;
justify-content: space-between;
}
Example: .grid-container {
display: grid;
justify-content: center;
}
Example: .grid-container {
display: grid;
justify-content: start;
}
Example: .grid-container {
display: grid;
justify-content: end;
}

The align-content Property

The align-content property is used to vertically align the whole grid inside the container.

Example .grid-container {
display: grid;
height: 400px;
align-content: center;
}
Note:The grid's total height has to be less than the container's height for the align-content property to have any effect Example .grid-container {
display: grid;
height: 400px;
align-content: space-evenly;
}
Example .grid-container {
display: grid;
height: 400px;
align-content: space-around;
}
Example .grid-container {
display: grid;
height: 400px;
align-content: space-between;
}
Example .grid-container {
display: grid;
height: 400px;
align-content: start;
}
Example .grid-container {
display: grid;
height: 400px;
align-content: end;
}

CSS Grid Item

consectetur adipiscing elit ut aliquam purus sit amet luctus venenatis lectus magna fringilla urna porttitor

CSS grid-column Property

The grid-column property defines on which column(s) to place an item.
You define where the item will start, and where the item will end. Note:The grid-column property is a shorthand property for the grid-column-start and the grid-column-end properties.

Example

Make "item1" start on line 1 and end on line 5:

.item1 {
grid-column: 1 / 5;
}
Example

Make "item2" start on column 2 and span 3 columns:

.item2 {
grid-column: 2 / span 3;
}

CSS grid-row Property

The grid-row property defines on which row to place an item.
You define where the item will start, and where the item will end.

Note:The grid-row property is a shorthand property for the grid-row-start and the grid-row-end properties. Example

Make "item1" start on row-line 1 and end on row-line 4:

.item1 {
grid-row: 1/ 4;
}

CSS grid-area Property

The grid-area property can be used as a shorthand property for the grid-row-start, grid-column-start, grid-row-end and the grid-column-end properties.

Example

Make "item8" start on row-line 1 and column-line 2, and end on row-line 5 and column line 6:

.item1 {
grid-area: 1 / 2 / 5 / 6;
}

Naming Grid Items

The grid-area property can also be used to assign names to grid items.
Named grid items can be referred to by the grid-template-areas property of the grid container.

Example

Item1 gets the name "myArea" and spans all five columns in a five columns grid layout:

.item1 {
grid-area: myArea;
}
.grid-container {
grid-template-areas: 'myArea myArea myArea myArea myArea';
}

Each row is defined by apostrophes (' ')
The columns in each row is defined inside the apostrophes, separated by a space.

Note:A period sign represents a grid item with no name.