Grid - Explicit Grid Flashcards
What is an explicit grid?
An explicit grid is a manually defined grid. In other words the part of the grid were you define a fixed number of lines and tracks that form the grid by using the properties grid-template-rows, grid-template-columns,
and grid-template-areas
.
Example:
.grid { display: grid; grid-template-columns: 1fr 1fr 1fr 1fr; grid-template-rows: 100px 100px; grid-gap: 20px; }
Source css-tricks
What do grid-template-columns and grid-template-rows properties do?
Define 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.
Source MDN
How can you reference grid-lines?
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:
.container { grid-template-columns: [first] 40px [line2] 50px [line3] auto [col4-start] 50px [five] 40px [end]; grid-template-rows: [row1-start] 25% [row1-end] 100px [third-line] auto [last-line]; }
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:
.container { grid-template-rows: [row1-start] 25% [row1-end row2-start] 25% [row2-end]; }
Source css-tricks
How can you define repeating parts on a grid template?
If your definition contains repeating parts, you can use the repeat()
notation to streamline things:
.container { grid-template-columns: repeat(3, 20px [col-start]); }
Which is equivalent to this:
.container { grid-template-columns: 20px [col-start] 20px [col-start] 20px [col-start]; }
If multiple lines share the same name, they can be referenced by their line name and count.
.item { grid-column-start: col-start 2; }
Source css-tricks
What does grid-template-areas property do?
Defines a grid template by referencing the names of the grid areas which are specified with the grid-area property. Repeating the name of a grid area causes the content to span those cells. A period signifies an empty cell. The syntax itself provides a visualisation of the structure of the grid.
Source css-tricks
How do you define a grid-area and use it in a grid-template-area?
Example:
.item-a { grid-area: header; } .item-b { grid-area: main; } .item-c { grid-area: sidebar; } .item-d { grid-area: footer; } .container { display: grid; grid-template-columns: 50px 50px 50px 50px; grid-template-rows: auto; grid-template-areas: "header header header header" "main main . sidebar" "footer footer footer footer"; }
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.
Source css-tricks
What happens to the grid-lines names when you define the template using grid-areas?
When you use the grid-area
s 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 when several areas start or end.
Source css-tricks
What does grid-template property do?
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>
– setsgrid-template-columns
andgrid-template-rows
to the specified values, respectively, and setsgrid-template-areas
tonone
.
.container { grid-template: none | <grid-template-rows> / <grid-template-columns>; }
It also accepts a more complex but quite handy syntax for specifying all three. Here’s an example:
.container { grid-template: [row1-start] "header header header" 25px [row1-end] [row2-start] "footer footer footer" 25px [row2-end] / auto 50px auto; }
That’s equivalent to this:
.container { grid-template-rows: [row1-start] 25px [row1-end row2-start] 25px [row2-end]; grid-template-columns: auto 50px auto; grid-template-areas: "header header header" "footer footer footer"; }
Source css-tricks
What is the problem with the grid-template property?
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
.
Source css-tricks
What do column-gap, row-gap, grid-column-gap, grid-row-gap properties do?
Specifies the size of the grid lines. You can think of it like setting the width of the gutters between the columns/rows.
Values:
<line-size>
– a length value
Example:
.container { /* standard */ column-gap: <line-size>; row-gap: <line-size>; /* old */ grid-column-gap: <line-size>; grid-row-gap: <line-size>; }
Note: The gutters are only created between the columns/rows, not on the outer edges.
Source css-tricks
What do gap, grid-gap do?
A shorthand for row-gap
and column-gap
Values:
<grid-row-gap> <grid-column-gap>
– length values
Example:
.container { /* standard */ gap: <grid-row-gap> <grid-column-gap>; /* old */ grid-gap: < grid-row-gap>\<grid-column-gap>; }
If no row-gap
is specified, it’s set to the same value as column-gap
.
Source css-tricks
What does justify-items property do?
Aligns grid items along the inline (row) axis (as opposed to align-items
which aligns along the block (column) axis). This value applies to all grid items inside the container.
Values:
-
start
– aligns items to be flush with the start edge of their cell -
end
– aligns items to be flush with the end edge of their cell -
center
– aligns items in the center of their cell -
stretch
– fills the whole width of the cell (this is the default)
.container { justify-items: start | end | center | stretch; }
Source css-tricks
What does align-items property do?
Aligns grid items along the block (column) axis (as opposed to justify-items
which aligns along the inline (row) axis). This value applies to all grid items inside the container.
Values:
-
stretch
– fills the whole height of the cell (this is the default) -
start
– aligns items to be flush with the start edge of their cell -
end
– aligns items to be flush with the end edge of their cell -
center
– aligns items in the center of their cell -
baseline
– align items along text baseline. There are modifiers to baseline — first baseline and last baseline which will use the baseline from the first or last line in the case of multi-line text.
.container { align-items: start | end | center | stretch; }
Source css-tricks
What does place-items property do?
place-items
sets both the align-items and justify-items properties in a single declaration.
Values:
-
<align-items> <justify-items>
– The first value setsalign-items
, the second valuejustify-items
. If the second value is omitted, the first value is assigned to both properties.
Source css-tricks
What does justify-content property do?
Sometimes the total size of your grid might be less than the size of its grid container. This could happen if all of your grid items are sized with non-flexible units like px. In this case you can set the alignment of the grid within the grid container. This property aligns the grid along the inline (row) axis (as opposed to align-content
which aligns the grid along the block (column) axis).
Values:
-
start
– aligns the grid to be flush with the start edge of the grid container -
end
– aligns the grid to be flush with the end edge of the grid container -
center
– aligns the grid in the center of the grid container -
stretch
– resizes the grid items to allow the grid to fill the full width of the grid container -
space-around
– places an even amount of space between each grid item, with half-sized spaces on the far ends -
space-between
– places an even amount of space between each grid item, with no space at the far ends -
space-evenly
– places an even amount of space between each grid item, including the far ends
.container { justify-content: start | end | center | stretch | space-around | space-between | space-evenly; }
Source css-tricks