Flexbox
Flex containers automatically convert their direct children into flex items, but not deeper descendants. Flexboxes can be nested.
Elements line up from top to bottom by default. Flex items can line up from either axis and from either direction, defaulting to from left to right.
-
display: blox (Default, Block Layout Mode)
ABCDE -
display: flex (Flex Layout Mode)
ABCDE
flex-direction
-
flex-direction: row (default)
ABCDE -
flex-direction: row-reverse
ABCDE -
flex-direction: column
ABCDE -
flex-direction: column-reverse
ABCDE
flex-wrap 1/2
(flex-direction: row)
flex-wrap: nowrap (default)
ABCDEABCDEflex-wrap: wrap
ABCDEABCDEflex-wrap: wrap-reverse
ABCDEABCDE
flex-wrap 2/2
(flex-direction: column)
flex-wrap: nowrap (default)
ABCDEABCDEflex-wrap: wrap
ABCDEABCDEflex-wrap: wrap-reverse
ABCDEABCDE
flex-flow: flex-direction flex-wrap
(shortcut)
flex-flow: column wrap
justify-content (flex-direction: row)
justify-content: flex-start (default)
ABCDEjustify-content: flex-end
ABCDEjustify-content: center
ABCDEjustify-content: space-between
ABCDEjustify-content: space-around
ABCDE
justify-content (flex-direction: column)
justify-content: flex-start (default)
ABCDEjustify-content: flex-end
ABCDEjustify-content: center
ABCDEjustify-content: space-between
ABCDEjustify-content: space-around
ABCDE
align-items (row)
align-item: stretch (default)
ABCDEalign-item: flex-start
ABCDEalign-item: flex-end
ABCDEalign-item: center
ABCDEalign-item: baseline
ABCDE
align-items (column)
align-item: stretch (default)
ABCDEalign-item: flex-start
ABCDEalign-item: flex-end
ABCDEalign-item: center
ABCDEalign-item: baseline
ABCDE
align-self
align-self: flex-start
ABCDEalign-self: flex-end
ABCDEalign-self: center
ABCDEalign-self: baseline
ABCDEalign-self: flex-start
ABCDE
align-content (flex-direction: row)
The effect of this property can only be observed when the flex container is set to 'flex-wrap: wrap' and the flex items wrap around to multiple lines.
align-content: stretch (default)
ABCDEABCDEalign-content: flex-start
ABCDEABCDEalign-content: flex-end
ABCDEABCDEalign-content: center
ABCDEABCDEalign-content: space-around
ABCDEABCDEalign-content: space-between
ABCDEABCDE
align-content (flex-direction: column)
align-content: stretch (default)
ABCDEABCDEalign-content: flex-start
ABCDEABCDEalign-content: flex-end
ABCDEABCDEalign-content: center
ABCDEABCDEalign-content: space-around
ABCDEABCDEalign-content: space-between
ABCDEABCDE
Aligning Items with Margins
Default
ABCDEmargin-right: auto (A)
ABCDEmargin-left: auto (C)
ABCDEmargin-right: auto; margin-left: auto (C)
ABCDEmargin-top: auto (E)
ABCDE
flex: flex-grow flex-shrink flex-basis (shortcut)
This property applies to flex items, not flex containers. Though this property is a shortcut for the three properties 'flex-grow', 'flex-shrink', and 'flex-basis', is it highly recommended to always use 'flex' in order to avoid conflicting default values.
The order of the properties matters for flex. Grow and shrink properties behave like True/False booleans. 1 = on, 0 = off. flex-basis sets the starting dimensions, either to a specific size or a size based on the contents.
Example
List item starts at 200px wide. It is allowed to expand to fill extra space (1), but it is not allowed to shrink (0) narrower than 200px.
li {flex: 1 0 200px; }
flex-grow
Default is 0, which means the item is not permitted to grow wider than the size of its content or its specified width. Having this turned off allows alignment properties to work, but if it is turned on, any extra space is filled, making alignment obsolete.
Assigning a higher flex-grow integer to an item makes it behave as a ratio. In the third example here, 'C' has 3 times more space than the other items, which are set to '1'.
-
flex-grow: 0 (default)
00000 -
flex-grow: 1
11111 -
flex-grow: 3 (C)
11311
flex-shrink
flex-shrink: 0
00000flex-shrink: 1 (default)
11111flex-shrink: integer
21411
flex-basis
Defines the starting size of the item before any wrapping, growing, or shrinking occurs. This can be used instead of the width/height property for flex items. Flex settings also override specified width/height properties for items.
flex-basis: auto (default)
ABCDEflex-basis: 100px
ABCDEflex-basis: 50px (A)
flex-basis: 100px (B, C, D)
flex-basis: 200px (E)ABCDE
flex shortcuts
flex: initial (flex: 0 1 auto)
Prevents the flex item from growing even when there is extra space, but allows it to shrink to fit in the container. Size is based on specified width/height properties, defauting to the size of the content. Justify-content can be used for horizontal alignment.ABCDEflex: auto (flex: 1 1 auto)
Allows items to be fully fexible, growing or shrinking as needed. Size is based on specified width/height properties.
ABCDEflex: none (flex: 0 0 auto)
Creates a completely inflexible flex item, sizing to specified width/height properties. Justify-content can be used for alignment.
ABCDEflex: integer (flex: integer 1 0px)
Creates a flexible item with the flex basis of 0. This means the flex item has absolute flex, and free space is allocated according to the flex number applied to items
Relative flex assigns extra space to items based on the size of the flex item's contents, and it applies to flex items with a flex-basis set to a size other than 0, such as with a width/height value or auto.
Absolute flex assigns extra size proportionally according to universal flex ratios. It applies to flex items with a flex-basis of 0. It is recommended to always inlude a unit after the 0, like 0px or 0%.
1111111111Top row has flex: 1, so it has absolute flex applied. Bottom row is included for comparison. It has flex: 1 1 auto, so it has relative flex applied.
order
The default is order: 0, upon which items will be ordered according to their semantic order. If multiple items have the same order number, it will refer to the semantic order to decide priority. Positive and negative values can both be used. The behavior is somewhat similar to z-index. However, the higher the number, the further pushed down it is, not higher.
order: 1 (A)
order: -1 (E)ABCDE