liveBook · Manning

Cross axis

As explained in Web Design Playground, Second Edition

The cross axis is the direction perpendicular to the main axis in a flex container.

Flexbox containers always have two directions to keep track of. The main axis runs with the flex items; the cross axis runs perpendicular to it. Think of them as two roads crossing: items travel along the main road, while cross-axis alignment decides where they sit across it. section 12.2

The cross axis is not permanently vertical or horizontal. It changes when you set flex-direction. With the default row direction, the main axis is horizontal and the cross axis is vertical. With column, the main axis is vertical and the cross axis becomes horizontal. Sneaky name, useful idea. section 12.2

Ask two separate questions when arranging flex items:

  • Where should items go along the main axis? Use justify-content. section 12.2
  • Where should items sit across the cross axis? Use align-items. section 12.2
  • If items wrap onto several lines, how should those lines sit across the cross axis? Use align-content. section 12.2
Figure 12.5 The align-items values in action

Compare the align-items values to see how flex items move across the cross axis: stretch, start, end, center, or baseline. section 12.2

Center a header logo and title vertically

A header with the default row direction places its image and heading along a horizontal main axis. That makes the cross axis vertical, so align-items: center centers the logo and title vertically within the header. Meanwhile, justify-content: flex-start keeps them at the start of the horizontal main axis. section 12.6

This distinction matters in larger layouts, too. A page body can use flex-direction: column, giving it a vertical main axis, while a nested header can keep the default horizontal main axis. Each flex container gets its own pair of axes, so check the container before deciding what “across” means. section 12.2 section 12.6

How Web Design Playground, Second Edition covers it

How deep it goes
Defines the perpendicular axis, relates it to flex-direction, and demonstrates alignment in a header layout.
Examples
Demonstrates a header logo and title centered on the vertical cross axis within a horizontal flex container.
What it assumes
Teaches the axis distinction from zero after converting an element into a flex container with display: flex.
What you can do after
Apply it via CSS Flexbox properties to align items across a container’s main direction.
How it connects
Connects the cross axis to Flexbox, flex-direction, justify-content, align-items, align-content, and flex-wrap.

Also explained in

Compare how 3 books explain cross axis →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Web Design Playground, Second Edition. The section links point to the parts of the book it draws on.