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.

The main axis is flexbox’s travel direction: the line along which a container lays out its flex items. Think of it as choosing whether your items form a row across the page or a stack down the page. Every flex container also has a cross axis, which runs perpendicular to the main axis. section 12.2

A vertical main axis runs from top to bottom. Set `flex-direction: column` on a flex container to make its children stack vertically; use `column-reverse` to stack them from bottom to top. The main axis is not permanently vertical—`row` and `row-reverse` make it horizontal instead. section 12.2

To make a vertical flex layout, take these small steps:

  • Choose a parent element to be the flex container with `display: flex`; its child elements become flex items automatically. section 12.2
  • Set `flex-direction: column` so the main axis points downward. section 12.2
  • Use `justify-content` when you want to arrange items along that vertical main axis. section 12.2
  • Use `align-items` when you want to position items across the horizontal cross axis. section 12.2

Stacking a page’s major regions

A page body can become a single-column flex container: its header, navigation, content, and footer then stack on the vertical main axis. Starting content at the top uses `justify-content: flex-start`; a maximum width and minimum height can shape the overall page area. section 12.11 section 15.11

body {
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    max-width: 50em;
    min-height: 100vh;
}

This rule makes `body` a flex container with a vertical main axis. `justify-content: flex-start` places its items at the top of that axis. section 12.11

A handy check: before choosing alignment properties, ask which way the items run. With `column`, “along the main axis” means up and down; “along the cross axis” means side to side. That one choice makes the rest of the flexbox controls easier to read. section 12.2

How Web Design Playground covers it

How deep it goes
Defines both axes and maps `flex-direction` values to horizontal or vertical item flow.
Examples
Uses a vertically stacked `body` in Holy-Grail and photo-gallery page layouts.
What it assumes
Teaches the axis after introducing `display: flex`, whose child elements automatically become flex items.
What you can do after
Apply it via CSS Flexbox to stack page regions vertically and arrange them from the top.
How it connects
Connects `flex-direction` with `justify-content`, `align-items`, flex containers, flex items, and the cross axis.

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.