CSS Grid Container
- Previous Page CSS Grid Layout Module
- Next Page CSS Grid Item
Grid container
To make an HTML element act as a grid container, you must set display
The property is set to grid or inline-grid.
The grid container is composed of grid items placed within columns and rows.
The grid-template-columns property
grid-template-columns
This property defines the number of columns in the grid layout and can define the width of each column.
This value is a list separated by spaces, where each value defines the length of the corresponding column.
If you want the grid layout to contain 4 columns, specify the width of these 4 columns; if all columns should have the same width, set it to "auto".
Example
Generate a grid containing four columns:
.grid-container { display: grid; grid-template-columns: auto auto auto auto; }
Note:If there are more than 4 items in a 4-column grid, the grid will automatically add new rows and place these items in them.
grid-template-columns
This property can also be used to specify the size (width) of the columns.
Example
Set the sizes of these 4 columns:
.grid-container { display: grid; grid-template-columns: 80px 200px auto 40px; }
The grid-template-rows property
grid-template-rows
This property defines the height of each column.
Its value is a list separated by spaces, where each value defines the height of the corresponding row:
Example
.grid-container { display: grid; grid-template-rows: 80px 200px; }
The justify-content property
justify-content
The property is used to align the entire grid within the container.
Note:The total width of the grid must be less than the width of the container so that the justify-content property can take effect.
Example
.grid-container { display: grid; justify-content: space-evenly; }
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
align-content
The property is used to vertically align the entire grid within the container.
Note:The total height of the grid must be less than the height of the container for the align-content property to take effect.
Example
.grid-container { display: grid; height: 400px; align-content: center; }
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; }
- Previous Page CSS Grid Layout Module
- Next Page CSS Grid Item