justify-content
controls the distribution of items along the main axis. The default is flex-start
.
Aside from distributing items using justify-content
, you can space items by using regular margins.
To split the items, use a margin setting of auto
on one of them.
The flex
property controls the size and flexibility of the flex items. It is actually the shorthand for three properties - flex-grow
, flex-shrink
, and flex-basis
. But since they work closely together, you should generally set all of them at once.
It's important to note that flex
is applied to the child flex items themselves, not the parent flex container.
The flex-basis
is the size of each item before any growing or shrinking happens. auto
is the default, which refers to an element's width
setting, if it has one. Otherwise it behaves like content
, and is based on the element's contents.
The default is flex: 0 1 auto
. That is, don't grow, yes shrink, auto width.
Items can grow and shrink at different rates. The effect of this is not always obvious, since flexbox calculates the grow and shrink factors after allotting the flex-basis
size to each item. To make it more obvious, we can set the flex-basis
to 0
.
Very often your flex items will have varying heights. By default, they will all stretch to match the height of the tallest item (an excellent feature!). The align-items
property controls their alignment on the cross axis, which usually means vertically.
You can also align an individual item differently from the others using the align-self
property (not shown).
This used to be much harder.
When you set an element to display: flex
, you're also giving it properties for flex-direction
and flex-wrap
. To change these, you'll usually use the shorthand flex-flow
property.
The default is flex-flow: row nowrap
. Here is flex-flow: row wrap
:
Media objects like photos often have captions or text associated with them. Putting those elements side-by-side is a good use case for flexbox. It also allows you the option to reverse their display order without changing the HTML.
To lay out the flex items in a different order, set the flex-direction
property to either row-reverse
or column-reverse
. Again, flex-direction
is usually specified as part of the flex-flow
shorthand, e.g. flex-flow: row-reverse nowrap
.
Flexbox also has an order
property, which lets you change the visual order of the flex items. While it can be handy, use this property very judiciously, as it does not change the tab order or anything else about the source HTML. If what you're trying to accomplish isn't purely visual, just change the order in the HTML.
By default, the order
setting on all the flex items is 0
, so they will respect the source order. By setting any item to a higher value, it will bump to the end. Negative values will move an item to the beginning.
"Cards" are a very common component in web layouts. Here's an example with multiple cards side-by-side. In this case the cards (in orange) are being laid out horizontally using flexbox, but you could also use grid for this part.
The key here is that each card is set to display: flex
, which makes it both a flex item and a flex container. With their main axis flipped vertical (flex-flow: column nowrap
), the flex-grow
setting will expand the inner items (in green) vertically, instead of horizontally.