What are flex box?

The flexbox, also known as the flexible box layout module, helps you efficiently design and build responsive webpages without using many positioning and float properties. aims at providing a more efficient way to lay out, align and distribute space among items in a container, even when their size is unknown and/or dynamic (thus the word “flex”).

The main idea behind the flex layout is to give the container the ability to alter its items’ width/height (and order) to best fill the available space (mostly to accommodate to all kind of display devices and screen sizes). A flex container expands items to fill available free space or shrinks them to prevent overflow.

Basic and terminology

Since flexbox is a whole module and not a single property, it involves a lot of things including its whole set of properties. Some of them are meant to be set on the container (parent element, known as “flex container”) whereas the others are meant to be set on the children (said “flex items”).

00-basic-terminology.svg

The main axis and the cross axis

The key to understanding flexbox is to understand the concept of a main axis and a cross axis. The main axis is the one set by your flex-direction property. If that is row your main axis is along the row, if it is column your main axis is along the column.

three-boxes-next-each-w-7b5d0fbdaf522.svg

Flex items move as a group on the main axis.

The cross axis runs in the other direction to the main axis, so if flex-direction is row the cross axis runs along the column.

three-boxes-different-he-20fd54d77efbb.svg

You can do two things on the cross axis. You can move the items individually or as a group, so they align against each other and the flex container. Also, if you have wrapped flex lines, you can treat those lines as a group to control how space is assigned to those lines.

Creating a flex container