Skip to content

Alignment, Justification, and axes

Like Grid, Flexbox allow us to modify the alignment and justification of their direct children.

There is a lot of similarity between how they work, with enough differences to confuse you when you’re switching contexts between one and the other.

Just like with Grid, we have alignment and justification with Flexbox. The big difference is, they don’t work based on the two axes of the flex container.

  • The main-axis
  • The cross-axis

By default, the main-axis is horizontal, and the cross-axis is vertical.

We use justification for the main axis. By default, this is start, which essentially means “line up at the ‘start’ of the main axis.”

In the example below, we can see how all the elements have lined up at the “start” of the horizontal axis, leaving empty space within the parent.

The default alignment on the cross-axis is called stretch, which means that all elements stretch to match the height of the parent. We use the alignment properties on the cross-axis.

Most of the time, you won’t have a declared height, so the tallest sibling defines the height, and the others “stretch” to match that height. You can select one of the elements and give it a larger height, or add more content to one, and you will see the siblings stretch in height to match.

We can change how the elements are aligned on the main axis using justify-content, and on the cross axis using align-items.

Both of them work similarly to how their Grid equivilants work, but unlike with Grid, the most common ones you’ll use are:

  • justify-content: space-between
  • justify-content: center
  • align-items: center

With Grid, we rarely use justify-content because our columns usually fill up the entire width of the grid container. With Flexbox, a lot of the time there is leftover space inside the container, and we want to distribute that space.

Where things get confusing is when we change the direction of the main-axis using flex-direction, where we have two values that we can pick:

  • row: the default value
  • column: changes the direction of the main axis to be a column

See the Pen Flexbox visualizer by Kevin (@kevinpowell) on CodePen.

If you look at how Flexbox works, the concept of justify-items doesn’t work, since the elements are controlling their own size. With Grid, the cell can be bigger than the element on both the vertical and horizontal axis at the same time. With Flexbox, that can only happen on the cross-axis.