Properties for the Grid container
display
Defines the element as a grid container and establishes a new grid formatting context for its contents.
Values:
grid
– generates a block-level gridinline-grid
– generates an inline-level grid
1 | .container { |
grid-template-columns and grid-template-rows
Defines the columns and rows of the grid with a space-separated list of values. The values represent the track size, and the space between them represents the grid line.
Values:
<track-size>
– can be a length, a percentage, or a fraction of the free space in the grid (using thefr
unit)<line-name>
– an arbitrary name of your choosing
1 | .container { |
Grid lines are automatically assigned positive numbers from these assignments (-1 being an alternate for the very last row).
But you can choose to explicitly name the lines. Note the bracket syntax for the line names:
1 | .container { |
Note that a line can have more than one name. For example, here the second line will have two names: row1-end and row2-start:
1 | .container { |
If your definition contains repeating parts, you can use the repeat() notation to streamline things:
1 | .container { |
Which is equivalent to this:
1 | .container { |
If multiple lines share the same name, they can be referenced by their line name and count.
1 | .item { |
The fr
unit allows you to set the size of a track as a fraction of the free space of the grid container. For example, this will set each item to one third the width of the grid container:
1 | .container { |
The free space is calculated after any non-flexible items. In this example the total amount of free space available to the fr units doesn’t include the 50px:
1 | .container { |
grid-template-areas
Values:
<grid-area-name>
– the name of a grid area specified withgrid-area
.
– a period signifies an empty grid cellnone
– no grid areas are defined
1 | .container { |
e.g.
1 | .item-a { |
That’ll create a grid that’s four columns wide by three rows tall. The entire top row will be composed of the header area. The middle row will be composed of two main areas, one empty cell, and one sidebar area. The last row is all footer.
Each row in your declaration needs to have the same number of cells.
You can use any number of adjacent periods to declare a single empty cell. As long as the periods have no spaces between them they represent a single cell.
Notice that you’re not naming lines with this syntax, just areas. When you use this syntax the lines on either end of the areas are actually getting named automatically. If the name of your grid area is foo, the name of the area’s starting row line and starting column line will be foo-start, and the name of its last row line and last column line will be foo-end. This means that some lines might have multiple names, such as the far left line in the above example, which will have three names: header-start,
main-start
, and footer-start
.
grid-template
A shorthand for setting grid-template-rows
, grid-template-columns
, and grid-template-areas
in a single declaration.
Values:
none
– sets all three properties to their initial values<grid-template-rows> / <grid-template-columns>
– sets grid-template-columns and grid-template-rows to the specified values, respectively, and sets grid-template-areas to none
It also accepts a more complex but quite handy syntax for specifying all three. Here’s an example:
1 | .container { |
That’s equivalent to this:
1 | .container { |
Since grid-template doesn’t reset the implicit grid properties (grid-auto-columns, grid-auto-rows, and grid-auto-flow), which is probably what you want to do in most cases, it’s recommended to use the grid property instead of grid-template.