Updated September 8th, 2019
Flexbox is a one-dimensional layout method for laying out items in rows or columns. Items flex to fill additional space and shrink to fit into smaller spaces.
From CSS Tricks:
The main idea behind the flex layout is to give the container the ability to alter its items’ width/height (and order) to best fill the available space (mostly to accommodate to all kind of display devices and screen sizes). A flex container expands items to fill available free space, or shrinks them to prevent overflow.
From MDN: “The following simple layout requirements are either difficult or impossible to achieve with such tools, in any kind of convenient, flexible way:”
Example from MDN, three
articles within a
section, default (no flex):
display: flex to the
section containing the
Play with these are you read these notes:
display: flex;on the container element to enable flex layout.
flex-direction: property, where property =
flex-flowis a shorthand
flex-wrapproperties, which together define the flex container’s main and cross axes.
This defines the alignment along the main axis. It helps distribute extra free space left over when either all the flex items on a line are inflexible, or are flexible but have reached their maximum size.
flex-start(default): items are packed toward the start line
flex-end: items are packed toward to end line
center: items are centered along the line
space-between: items are evenly distributed in the line; first item is on the start line, last item on the end line
space-around: items are evenly distributed in the line with equal space around them.
space-evenly: items are distributed so that the spacing between any two items (and the space to the edges) is equal.
This defines the default behavior for how flex items are laid out along the cross axis on the current line.
start: cross-start margin edge of the items is placed on the cross-start line
end: cross-end margin edge of the items is placed on the cross-end line
center: items are centered in the cross-axis
baseline: items are aligned such as their baselines align
stretch (default): stretch to fill the container (still respect min-width/max-width)
From CSS-Tricks align-content:
This aligns a flex container’s lines within when there is extra space in the cross-axis, similar to how justify-content aligns individual items within the main-axis.
Note: this property has no effect when there is only one line of flex items.
order: “By default, flex items are laid out in the source order. However, the order property controls the order in which they appear in the flex container.”
flex-grow: “This defines the ability for a flex item to grow if necessary. It accepts a unitless value that serves as a proportion. It dictates what amount of the available space inside the flex container the item should take up.”
flex-shrink: This defines the ability for a flex item to shrink if necessary.
flex-basis: This defines the default size of an element before the remaining space is distributed. It can be a length (e.g. 20%, 5rem, etc.) or a keyword.
This is the shorthand for flex-grow, flex-shrink and flex-basis combined. The second and third parameters (flex-shrink and flex-basis) are optional. Default is 0 1 auto.
It is recommended that you use this shorthand property rather than set the individual properties. The short hand sets the other values intelligently.
This allows the default alignment (or the one specified by align-items) to be overridden for individual flex items.
Note: The default alignment is the alignment set on the container.