Flexbox and CSS Grid Reference
Flexbox helps you layout elements inside a container in 1 dimension (by describing the attributes of a list of elements). CSS Grid helps you layout in 2 dimensions (by specifying the attributes of rows and columns inside a container). This page is a quick reference for both.
Last Updated: October 24th, 2020
Flexbox
For the Flex Container
display
.container { display: flex | inline-flex; }
flex
inline-flex
flex-direction
.container { flex-direction: row | row-reverse | column | column-reverse; }
row
(default): The direction text is laid out in a line (left to right inltr
; right to left inrtl
)row-reverse
: Likerow
, but reversedcolumn
: The direction in which lines of text are stackedcolumn-reverse
: Likecolumn
but reversed
flex-wrap
.container { flex-wrap: nowrap | wrap | wrap-reverse; }
nowrap
(default): All Flex Items will be on one linewrap
: Flex Items will wrap onto multiple lines, top to bottomwrap-reverse
: Likewrap
but bottom to top
flex-flow
.container { flex-flow: column-reverse wrap-reverse; }
Combines the following properties:
justify-content
.container { justify-content: flex-start | flex-end | center | space-between | space-around | space-evenly; }
flex-start
(default): Pack Flex Items from the startflex-end
: Pack Flex Items from the endcenter
: Pack Flex Items around the centerspace-between
: Distribute Flex Items evenly, first item flush with start, last item flush with endspace-around
: Distribute Flex Items evenly, with half-size space at either endspace-evenly
: Distribute Flex Items evenly, with items having equal space around them- Note: There are other values for this property. MDN Reference
align-items
Like justify-content
, but for the cross-axis instead of the main-axis.
.container { align-items: flex-start | flex-end | center | stretch | baseline; }
flex-start
(default): Pack Flex Items from the startflex-end
: Pack Flex Items from the endcenter
: Pack Flex Items around the centerstretch
: Flex Items stretch to fill the containerbaseline
: Align the Flex Items baselines- Note: There are other values for this property. MDN Reference
For the Flex Items
order
.item { order: 1; }
flex-grow
This is the opposite/inverse of flex-shrink
.
.item { flex-grow: 1; }
flex-shrink
This is the opposite/inverse of flex-grow
.
.item { flex-shrink: 1; }
flex-basis
This property sets the initial main size of a Flex Item.
.item { flex-basis: auto; }
flex
Shorthand for flex-grow
, flex-shrink
, and flex-basis
.
The last two parameters are optional. It's recommended to use this property instead of the individual ones.
.item { flex: none | [ <'flex-grow'> <'flex-shrink'>? || <'flex-basis'>]; }
align-self
.item { align-self: auto | flex-start | flex-end | center | stretch | baseline; }
auto
(default): Same as the container'salign-items
valueflex-start
: Pack Flex Items from the startflex-end
: Pack Flex Items from the endcenter
: Pack Flex Items around the centerstretch
: Flex Items stretch to fill the containerbaseline
: Align the Flex Items baselines- Note: There are other values for this property. MDN Reference
CSS Grid
For the Grid Container
display
.container { display: grid | inline-grid; }
grid
inline-grid
grid-template-columns/grid-template-rows
.container { grid-template-columns: 200px 100px auto; grid-template-rows: 25% auto; }
.container { grid-template-columns: repeat(4, 100px); grid-template-rows: repeat(3, 40px); }
column-gap/row-gap
Note: These properties used to be grid-column-gap
and grid-row-gap
.
.container { column-gap: 3rem; row-gap: 20px; }
gap
Note: This property used to be grid-gap
.
A shorthand for row-gap
and column-gap
.
.container { gap: <grid-row-gap> <grid-column-gap>; }
justify-items
Aligns Grid Items along the row axis. See align-items
for aligning items along the column axis.
.container { justify-items: start | end | center | stretch; }
start
: Align Grid Items to be flush with the start edge of the cellend
: Align Grid Items to be flush with the end edge of the cellcenter
: Align Grid Items in the center of the cellstretch
(default): Fills the width of the cell- Note: There are other values for this property. MDN Reference
align-items
Aligns Grid Items along the column axis. See justify-items
for aligning items along the row axis.
.container { align-items: start | end | center | stretch; }
start
: Align Grid Items to be flush with the start edge of the cellend
: Align Grid Items to be flush with the end edge of the cellcenter
: Align Grid Items in the center of the cellstretch
(default): Fills the height of the cell- Note: There are other values for this property. MDN Reference
place-items
Sets both the align-items
and justify-items
properties at once.
.container { place-items: <align-items> / <justify-items>; }
justify-content
If the grid itself is smaller than the grid container, aligns the grid along the inline (row) axis (align-content
aligns along the block (column) axis).
.container { justify-content: start | end | center | stretch | space-around | space-between | space-evenly; }
start
: Align Grid to be flush with the start edge of the Grid Containerend
: Align Grid to be flush with the end edge of the Grid Containercenter
: Align Grid in the center of the Grid Containerstretch
: Resize Grid Items to allow the Grid to fill the width of the grid containerspace-around
: Distribute Grid Items evenly (half-size spaces on either end)space-between
: Distribute Grid Items evenly (no space on either end)space-evenly
: Distribute Grid Items evenly
align-content
If the grid itself is smaller than the grid container, aligns the grid along the block (column) axis (justify-content
aligns along the inline (row) axis).
.container { align-content: start | end | center | stretch | space-around | space-between | space-evenly; }
start
: Align Grid to be flush with the start edge of the Grid Containerend
: Align Grid to be flush with the end edge of the Grid Containercenter
: Align Grid in the center of the Grid Containerstretch
: Resize Grid Items to allow the Grid to fill the width of the grid containerspace-around
: Distribute Grid Items evenly (half-size spaces on either end)space-between
: Distribute Grid Items evenly (no space on either end)space-evenly
: Distribute Grid Items evenly
place-content
Sets both the align-content
and justify-content
properties at once.
.container { place-content: <align-content> / <justify-content>; }
For the Grid Items
grid-column-start / grid-column-end / grid-row-start / grid-row-end
Determines a Grid Item's location in the Grid using grid lines. If no grid-column-end
or grid-row-end
is declared, the Grid Item will span 1 track by default.
.item { grid-column-start: <number> | <name> | span <number> | span <name> | auto; grid-column-end: <number> | <name> | span <number> | span <name> | auto; grid-row-start: <number> | <name> | span <number> | span <name> | auto; grid-row-start: <number> | <name> | span <number> | span <name> | auto; }
<number>
Refer to a numbered Grid line<name>
: Refer to a named Grid linespan <number>
: The Grid Item will span across this number of Grid tracksspan <name>
: The Grid Item will span until it hits the named Grid lineauto
: Auto-placement, automatic span, or a default span of one
grid-column / grid-row
Sets both the start
and end
properties for either the column or row.
.item { grid-column: <start-number|start-name> / <end-number|end-name>; grid-row: <start-number|start-name> / <end-number|end-name>; }