CSS Flexbox Cheatsheet 2021 | Coding Fantasy
If you are new to Flexbox, you can learn everything you need to know in just 10 minutes. Here's a complete, comprehensive cheatsheet to it.
So, what is Flexbox?
Let's start with the not-so-interesting but essential to understand flexbox information. The full name of flexbox is CSS Flexible Box Layout. By the name, we can see that flexbox is all about making our layout flexible. And that's right, it changes the way the modern layout is built.
But how? What’s unique about the flexbox that makes it better than the block or inline model?
The main advantage of flexbox is it allows the flex container to change the width, height, and most importantly the order of its elements to best fill the available space.
This was not possible (almost) before the flexbox. Flexbox has changed the game. And now everyone uses flexbox on a daily basis to build modern layouts.
But what exactly is Flexbox? Is it some CSS property?
Flexbox is an entire module and not a single property. It involves a lot of things including a whole set of properties. Before we dig into each of them, let’s talk a little about the terminology.
In this picture, you can find all the basic concepts behind the flexbox. First of all, let’s look at the flex container and the flex item.
The flex container is the parent element. All child elements inside it are called “flex items.” Flex items are only one-level deep children. So if we have some other elements inside “flex items” these elements are not called children of the flex container and the flexbox style of the container doesn’t apply to them.
The next major part of the flexbox is the axis. A Flexbox has a cross axis and main axis.
The main axis of a flex container is the primary axis along which flex items are laid out. It can be horizontal and vertical, depending on the
The cross axis is always perpendicular to the main axis.
main-end refer to the start and the end of the element by the main axis.
cross-end refer to the start and the end of the element by the cross axis.
cross-size refer to the size of the element by main and cross axis.
Now, we know everything we need to start learning the flexbox.
Flexbox Container Properties
There are two types of flexbox properties: the parent (flex-container) and the children (flex-item). Let’s start with the parent.
display property defines a flex container. It enables a flex context for all its direct children
It has two values:
Each defines the type of the flex container: inline or block. This depends on the given value.
This establishes the main axis. It determines the direction in which the flex items are placed in the flex container. There are four possible values of the
flex-direction: row; (default)
This sets the main axis direction from left to right.
This sets the main axis direction from right to left.
This sets the main axis direction from top to bottom.
This sets the main axis direction from bottom to top.
By default, flex items will all try to fit onto one line.
flex-wrap can change that and allow the items to wrap as needed with this property.
“flex-wrap” has three properties:
flex-wrap: nowrap; (default)
All flex items will be on one line.
Flex items wrap onto multiple lines, from top to bottom.
Flex items will wrap onto multiple lines from bottom to top.
This is a shorthand for the
flex-wrap properties, which together define the flex container’s main and cross axes. The default value is
flex-flow: row nowrap.
justify-content defines the alignment along the main axis. It has six main properties:
justify-content: flex-start; (default)
Flex items are packed toward the start of the flex-direction.
Flex items are packed toward the end of the flex-direction.
Flex items are centered along the main axis.
Flex items are evenly distributed in the line. The first item is on the start line, the last item on the end line.
Flex items are evenly distributed in the line with equal space around them. Note that visually the spaces aren’t equal, since all the items have equal space on both sides. The first item will have one unit of space away from the container edge, but two units of space between the next item because that next item has its own spacing that applies.
Flex items are distributed so that the spacing between any two items (and the space to the edges) is equal
align-items defines the alignment along the cross axis. It has five main properties:
align-items: stretch; (default)
Stretch to fill the container (still respect min-width/max-width)
Flex items are placed at the start of the cross axis.
Flex items are placed at the end of the cross axis
Flex items are centered in the cross-axis
Flex items are aligned such as their baselines align.
It’s like justify-content but for multi-lines and for the cross axis. This property only works when the container has
flex-wrap: wrap; or
flex-wrap: wrap-reverse; It has eight main properties:
align-content: normal; (default)
Flex items are packed in their default position as if no value was set.
Flex items packed to the start of the container.
Flex items packed to the end of the container.
Flex items centered in the container.
Flex items evenly distributed. The first line is at the start of the container while the last one is at the end.
Flex items evenly distributed with equal space around each line.
Flex items are evenly distributed with equal space around them.
Flex lines stretch to take up the remaining space.
Flexbox Children Properties
Now let’s talk about flexbox item (children) properties.
The order property controls the order in which the flexbox items appear in the flex container. By default, flex items are laid out in the source order.
order: 2; /* default is 0 */
flex-grow defines the ability for a flex item to grow if necessary. It dictates what amount of the available space inside the flex container the item should take up.
With flex grow:
If all items have
flex-grow set to
1, the remaining space in the container will be distributed equally to all children. If one of the children has a value of
2, the remaining space will take up twice as much space as the others (or it will try to, at least).
flex-shrink defines the ability for a flex item to shrink if necessary.
flex-basis defines the default size of an element before the remaining space is distributed. It can be a length (e.g. 20%, 5rem, etc.) or a keyword.
flex is the shorthand for
flex-basis combined. The second and third parameters (flex-shrink and flex-basis) are optional. The default is
0 1 auto.
flex: 0 1 auto;
flex: 2 1 500px;
align-self overrides the value of
align-items for particular flex item. align-self share the same values as align-items.
Ready to become a Flexbox hero?
Now, you know everything you need to know about the flexbox. Let's do some practice.
Learn Flexbox by playing coding game – Flex Box Adventure.