Sub topic 2
Main axis and cross axis
Every flexbox property you use for alignment or sizing refers to one of two axes. This chapter explains what those axes are, how flex-direction and the writing mode set them, and how that explains the behavior of justify-content and align-items. It assumes you know basic HTML, CSS selectors, the box model, and how to create a flex container with display: flex.
Two axes, not horizontal and vertical
A flex container lays out its items along one line at a time. That line runs along the main axis. The cross axis is perpendicular to the main axis.
Flexbox does not use the words "horizontal" and "vertical" in its property names. It uses axes, and the axes can point either way. With the default flex-direction: row, the main axis is horizontal and the cross axis is vertical. With flex-direction: column, they swap.
Each axis has a start and an end. In a left-to-right language with row, main start is the left edge and main end is the right edge. Cross start is the top and cross end is the bottom.
How flex-direction sets the axes
The flex-direction property on the container chooses the main axis and the direction items flow along it.
| Value | Main axis | Main start to end | Cross axis |
|---|---|---|---|
| `row` (default) | Inline direction (horizontal in English) | Left to right | Top to bottom |
| `row-reverse` | Inline direction | Right to left | Top to bottom |
| `column` | Block direction (vertical in English) | Top to bottom | Left to right |
| `column-reverse` | Block direction | Bottom to top | Left to right |
The reverse values do not change which line the main axis lies on. They flip the start and end. The cross axis is unaffected, except when flex-wrap: wrap-reverse is used, which flips the cross start and end.
Writing mode and direction
row follows the inline direction of the text, not a fixed physical direction. In a right-to-left language such as Arabic or Hebrew, row runs from right to left. In a vertical writing mode such as writing-mode: vertical-rl, row runs vertically.
This is why the specification uses "start" and "end" and not "left" and "right". If your site supports several languages, justify-content: flex-start follows the text direction without extra work.
Which property works on which axis
Two properties cause most of the confusion. The rule is short.
justify-contentdistributes items along the main axis.align-itemsaligns items along the cross axis, within each flex line.
The names suggest "justify is horizontal, align is vertical". That is only true for row. When you switch to column, justify-content acts vertically and align-items acts horizontally.
Here is a runnable page that shows both directions with the same alignment properties.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Axes demo</title>
<style>
.box {
display: flex;
justify-content: space-between;
align-items: center;
height: 120px;
width: 300px;
border: 2px solid #333;
margin-bottom: 1rem;
}
.box.column {
flex-direction: column;
}
.item {
background: #7aa7e0;
padding: 0.5rem;
}
</style>
</head>
<body>
<div class="box">
<div class="item">A</div>
<div class="item">B</div>
<div class="item">C</div>
</div>
<div class="box column">
<div class="item">A</div>
<div class="item">B</div>
<div class="item">C</div>
</div>
</body>
</html>In the first box, the items spread across the width (main axis) and sit in the vertical middle (cross axis). In the second box, the same two declarations produce a different result. The items spread down the 120px height and sit in the horizontal middle. The CSS for alignment is identical, and only flex-direction differs.
Which dimension each axis controls
Sizing also depends on the axes. flex-basis, flex-grow and flex-shrink all work on the main axis. They set the item's size in the main dimension:
- In a
rowcontainer, the main size is the item's width. - In a
columncontainer, the main size is the item's height.
The cross size is usually not flexible in the same way. It comes from the item's content or from an explicit width or height. By default, align-items: stretch makes each item fill the container's cross size. That is why items in a row all share the height of the tallest item.
A stretched item only fills the cross axis if its cross-size property is auto. If you set height: 50px on an item in a row, it will not stretch.
Free space and why it matters
The container has a certain amount of space along each axis. Flexbox alignment is about free space, which is the container's size minus the space the items already take.
justify-contentonly has an effect if there is free space on the main axis. If the items fill the line, there is nothing to distribute.align-itemspositions each item within its flex line on the cross axis. In a single-line container with a definite height, the line fills the container. In a container with an automatic height, the line is only as tall as the tallest item, soalign-items: centerhas nothing visible to do.
This explains a common complaint: "I set align-items: center and nothing moved." The cause is usually a container with no extra height. Give the container a height or a min-height, and the effect appears.
The same applies in a column. justify-content: center on a column container does nothing visible if the container's height is auto, because the container shrinks to fit its items and there is no free space.
Common errors
- Using
justify-contentfor vertical centering in a row. In arow, vertical centering isalign-items: center.justify-contentonly moves items horizontally. - Forgetting that column swaps the properties. After changing to
column, review eachjustify-contentandalign-itemsdeclaration. Their effects have swapped. - Expecting reversed order to change the axis.
row-reversechanges direction along the same line. It does not move the main axis to a different line. - Setting a fixed cross size and expecting stretch. An explicit
heighton an item in a row disables stretching for that item. - Testing only in English. In a right-to-left page,
rowflows from right to left. Layout that depends on a physical left or right will break there. Use start and end values.
Putting it together
For any flex container, answer three questions in order:
- What is
flex-direction? That gives the main axis and its start and end. - Is there free space on the main axis? If so,
justify-contentdecides where it goes. - How tall or wide is the flex line on the cross axis?
align-itemsdecides where each item sits in that space.
The next chapters build on this model. Direction, distribution, alignment, wrapping and sizing are all adjustments to one of the two axes.
Exercise
A container has display: flex; flex-direction: column; height: 400px; justify-content: center; align-items: flex-start;. Where do its items appear? Which property would you change to center them horizontally instead of left-aligning them?
Show answer
The main axis is vertical, so justify-content: center places the group of items in the vertical middle of the 400px height. The cross axis is horizontal, so align-items: flex-start aligns each item to the left edge (in a left-to-right language). To center the items horizontally, change align-items to center.
Exercise
You have a row-direction flex container with align-items: center, but the items stay at the top. The container has no height set. What is the cause, and how do you fix it?
Show answer
With an automatic height, the container is only as tall as its tallest item, so there is no free space on the cross axis and centering has no visible effect. Give the container a height or min-height, for example min-height: 200px, and the items will be centered vertically.