liveBook · Manning

Main axis

As explained in Web Design Playground

The primary flexbox axis running top to bottom along which flex items are arranged when the container is vertical.

Picture a flex container as a path for its child elements, called flex items. The main axis runs in the direction those items travel. Set `display: flex` on the container, then use `flex-direction` to choose the path. The cross axis runs perpendicular to it. section 12.2

The direction changes which way the main axis points:

  • `row` runs horizontally from left to right and is the default; `row-reverse` runs from right to left. section 12.2
  • `column` runs vertically from top to bottom; `column-reverse` runs in the opposite direction. section 12.2
body {
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    max-width: 50em;
    min-height: 100vh;
}

Here, the `body` is a flex container. `flex-direction: column` gives it a vertical main axis, and `justify-content: flex-start` places its items at the start of that axis—the top of the page. section 12.11

Why choose a direction? A page can stack its major parts along a vertical main axis while using horizontal flex containers for a header, navigation bar, and content columns. A photo-gallery layout likewise uses a single-column `body` to start its items at the top. Try changing a container between `row` and `column`: which way do its items run now? section 12.11 section 15.11

How Web Design Playground covers it

How deep it goes
Defines the main axis against the cross axis and applies vertical direction and start alignment in page layouts.
Examples
Uses a holy-grail page layout and a photo gallery with vertically stacked body content.
What it assumes
Introduces flex containers with display: flex and explains both axes without assuming prior knowledge of those terms.
What you can do after
Apply it via CSS flex-direction: column and justify-content: flex-start to stack page elements from the top.
How it connects
Connects flex-direction, flex containers, the cross axis, 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 Web Design Playground. The section links point to the parts of the book it draws on.