Sub topic 12
How flexbox and grid already follow the flow
This chapter assumes you know the block and inline axes, and that dir and writing-mode decide which physical side each one points to. It also assumes you know basic flexbox and grid syntax.
Flex and grid use flow-relative terms
Flexbox and grid were designed after writing modes existed, so most of their vocabulary is already flow-relative. You don't need to convert it. You only need to know which axis each term refers to.
In flexbox, flex-direction: row means "run along the inline axis". It does not mean "run left to right". The main axis starts at the inline-start side, so in a right-to-left document a row flex container places its first item on the right.
<div class="toolbar" dir="ltr"><button>One</button><button>Two</button><button>Three</button></div>
<div class="toolbar" dir="rtl"><button>One</button><button>Two</button><button>Three</button></div>
<style>
.toolbar {
display: flex;
gap: 0.5rem;
justify-content: flex-start;
padding-inline: 1rem;
}
</style>Here flex-start is the start of the main axis. In the first toolbar, "One" is on the left. In the second, it is on the right. The same rule works in both, and no override is needed.
Grid works the same way
Grid lines and tracks follow the axes too. Columns run along the inline axis and rows along the block axis. In a right-to-left document, the first column is on the right.
.layout {
display: grid;
grid-template-columns: 12rem 1fr;
gap: 1rem;
}
.sidebar {
grid-column: 1;
}The sidebar stays in the first column. That column sits at the inline-start side, so it is on the left in English and on the right in Arabic.
In a vertical writing mode, "columns" are the tracks that run along the inline axis, so they stack from top to bottom. The names stay the same and the physical result changes.
Alignment properties
The alignment properties split by axis, not by screen direction.
| Container | Inline axis | Block axis |
|---|---|---|
| Grid | `justify-content`, `justify-items`, `justify-self` | `align-content`, `align-items`, `align-self` |
| Flex, `row` | `justify-content` | `align-items`, `align-content` |
| Flex, `column` | `align-items` | `justify-content` |
In flexbox, justify-* always follows the main axis. That axis is the inline axis for row and the block axis for column.
Where physical terms still appear
A few keywords are physical and don't flip. justify-content: left and right always mean the physical sides. row-reverse reverses relative to the current start, so it flips in right-to-left text. Spacing inside a flex or grid layout still needs logical properties. A margin-left on a flex item will be on the wrong side in right-to-left text, even though the container flows correctly.
Exercise
A container has display: flex; justify-content: flex-start; and is inside a document with dir="rtl". On which physical side do its items gather, and why?
Show answer
On the right. flex-start is the start of the main axis. With the default row direction, the main axis is the inline axis, and right-to-left text starts at the right.