Currently, among the CSS layout solutions, grid layout can be considered the most powerful one. It divides the webpage into grids and uses these grids to create various layouts. Grid layout and flex layout have some similarities in that they both specify the position of multiple members within a container. The difference is that flex layout is a one-dimensional layout that specifies the position of members relative to the axis, while grid layout divides the container into rows and columns, creating cells, and then specifies the cell in which the members are located, making it a two-dimensional layout.
To use grid layout, specify display: grid;
for the container. The area in grid layout that adopts grid positioning is called the container, and the child elements that adopt grid positioning within the container are called members. The horizontal area in the container is called rows, and the vertical area is called columns, which can be seen as a two-dimensional array. The lines that divide the grids are called grid lines. Normally, there are n + 1
horizontal grid lines for n
rows, and m + 1
vertical grid lines for m
columns. Note that when the container is set to grid layout, the float
, display: inline-block
, display: table-cell
, vertical-align
, and column-*
settings for the container's child elements will be invalidated.
The grid-template-rows
property defines the height of each row. Set as many values as there are rows, and the values can be fixed pixels or percentages. The grid-template-columns
property defines the width of each column. Set as many values as there are columns, and the values can be fixed pixels or percentages.
The repeat()
function can simplify repeated values, automatically repeating the specified rule.
Sometimes, the size of the cells is fixed, but the size of the container is uncertain. If you want each row or column to accommodate as many cells as possible, you can use the auto-fill
keyword to indicate automatic filling. When the container is not sufficient to accommodate the members, it will automatically wrap to the next line.
To represent the proportional relationship, the grid layout provides the fr
keyword. If the widths of two columns are 1fr and 2fr respectively, it means that the latter is twice the size of the former.
The minmax()
function generates a length range, representing a length within that range. It takes two parameters, the minimum value and the maximum value. When there is not enough space, it will automatically reduce the length or width to the set minimum value from the maximum value.
minmax( [ <length> | <percentage> | min-content | max-content | auto ] , [ <length> | <percentage> | <flex> | min-content | max-content | auto ] )
.
The auto
keyword represents that the length is determined by the browser itself, which is basically equal to the maximum width of the column cell, unless the cell content is set with min-width
and its value is greater than the maximum width.
The grid-row-gap
property sets the gap between rows, which is the row spacing.
The grid-column-gap
property sets the gap between columns, which is the column spacing.
Translate into English:
The grid-gap
property is a shorthand for grid-column-gap
and grid-row-gap
. If the second value of grid-gap
is omitted, the browser assumes that the second value is equal to the first value.
Grid layout allows specifying areas, which are composed of one or more grid cells. The grid-template-areas
property is used to define these areas. The naming of the areas affects the grid lines. The starting grid line of each area is automatically named {areaName}-start
, and the ending grid line is automatically named {areaName}-end
.
After dividing the grid, the container's child elements are automatically placed in each grid in order. The default placement order is row by row, but it can be changed to column by column by setting grid-auto-flow
to column
. The grid-auto-flow
property can also be set to row dense
and column dense
, which are mainly used to determine how the remaining items are automatically placed after certain items are specified.
The justify-items
property sets the horizontal position of the content within the grid items. It can take the values start | end | center | stretch
.
stretch
(default): Stretches the content to fill the entire width of the grid cell.start
: Aligns the content to the start edge of the grid cell.end
: Aligns the content to the end edge of the grid cell.center
: Centers the content within the grid cell.The align-items
property sets the vertical position of the content within the grid items. It can take the values start | end | center | stretch
.
stretch
(default): Stretches the content to fill the entire width of the grid cell.start
: Aligns the content to the start edge of the grid cell.end
: Aligns the content to the end edge of the grid cell.center
: Centers the content within the grid cell.The place-items
property is a shorthand for align-items
and justify-items
. If the second value is omitted, the browser assumes it to be the same as the first value.
The justify-content
property determines the horizontal position of the entire content area within the container, i.e., the horizontal distribution of the grid items. It can take the values start | end | center | stretch | space-around | space-between | space-evenly
.
The align-content
property determines the vertical position of the entire content area within the container, i.e., the vertical distribution of the grid items. It can take the values start | end | center | stretch | space-around | space-between | space-evenly
.
The place-content
property is a shorthand for align-content
and justify-content
. If the second value is omitted, the browser assumes it to be the same as the first value.
Sometimes, some items are positioned outside the existing grid. For example, the grid has only 3
columns, but a certain item is specified in the 5th
row. In this case, the browser will automatically generate extra grid to accommodate the item. The grid-auto-columns
and grid-auto-rows
properties are used to set the column width and row height of the automatically created extra grid. Their syntax is exactly the same as grid-template-columns
and grid-template-rows
. If these two properties are not specified, the browser will determine the column width and row height of the new grid based on the size of the cell content.
The grid-column-start
property specifies the vertical grid line where the left border is located, and the grid-column-end
property specifies the vertical grid line where the right border is located.
The grid-row-start
property specifies the horizontal grid line where the top border is located, and the grid-row-end
property specifies the horizontal grid line where the bottom border is located.
You can also name the axes to specify the position.
The grid-column
property is a shorthand for grid-column-start
and grid-column-end
, and the grid-row
property is a shorthand for grid-row-start
and grid-row-end
.
The grid-area
property specifies where an item is placed in the area specified by grid-template-areas
. It can also be used as a shorthand for grid-row-start
, grid-column-start
, grid-row-end
, and grid-column-end
to directly specify the position of the item.
The justify-self
property sets the horizontal position of the content of a cell, and its usage is exactly the same as the justify-items
property. However, it only applies to individual items, and the values can be start | end | center | stretch;
.
The align-self
property sets the vertical position of the content of a cell, and its usage is exactly the same as the align-items
property. It also only applies to individual items, and the values can be start | end | center | stretch;
.
stretch
, which stretches and fills the entire width of the cell.start
: Aligns with the start edge of the cell.end
: Aligns with the end edge of the cell.center
: Centers the content inside the cell.The place-self
property is a shorthand for align-self
and justify-self
.