Adding shorthand flex:1
to each items is indeed handy.
But what it does is only to ignore all predeclared width, and distribute the space evenly between each items. It also doesn't say anything about vertical alignment. Here is how to have more control over alignments of the flexbox.
justify-content
: controls alignment of all items on the main axis.align-items
: controls alignment of all items on the cross axis.align-self
: controls an individual flex item on the cross axis.align-content
: controls space between flex lines on the cross axis. See packing flex lines.gap
, column-gap
, and row-gap
: creates gaps between flex items.justify-content
Controls alignment of all items on the main axis
justify-content: flex-start
justify-content: flex-end
justify-content: center
justify-content: space-between
justify-content: space-around
justify-content: space-evenly
If direction is reversed, of course the start and end are also flipped
flex-direction: row-reverse;
justify-content: flex-start
If the axis is changed, justify content is applied to the vertical space.
flex-direction: column-reverse;
height: 300px;
justify-content: flex-start;
margin: auto
There are no justify-items or justify-self properties. How to part the first few items to the left and the rest to the right? Using margin-left: auto
on the first item pushed to the right or margin-right: auto
on the previous item.
margin-left: auto
on box C
margin-right: auto
on box C
margin: auto
on box C
align-items
Controls alignment of all items on the cross axis.
align-items: stretch
align-items: flex-start
align-items: baseline
flex-start
, being on the top, when the font are all the same.
align-items: flex-end
align-items: center
align-self
Controls an individual flex item on the cross axis.
You can set align-items
property on container that works for each items, or do it at individual item with align-self
property. The values are the same, plus align-self: auto
which reset the value to what is defined at the flex container.
Note on align-self: baseline
: Items with baseline value will base-align among themselves. So if there's only 1 with such value, it will just go to the top instead.
Container below is set with align-items: center
Let's play with flex-direction
on above container
flex-direction: row-reverse
For the vertical flexbox, predefined container's height is removed.
flex-direction: column
flex-direction: column-reverse
It seems like the baseline value doesn't take effect here. I have tried adding padding or margin as well to one of the item with baseline value, but it only send that box away from the left container border, but doesn't affect the other one.
align-content
Controls the distribution of space between rows in wrapped multiplt-line flex container. See packing flex lines.
Without align-content
declaration:
align-content: stretch
align-content: flex-start
align-content: flex-end
align-content: center
align-content: space-between
align-content: space-around
align-content: space-evenly
Since this is new, it's not well supported yet, maybe avoid using.
gap
The flex container below is set to:
flex-wrap: wrap;
justify-content: center;
align-content: center;
To create gap between flex items:
column-gap
row-gap
flex-wrap: wrap
.
gap
column-gap
and row-gap
.