liveBook · Manning

Cross axis

As explained in CSS in Depth, Second Edition

The cross axis is the layout direction perpendicular to a flex container's main axis.

In a flex container, items are laid out along the main axis. The cross axis runs perpendicular to it. For a row flowing left to right, the cross axis runs from top to bottom. Change the flex direction to a column, and the cross axis runs from left to right instead. Identify the axes before choosing an alignment property: “vertical” does not always mean “cross-axis.” section 4.1 section 4.3.1

Figure 4.1 A flexbox container and its elements
figure

Use the container diagram to locate the main and cross axes before adjusting alignment. section 4.1

The cross axis determines what these alignment controls do:

  • align-items positions all items across the cross axis. Its initial value, stretch, fills the container’s height in a row layout or width in a column layout; center and the start/end values offer other placements. section 4.4.1
  • align-self overrides align-items for one item. Its initial value, auto, defers to the container’s setting. section 4.4.2
  • With flex-wrap enabled, align-content controls spacing between rows along the cross axis. It is not a substitute for align-items when aligning individual items. section 4.4.1

A sidebar that changes direction

Make a sidebar a nested flex container with flex-direction: column. Its tiles now run top to bottom on the main axis, while the cross axis runs left to right. In this layout, flex: 1 acts on tile height rather than width. Height still needs a constraint for flex growth to have an effect; here, that constraint comes from the outer flex layout. section 4.3.1

Listing 4.9 Creating a flex column on the right
.column-sidebar {             #1
  flex: 1;                   #1
  display: flex;             #1
  flex-direction: column;    #1
  gap: var(--gap-size);      #1
}

.column-sidebar > .tile {
  flex: 1;                  #2
}

The listing shows the sidebar’s column direction and the flex value applied to its tiles. section 4.3.1

How CSS in Depth, Second Edition covers it

How deep it goes
Defines the axis, follows its rotation in a column layout, and distinguishes item alignment from spacing wrapped rows.
Examples
Uses a flex-container diagram and a nested column sidebar whose tiles grow vertically.
What it assumes
Builds the axis model from flex containers and flex items while assuming familiarity with basic CSS page layout.
What you can do after
Operate/configure CSS flexbox alignment by choosing controls for items or wrapped rows after identifying the cross axis.
How it connects
Connects flex direction, the main axis, align-items, align-self, and align-content.

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 CSS in Depth, Second Edition. The section links point to the parts of the book it draws on.