Flexbox and when to use it
Flexbox is best suited to laying out content blocks within a page that need to flow and change to suit the page. It’s best suited to one-dimensional layouts that flow from one row to the next, as compared to CSS grid which allows for two-dimensional positioning of elements.
Flex containers hold flex items; these child items will no longer display as block-level elements but will arrange themselves according to the properties of the flex container and items. Enable flex properties with display: flex;
on the flex container.
Flex properties
Justify Content
flex-start (default)
flex-end
center
space between
space-around
space-evenly
Align Content (aligns rows vertically)
flex-start (default)
flex-end
center
space between
space-around
space-evenly
Align Items (aligns items vertically)
flex-start (default)
flex-end
center
baseline
stretch
Item Specific Properties
align-self
order
order: 4
order: 5
order: 2
order: 3
order: 1
Flex Flow (Direction & Wrap)
row wrap
row-reverse wrap
column wrap
column-reverse wrap
row wrap-reverse
row-reverse wrap-reverse
column wrap-reverse
column-reverse wrap-reverse
Flex (Grow, Shrink, Basis)
flex-grow
Default: 0
This enables each flex item to grow proportionally to fill the container depending on their flex-grow value. Values can be integers or floats. For example, 3 will absorb 3x as much of the empty space as 1.
flex-shrink
Default: 1
This enables each flex item to shrink proportionally to fit inside the container depending on their flex-shrink value. Shrinking will depend on the content within an item; items will not shrink below their content size. Values can be integers or floats. For example, 3 will shrink 3x as much as 1.
flex-basis
Default: auto
This tells the browser what the ideal size is for the item. It will try to render it this size, but will shrink as necessary to fit within the flex container if enabled. Values must be a dimensioned size such as % or px. Set flex-shrink to 0 and add a wrap to force items to maintain their size and break onto new lines if needed.
flex
Default: 1 0 auto
Format: [flex-grow] [flex-shrink] [flex-basis]
or [max] [min] [ideal]
If there is only one numerical value, that will only set flex-grow
.