CSS grid cheat sheet
Parents
Properties to apply to the parent grid container element.
-
display: gridTurn a parent, container element into a grid.
Causes all children to flow into grid cells.
display: grid;
-
grid-template-columnsDefine how the columns are measured & how many there are.
/* 3 columns, equal size */ grid-template-columns: 1fr 1fr 1fr; /* 3 columns, different sizes */ grid-template-columns: 20rem 1fr; /* Create 12 equal-width columns */ grid-template-columns: repeat(12, 1fr);
-
grid-template-rowsDefine how the rows are measured & how many there are.
/* 2 rows, equal size */ grid-template-rows: 1fr 1fr; /* 2 rows, different sizes */ grid-template-rows: auto 3rem; /* Create 6 equal-height rows */ grid-template-rows: repeat(6, 1fr);
-
grid-template-areasCreate a series of named areas within the parent to move elements into.
Each grid row is contained by quotes.
Each grid column is a word, separated by spaces, within the row quotes.
/* 3 columns, 3 rows * Areas spanning more than 1 column/row */ grid-template-areas: "masthead masthead toolbar" "main main sidebar" "footer footer sidebar";
-
gap,column-gap,row-gapCreate space like a margin between the columns/rows of the grid.
/* Between columns & rows */ gap: 1rem; /* Between columns */ column-gap: 1rem; /* Between rows */ row-gap: 1rem;
-
justify-content&justify-itemsAlign the elements horizontally within the grid.
contentmoves all the elements together as a group.itemsmoves each element individually within their area.justify-content: center; justify-items: end;
-
align-content&align-itemsAlign the elements vertically within the grid.
contentmoves all the elements together as a group.itemsmoves each element individually within their area.align-content: start; align-items: center;
-
place-content&place-itemsApply both
justifyandalignat the same time.place-content: start; place-items: end;
-
grid-auto-columns&grid-auto-rowsDefine the size of automatically generated intrinsic rows & columns.
grid-auto-columns: 1fr; grid-auto-rows: 10rem;
-
grid-auto-flow: denseAttempt to tightly pack grid cells, even moving cells earlier in the order.
grid-auto-flow: dense;
Children
These properties are to be applied to the children of the grid parent container.
-
grid-columnChoose which column the element starts & finishes at.
/* Start at column line 2 */ grid-column: 2; /* Start at line 2, go to line 6 */ grid-column: 2 / 6; /* Start at line 2, go to last line */ grid-column: 2 / -1; /* Start at line 2, span horizontally 3 rows */ grid-column: 2 / span 3;
-
grid-rowChoose which row the element starts & finishes at.
/* Start at row line 2 */ grid-row: 2; /* Start at line 2, go to line 6 */ grid-row: 2 / 6; /* Start at line 2, go to last line */ grid-row: 2 / -1; /* Start at line 2, span vertically 3 rows */ grid-row: 2 / span 3;
-
grid-areaAssign the element to a specific area in the grid.
grid-area: sidebar;
-
justify-selfMove the element itself horizontally within its defined area.
justify-self: center;
-
align-selfMove the element itself vertically within its defined area.
align-self: center;
-
place-selfApply both
justify-self&align-selfat the same time.place-self: center;
Getting fancy
-
Automatic responsive columns
grid-template-columns: repeat(auto-fit, minmax(min(15rem, 100%), 1fr));Fit as many elements across a row, creating new rows as needed, with an ideal size of
15remIf you use this, only change
15rem
-
display: contentsUse on a parent element to get its children onto the grid.
Be careful with accessibility—some browsers don’t fully support it.
Subgrid will eventually be a better option.