The Complete CSS Flex Box Tutorial
The Complete CSS Flex Box Tutorial
Tutorial
Much like CSS Grid (my other Medium tutorial) Flex
Box is quite complex because it consists of not one
but two element types: The container & items.
Flex
Flex is a set of rules for automatically stretching multiple
columns and rows
of content across parent container.
display:flex
Unlike many other CSS properties, in Flex you have a main
container and
items nested within it. Some CSS flex properties are used
only on the parent. Others only on the items.
You can think of a flex element as a parent container
with display:flex. Elements placed inside this container
are called items. Each container has a flex-start and flex-
end points as shown on this diagram.
If you have more than 6 items listed, even more rows will be
created.
Flex items equally distributed on the Main-Axis. We’ll take a look at
the properties and values to accomplish this in just a moment.
How the rows and columns are distributed inside the parent
element is determined by CSS Flex properties flex-
direction, flex-wrap and a few others that will be
demonstrated throughout the rest of this flex tutorial.
Direction
It’s possible to set direction of the item’s flow by reversing it.
flex-direction:row-reverse changes direction of the item list flow.
The default is row, which means flowing from left to right, as you
would expect!
Wrap
Flow
flex-flow:row wrap-reverse;
flex-flow:row-reverse wrap;
flex-flow:row-reverse wrap-reverse;
align-items
align-items controls the align of items horizontally,
relative to the parent container.
flex-basis
flex-shrink
order
Using order property it’s possible to re-arrange the natural
order of items.
justify-items
One last thing for those who are looking to use CSS
Grid together with Flex Box… CSS grid’s justify-items is
similar to Flex’s justify-content. (The properties
described in the above diagram will not work in Flex, but it’s
pretty much the grid’s equivalent for aligning cell content.)