Flexbox Mental Model
- css
- We have a Primary Axis (determined by the
flex-direction
property) and a Cross Axis - To distribute all items on the Primary Axis, we use
justify-content
- To position all items along the Cross Axis we use
align-items
- To align a particular item along the Cross Axis we use
align-self
- It is important to note that we can position items on the cross axis without impacting other items. The same is not true for the primary axis where we can only talk about how to distrubute the group.
- Keyword summary
- Justify => Primary
- Align => Cross
- Content => A group of "stuff" that can be distributed
- Items => Single items that can be positioned individually
- When we provide a
width
in Flexbox, it's known as a hypothetical size - it's not a hard rule, it's more of a suggestion. flex-basis
refers to the size along the primary axis (so the horizontal width if we haveflex-direction: row
)flex-grow
refers to the factor by which an item should grow along the primary axis if there is space. By default this is 0. Absolute values don't matter, its about the ratio between all items'flex-grow
values.flex-shrink
refers to the factor by which an item should shrink along the primary axis. Default value is 1. Set to 0 to prevent shrinking.- If an item has a minimum size e.g.
min-width
then this will always be respected. To override a built-in size setmin-width: 0
but be very careful as normally this is for a reason! margin: auto
will gobble up remaining space- Applying
flex-wrap: wrap
means items won't shrink below their hypothetical size. This gives us a new propertyalign-content
- distribute the group ("content", not "items") along the cross axis (i.e. "align")
Resources
https://www.joshwcomeau.com/css/interactive-guide-to-flexbox/