Web - Alignment and Distribution
Code Life
By Pierre Marsaa
Understanding Flexbox Axes
Before we learn how to align items in Flexbox, we need to understand the two axes that Flexbox operates on: the main axis and the cross axis.
By default (when flex-direction is row):
- The main axis runs horizontally from left to right
- The cross axis runs vertically from top to bottom
Flexbox main axis and cross axis
When you change the flex-direction (to column, for example), the axes switch:
- With flex-direction: column, the main axis runs vertically
- The cross axis then runs horizontally.
Here is a code example to illustrate this.
↗️
Loading editor...
↶
↷
Understanding which axis is which is crucial because:
- The justify-content property aligns items along the main axis
- The align-items property aligns items along the cross axis
Verify