This post was originally posted on my blog
Flexbox is awesome. If you have worked with modern CSS, whether you are rolling your own or using a frameworks like Bootstrap & Foundation, chances are you are using Flexbox for layout and alignment.
To align or to justify..that is the question
For a very long time, I have been trying to figure out what is the difference between align-items, align-content & justify-content and how they work or more importantly when they work.
TL;DR
- If you have set your
flex-directiontorow, thenjustify-contentworks on the horizontal axis andalign-*properties work on the vertical axis. - If you have set your
flex-directiontocolumn, thenalign-*properties work on the horizontal axis andjustify-contentworks on the vertical axis.
See the Pen Align items vs Justify Content by Anirudh Varma
(@thebigfatpanda12) on CodePen.
What separates align-items and align-content?
-
align-itemseffect the alignment of items on the current line. -
align-contenteffects the alignment accross lines of a flex-container. This means that this property has no effect on single-line containers.
The rest of the article is just a brain dump of everything I learned while trying to figure out the above.
Nomenclature
-
Flex Container: An element on which the
displayvalue is set toflex. - Flex Items: Direct children of the flex containers are called flex items.
-
Main Axis: The axis defined by the
flex-directionproperty.
-- flex-direction:row means that elements inside the flex container are aligned next to each other. Technically, these elements are aligned along the inline-axis (just like inline elements).
-- flex-direction:column means that elements are aligned along the vertical axis i.e. below each other. Technically called the block-axis (just like block elements)
Cross Axis: The axis perpendicular to the main axis is called the cross axis.
Single-line Container: Flex containers whose
flex-wrapproperty is set tonowrap. By default every flex container is a single-line container.Multi-line Containers: Flex containers whose
flex-wrapproperty is set towrap
TL;DR with Nomenclature
Now that we know the jargon, we can rewrite the TL;DR as-
-
justify-contentworks on the Main Axis andalign-*properties work on the Cross Axis. -
justify-contentandalign-itemsare similar in their behviour, the difference being thatjustify-contentworks on the the main axis whilealign-itemsworks on the cross axis. -
align-contentworks only on multi-line containers and has no effect on single line containers.
Top comments (1)
Muy buena explicación y muy práctica.👏👏