← Back to all tutorials

Axis

Deep dive into the main axis and cross axis — how flex-direction changes them and how alignment works on each.

Axis

Understanding the main axis and cross axis is the key to mastering Flexbox alignment. Every Flexbox property works along one of these two axes.

The Two Axes

AxisDefault DirectionControlled By
Main AxisHorizontal (left → right)justify-content
Cross AxisVertical (top → bottom)align-items

The cross axis is always perpendicular to the main axis.

How flex-direction Changes the Axes

flex-directionMain AxisCross Axis
row (default)Horizontal →Vertical ↓
row-reverseHorizontal ←Vertical ↓
columnVertical ↓Horizontal →
column-reverseVertical ↑Horizontal →

Visual: Row Direction

flex-direction: row (default)

Main Axis →   →   →   →   →
┌────────────────────────────────┐
│  ┌──────┐  ┌──────┐  ┌──────┐ │  ↓ Cross
│  │ Item │  │ Item │  │ Item │ │  ↓ Axis
│  │  1   │  │  2   │  │  3   │ │  ↓
│  └──────┘  └──────┘  └──────┘ │
└────────────────────────────────┘

Visual: Column Direction

flex-direction: column

Cross Axis →   →   →
┌──────────────────┐
│  ┌────────────┐  │  ↓
│  │   Item 1   │  │  ↓ Main
│  └────────────┘  │  ↓ Axis
│  ┌────────────┐  │  ↓
│  │   Item 2   │  │  ↓
│  └────────────┘  │  ↓
│  ┌────────────┐  │  ↓
│  │   Item 3   │  │  ↓
│  └────────────┘  │
└──────────────────┘

justify-content — Main Axis

.container {
    display: flex;
    justify-content: center;  /* Centers items along the MAIN axis */
}

/* In row: centers horizontally */
/* In column: centers vertically */

align-items — Cross Axis

.container {
    display: flex;
    align-items: center;  /* Centers items along the CROSS axis */
}

/* In row: centers vertically */
/* In column: centers horizontally */

Perfect Centering

.container {
    display: flex;
    justify-content: center;  /* Main axis: center */
    align-items: center;      /* Cross axis: center */
    height: 100vh;
}

/* Items are perfectly centered both horizontally and vertically!
   This is the simplest centering technique in CSS. */

The Mental Model

When you're confused about which property does what, ask these two questions:

  1. Which direction do items flow? → That's the main axis
  2. Which direction is perpendicular? → That's the cross axis

Then:

  • justify-content = main axis alignment
  • align-items = cross axis alignment

Common Confusion

DirectionHorizontal CenteringVertical Centering
flex-direction: rowjustify-content: centeralign-items: center
flex-direction: columnalign-items: centerjustify-content: center

The properties swap roles when you switch from row to column, because the axes swap!

Key Takeaways

  • Flexbox has two axes: main (item flow direction) and cross (perpendicular)
  • flex-direction determines which axis is main and which is cross
  • justify-content aligns along the main axis
  • align-items aligns along the cross axis
  • In row: justify = horizontal, align = vertical
  • In column: justify = vertical, align = horizontal
  • Both center + center = perfect centering