Introduction To Flexbox

Flexbox is a new and revolutionary way to manage blocks. Conventionally, blocks are managed via defining display: block for vertical blocks and display: inline or display: inline-block for horizontally placing blocks. By block, we mean by element here.

Flexbox provides us an efficient way to control how the elements are sized, aligned (vertically or horizontally), change their order of appearance, change the direction in which all elements are laid out within the container.

The Flex structure layout is based on dividing elements into containers (parent elements) and items (child elements).

Note: By default, items are aligned in Row from Left to Right.


The flex-direction property changes the position and direction of the items. It has four possible values:

flex-direction: column – the direction of items is from top to bottom.

flex-direction: column-reverse – the direction of items is from bottom to top.

flex-direction: row (default) – the direction of flex items is from left to right.

flex-direction: row-reverse – the direction of flex items is from right to left.


The flex-wrap property defines how the items inside the containers should wrap when it has too many items. The possible flex-wrap values are nowrapwrap, and wrap-reverse.

flex-wrap: wrap – allows items to wrap inside the container.

flex-wrap: nowrap (default) – the items don’t wrap inside the containers. Instead, they overflow the container.

flex-wrap: wrap-reverse – the items wrap in the reverse direction, from bottom to top.


We can specify flex-direction and flex-wrap in one declaration through the flex-flow property. The default value of flex-flow is the combination of default values of the two properties. i.e.:

flex-flow: row nowrap;


This Flex property determines how space is allotted between items inside the container.

justify-content: flex-start (default) – aligns items against the start of the flex container.

justify-content: flex-end – aligns items against the end of the flex container.

justify-content: center – aligns the flex items to the center of the flex container.

justify-content: space-between – puts an even amount of space between flex items, but does not add space on the ends. The first item sticks to the start of the line. The last item sticks to the end of the line.

justify-content: space-around – distributes items evenly, with equal spaces around each of them.


We can align the items inside the container using the align-items property. This property aligns the elements along the vertical axis.

Let us consider align-items property as a vertical version of justify-content. While justify-content goes on the x-axis the align-items go on the y-axis.

align-items: flex-start – aligns the items towards the top of the flex container.

align-items: flex-end – aligns the items at the bottom of the flex container.

align-items: center – places items in the middle of the flex container.

align-items: stretch (default) – each item will fill the space from top to bottom if minimum or definite height is not set.

align-items: baseline – aligns items along their baseline of content (the line upon which most letters sit). Baseline depends on the tallest item in a line.


The align-content property is similar to align-items, but it aligns lines of items within a container vertically.

Note: If there is only one line of items in a container, this property won’t work.

align-content: flex-start – distributes lines towards the start of the container.

align-content: flex-end – distributes lines towards the end of the container.

align-content: center – distributes lines at the center of the container.

align-content: stretch (default) – stretches the lines to use all available space of the container.

align-content: space-between – distribute lines in a flex container such that there is an equal amount of space between each line.

align-content: space-around – distribute lines in a flex container such that there is an equal amount of space around each line.


By default, flex items in the container appear in the same order in which they are coded. But we can change this sequence using the order property.

We can assign a negative or positive number as its value. According to these numbers, the items will be re-ordered. The lowest numbers go first and the highest numbers go last.


The flex-grow property defines how much a flex item expands with respect to other items (if space available). We set its value using positive numbers. The number represents a proportion.

For example, if we assign flex-grow: 2 to a flex item, it will expand twice as big compared to the rest of the items in the same flex container.


The flex-shrink property defines the proportion in which a flex item shrinks more than other items in that container (when there is not enough space to keep the original size).


The align-self property overrides the values of align-items to align a single item.

In the following example, item #2 has the align-self: flex-start property, #3 has the align-self: center property and item #4 has the align-self: flex-end property rest items have default property.