Grid tracks define where content in the grid can be placed, essentially the rows and columns of the grid. There are two types of grid:
The following things can be configured for grid tracks:
grid-template-columns: <column-defs> grid-template-rows: <column-defs>
|
Explicit grid track definitions. |
grid-auto-columns: <column-defs> grid-auto-rows: <column-defs>
|
Implicit grid track definitions. |
grid-template-columns: 10px 20px
|
Fixed size tracks. |
grid-template-columns: 1fr 2fr grid-template-columns: minmax(20px, auto)
|
Flexible size tracks can be defined with the following units:
|
grid-template-columns: repeat(3, 1fr)
|
The repeat utility function allows you to repeat track listings a number of times. |
grid-gap: 15px
|
Sets the gap (gutter/alley) between grid tracks. |
The fr
unit's behaviour is a little more complicated than just 1 fraction of the space of the grid.
The important details are:
1fr
isn't a fraction of the grid's total space, but a fraction
of the available space. This means if there's a fixed with column in there, any fractional
units will divide up the remaining space between themselves, rather than using a fractional size of the entire
grid.
Grid items can grow beyond their fractional size. If the min-content
size of
a grid item is greater than the configured fractional amount for a column then the column size will expand
to fit that content. In this case the defined fractional amount will be different than actual columns size.
Note you can fix this by using the minmax(0, 1fr)
function to set the minimum width of a column
to 0. This basically causes the grid to ignore min-content
and always size to the given
fractional amount.
fr
unit sets the minimum width of a track as a fraction of the
available space.
fr
unit instead of %
?
fr
unit allows you to take up a fraction of remaining space, not just total space.gap
property will work automatically with fr
, but not with %
.
The minmax()
function allows you to set a minimum and maximum on a grid track.
The auto-fill
and auto-fit
properties can be used to automatically determine the number of
columns to use based on the available space and the configured column sizes.
Some rules:
fr
or auto
, then any remaining space will be divided up
among the columns.
The difference between auto-fill
and auto-fit
can be seen when the number of grid items
is less than the number of automatically created tracks:
auto-fill
will leave empty tracks thereauto-fit
will collapse empty tracks and divide space between tracks with defined grid items.
The core difference between :
repeat(3, minmax(30px, 1fr))
and
repeat(auto-fill, minmax(30px, 1fr))
auto-fill
automatically changes the number of columns used, where as the fixed one doesn't.
Whenever you explicitly place items on a grid you specify their position using grid lines. Relevant properties:
grid-column-start: 1 grid-column-end: 3 grid-column: <grid-column-start> / <grid-column-end>
|
Column positions for a grid item. |
grid-row-start: 1 grid-row-end: 3 grid-row: <grid-row-start> / <grid-row-end>
|
Row positions for a grid item. |
grid-area: <grid-row-start> / <grid-column-start> / |
Shorthand property for entire grid item area. |
grid-row: 2 / span 4 |
span specifies that an item should span the given number of tracks. |
grid-column: -1 / -2 |
Negative numbers start from the end track. |
Other notes:
span 1
.Grid lines can be given names and referenced elsewhere by their names.
grid-template-columns: [line-name-1] 1fr [line-name-2] 2fr [line-name-3]
grid-column: line-name-1 line-name-3
[line-name-1 line-name-2]
The same line names can be used for multiple lines. Note:
span
is used with a line name it will apply only to lines with a matching name.grid-row-end: line-name 3
repeat
with a line name will cause multiple lines to use the given name.You can define how items will be placed in a grid using a string template syntax. See below for details:
grid-area: grid-area-name |
Grid items are given a name with the grid-area property. |
grid-template-areas: |
The grid container then has a template defined which references these named areas and positions the corresponding items as defined in the template. Each row is a new quoted line. Each name in a line is a column. |
grid-template: |
A shorthand alternative to grid-template-areas which allows you also specify the
row and column definitions. The row definitions are at the end of each row and the column definitions
are below each column.
|
grid: <grid-template> |
Exactly the same as grid-template , but also resets implicit grid properties (for
auto placement).
|
Notes:
.
".
If you define a named grid area then you will automatically get corresponding named lines generated for that area.
E.g. if you have an area named header
then you will get the following line names automatically
generated for both columns and rows: header-start
and header-end
.
The same applies in reverse as well. If you have lines with the same name that form an area then the corresponding
named area is automatically generated and can be used to place grid items. The lines must start with the same name
and end in -start
and -end
.
The previous sections have covered explicitly placing items in the grid. Items that are not placed explicitly as defined above will be placed automatically. Automatic placement happens as follows:
Here are some relevant properties:
grid-auto-rows: 1fr |
Defines implicit grid rows. Multiple tracks are supported and repeat once used (not supported in
Firefox). The default value is auto .
|
grid-auto-columns: minmax(50px, auto) |
Defines implicit grid columns. Multiple tracks are supported and repeat once used (not supported in
Firefox). The default value is auto .
|
grid-auto-flow: [row | column] [dense] |
Defines whether extra rows or columns are created when the explicit grid is full.
Defaults to row . If you specify dense then no empty gaps will be left is possible (defaults to
sparse where empty spaces aren't back-filled).
|
order: 2 |
Can be used to control the order grid items are automatically placed in the grid (defaults to
0 ). Items that have the same order are placed in DOM source order. |
grid-column-start: auto grid-column-end: span 2 |
You can combine auto placement with track spanning if you set an end property to span and
leave the start property as auto . This will leave empty spaces if it can't fit. By default smaller
items won't go back and fill empty spaces if they can fit (unless dense is specified as described
above). |
In CSS grid you can align items in grid areas, and grid tracks within the grid. Alignment applies when the item or track is smaller than the area or grid. There are two axis to be aware of:
The following properties apply:
Property | Applies to | Axis | Individual or all | Default | Notes |
---|---|---|---|---|---|
align-self |
Items in grid areas | Block | Individual | stretch start if the item has an intrinsic aspect ratio |
|
align-items |
All | ||||
justify-self |
Inline | Individual | |||
justify-items |
All | ||||
place-self |
Block and inline | Individual | |||
place-items |
All | ||||
margin |
Block or inline | Individual | 0 |
You can also align items with fixed or auto margins. |
|
align-content |
Tracks in grid | Block | All | start (why grid items appear in top left) |
If an item spans multiple tracks then it may be stretch when track alignment changes. |
justify-content |
Inline | ||||
place-content |
Block and inline |