CSS Flexbox: Mastering the Main and Cross Axis

Flexbox treats layout as a one-dimensional problem: either a row or a column. All alignment rules operate along a 'main axis' and a perpendicular 'cross axis'. Use it for navbars or card components.
Why it exists
Before Flexbox, creating simple, robust layouts like vertically centering an element or distributing items evenly was notoriously difficult, often requiring CSS hacks. Flexbox was created to provide a predictable, one-dimensional model for aligning and distributing space among items in a container.
The mental model
Think of a Flexbox container as a single track, either horizontal or vertical. You place items on this track and then tell the container how to space them out and align them. This track has two directions: the main axis (the direction of the track itself) and the cross axis (perpendicular to the track). All alignment properties are relative to these two axes, not to physical top/bottom/left/right.
How it works
You create a flex container by setting display: flex on an element. The primary property is flex-direction, which sets the main axis. It has four values: row (default, left-to-right), row-reverse (right-to-left), column (top-to-bottom), and column-reverse (bottom-to-top). The cross axis automatically runs perpendicular to your choice. For example, with flex-direction: row, the main axis is horizontal and the cross axis is vertical. With flex-direction: column, the main axis becomes vertical and the cross axis becomes horizontal. Properties like justify-content align items along the main axis, while align-items aligns them along the cross axis.
When to use it
Use Flexbox for components that need to be laid out in a single dimension. It excels at navigation bars, lists of items (like products in a row), aligning form inputs and buttons, and centering content within a container. It's the workhorse for component-level layout.
When not to use it
Avoid Flexbox for complex, two-dimensional page layouts that require simultaneous control over both rows and columns. For that, CSS Grid is the superior tool. While Flexbox can wrap items to create multiple lines, it's less explicit and harder to manage for a full page grid than a true grid system.
One canonical example
To create a navigation bar where items are spaced evenly, you would set the container to display: flex, flex-direction: row, and justify-content: space-between. This establishes a horizontal main axis and tells the items to distribute themselves with the first item at the start, the last at the end, and even spacing between all items.
Interview question
When a Flexbox container has flex-direction: column, which statement correctly describes its main and cross axes?
- a.Both the main and cross axes are horizontal.
- b.The main axis is horizontal, and the cross axis is vertical.
- c.Both the main and cross axes are vertical.
- d.The main axis is vertical, and the cross axis is horizontal.Correct
Why? this is the answer
The card states that flex-direction sets the main axis, and if flex-direction is column, the main axis becomes vertical, with the cross axis automatically perpendicular, hence horizontal. Option B describes the axis orientation for flex-direction: row, which is a common point of confusion.
Just read this? Test yourself on what you have been reading.
Read the original → developer.mozilla.org
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on css — each one lists the topics its interview covers.
See open roles