liveBook · Manning

Main axis

As explained in CSS in Depth, Second Edition

The main axis is the primary direction in which a flex container lays out and distributes its flex items.

The main axis is the primary direction in which a flex container places and distributes its flex items. In a normal row, it runs from main start on the left to main end on the right; the cross axis runs perpendicular to it. section 4.1

Figure 4.1 A flexbox container and its elements
figure

Use the diagram to distinguish the flex container, flex items, main axis, cross axis, and their start and end points. section 4.1

The main axis determines how several flexbox rules affect a layout:

  • Changing flex-direction changes the main-axis direction. A column or column-reverse layout rotates it so items flow vertically from top to bottom or in reverse. section 4.3.1
  • flex-basis, flex-grow, and flex-shrink apply along the main axis. In a row they affect width; in a column they affect height. section 4.3.1
  • justify-content distributes unused space along the main axis, using values such as flex-start, flex-end, center, space-between, space-around, and space-evenly. section 4.4.1
  • flex-wrap creates additional lines along the main-axis flow; in a constrained column, wrapped items can overflow into a new column. section 4.4.1

A vertical nested flexbox

A sidebar can become a column flex container with flex-direction: column. Its main axis then runs vertically, so assigning flex: 1 to its tiles lets their heights stretch and align the tile bottoms when the outer layout provides a constrained height. section 4.3.1

A vertical flex container still grows naturally to fit its contents. Therefore, flex-grow and flex-shrink affect item heights only when another rule or container supplies a specific height. section 4.3.1

How CSS in Depth, Second Edition covers it

How deep it goes
Defines the axis, follows its rotation through flex-direction, and explains sizing, spacing, wrapping, and height constraints.
Examples
Uses navigation, tiles, a nested sidebar column, axis diagrams, and spacing and wrapping property examples.
What it assumes
Assumes basic CSS layout knowledge while introducing flex containers, flex items, and the two axes as foundational concepts.
What you can do after
Apply via CSS Flexbox to choose flow direction, predict sizing dimensions, distribute space, and diagnose constrained column layouts.
How it connects
Connects the main axis to flex containers, flex items, the cross axis, flex-direction, flex sizing, flex-wrap, and justify-content.

Also explained in

Compare how 3 books explain main axis →

Related concepts

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