CSS Grid - KeynesYouDigIt/Knowledge GitHub Wiki
CSS Grid
Basics
- Define a container with
display: grid - Set column and row sizes with
grid-template: rows / columns - Place child elements with
grid-area
Terminology
- Grid Container: Parent element, has
display: grid - Grid Item: Direct descendants of a grid container
- Grid Line: Dividing line (horizontal or vertical) of the grid
- Grid Track: Space between two lines (columns or rows)
- Grid Cell: Intersection of two tracks
- Grid Area: Space encompassed by 4 grid lines (any size)
Concepts
- Valid units include
px,%,auto,em/rem, andfrfris a fraction of unallocated space in the grid
- You can repeat declarations with
repeat(3, 1fr) - You can give grid-lines labels with
1 fr [optional-label another-optional-label] - You can specify a grid with
grid-template: rows / columns - You can position a grid item by specifying its starting and ending grid line with
grid-row: start / endorgrid-column: start / end- This can include negative values
- You can use the
spankeyword to say how many rows or columns you would like it to span grid-areacombines rows and columns like this:grid-area: row-start / column-start / row-end / column-end- You can also assign it to a
grid-template-areaname
- You can also assign it to a
- You can order an item in the grid with the
orderkeyword - You can label grid areas with:
grid-template-areas: "header header header" "main main sidebar" ". footer footer".means it should be emptynonemeans it's undefined
- You can specify gutters with
grid-column-gapandgrid-row-gap, orgrid-gap: row column - You can specify the justification and alignment of rows and columns with
justify-itemsandalign-items. They acceptstart,end,center, andstretch.- You can override this on items with
justify-selfandalign-self
- You can override this on items with
- You can specify the justification and alignment of the entire grid with
justify-contentandalign-contentif there is excess unused space. Takes the same arguments as flexbox.
Responsive Grids
- Use named areas to assign content to areas, and then redefine the shape of the grid using the media queries.
- You can hack
repeatwithrepeat(autofill, minmax(smallest, largest)). They start at the min size, but grow up to the max size if the content allows - You can allow rows to be automatically generated and sized with
grid-auto-rows: some-size;- If you pass this more than one, it will create a pattern