Sub topic 12
How flexbox and grid already follow the flow
This chapter assumes you know the block and inline axes, how writing-mode and direction set them, and the logical margin, padding and inset properties from earlier chapters. You should also be comfortable with basic flexbox and grid syntax.
Flexbox and grid never needed logical properties added to them. They were designed after writing modes were understood, so they use flow-relative terms throughout. Knowing exactly how they map to the axes helps you avoid two mistakes: rewriting layouts that already adapt, and trusting a few physical values that do not adapt.
Flexbox: main and cross axes follow the writing mode
A flex container has a main axis and a cross axis. flex-direction chooses which logical axis the main axis uses.
rowandrow-reverseput the main axis along the inline axis.columnandcolumn-reverseput it along the block axis.- The cross axis is whichever axis the main axis is not on.
The words "row" and "column" are not physical. A row is a line of items along the inline direction. That runs left to right in English, right to left in Arabic, and top to bottom in Japanese with writing-mode: vertical-rl.
| Writing mode and direction | `row` main axis runs | `column` main axis runs |
|---|---|---|
| `horizontal-tb`, `ltr` | left to right | top to bottom |
| `horizontal-tb`, `rtl` | right to left | top to bottom |
| `vertical-rl`, `ltr` | top to bottom | right to left |
| `vertical-lr`, `ltr` | top to bottom | left to right |
| `vertical-rl`, `rtl` | bottom to top | right to left |
The diagram shows row selecting the inline axis. With column, the roles swap: the block axis becomes the main axis and the inline axis becomes the cross axis.
Alignment keywords are flow-relative
The alignment properties use start and end of the relevant axis, not left and right.
justify-contentaligns items on the main axis.flex-startmeans the main-start edge.align-itemsandalign-contentwork on the cross axis.flex-startmeans the cross-start edge.align-selfoverridesalign-itemsfor one item.
Here is a toolbar that needs no changes for right-to-left languages:
<!doctype html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Toolbar</title>
<style>
.toolbar {
display: flex;
justify-content: flex-start;
align-items: center;
gap: 0.5rem;
padding-inline: 1rem;
border-block-end: 1px solid gray;
}
.toolbar .spacer {
margin-inline-start: auto;
}
</style>
</head>
<body>
<div class="toolbar">
<button>Back</button>
<button>Forward</button>
<span class="spacer"></span>
<button>Settings</button>
</div>
</body>
</html>In ltr, Back sits at the left and Settings is pushed to the right. Change dir="ltr" to dir="rtl" and the whole toolbar mirrors, with no CSS changes. Back moves to the right edge and Settings to the left.
The spacer uses margin-inline-start: auto. An auto margin absorbs the free space on that side of the item. Using the logical form means it absorbs space on the correct side in either direction. margin-left: auto would push the wrong way in RTL.
The physical exceptions in flexbox
A few values in alignment properties stay physical because the specification keeps them for authors who want that:
justify-content: leftandjustify-content: rightare physical. They do not mirror.text-align: leftinside a flex item is physical in the same way.
Use start, end, flex-start and flex-end unless you really mean the left or right of the screen.
Reversal and order do not change the DOM
row-reverse, column-reverse and order only change the visual placement. The main-start edge swaps with main-end. The following also stay unchanged:
- DOM order
- Screen reader order
- Tab order
Keyboard focus can then jump around the screen in a confusing way. Reverse visually only when the content order is still meaningful, for example a button pair whose emphasis differs between cultures.
Grid: columns run along the inline axis
Grid works the same way with different names. The inline axis holds the columns and the block axis holds the rows.
grid-template-columnssizes tracks along the inline axis.grid-template-rowssizes tracks along the block axis.- Line numbers count from the start edge.
grid-column: 1 / 3starts at the inline-start edge, whichever side that is. grid-template-areasis drawn in reading order. In an RTL document, the first name in each string is on the right.column-gapandrow-gapapply to the inline and block axes. In vertical writing modes the "column" gap is therefore vertical.
A page shell with a sidebar:
.shell {
display: grid;
grid-template-columns: 16rem 1fr;
grid-template-areas:
"nav main";
min-block-size: 100vb;
gap: 1.5rem;
}
.shell nav {
grid-area: nav;
padding-inline-end: 1rem;
border-inline-end: 1px solid lightgray;
}
.shell main {
grid-area: main;
}In English, the sidebar is on the left and the border is on its right, facing the content. With dir="rtl" the sidebar is on the right and the border is on its left. The two logical properties and the grid mirror together.
The grid alignment properties follow the same axis rule, which differs from flexbox because there is no single main axis:
| Property | Axis | Applies to |
|---|---|---|
| `justify-items`, `justify-self` | inline | each item inside its grid area |
| `align-items`, `align-self` | block | each item inside its grid area |
| `justify-content` | inline | the whole set of tracks |
| `align-content` | block | the whole set of tracks |
A common source of confusion: in grid, justify-* is always inline and align-* is always block. In flexbox, justify-content is main-axis and align-* is cross-axis, so it flips when you switch to flex-direction: column.
Vertical writing modes
Vertical text shows how consistent the model is. In the following rule, the container's row runs top to bottom, and align-items: flex-start pins items to the cross-start edge. In vertical-rl, that edge is the right side.
.vertical-bar {
writing-mode: vertical-rl;
display: flex;
flex-direction: row;
justify-content: flex-start;
align-items: flex-start;
gap: 0.75rem;
block-size: 100%;
}You did not write "right" or "top" anywhere, and the layout is still correct.
What does not follow the flow
Flexbox and grid solve placement and alignment. Several other features still use physical directions, and you need to handle them yourself:
background-position: left topandbackground-position-x. Newer logical keywords exist for some of these, but support varies, so test your targets.linear-gradient(to right, ...). The gradient direction is physical.transform: translateX(...)androtate(). They move along the screen's x axis.box-shadowandtext-shadowoffsets. The x offset is physical.- Icons such as arrows that imply direction. Mirror them with
:dir(rtl)or a[dir="rtl"]selector when the meaning is directional, as with "back" and "next" arrows. Do not mirror them when they depict physical objects, such as a clock.
.chevron {
transform: none;
}
.chevron:dir(rtl) {
transform: scaleX(-1);
}The :dir() pseudo-class matches the element's computed direction. If you need to support browsers without it, use [dir="rtl"] .chevron instead.
Common errors
- Adding
margin-leftto a flex item and forgetting that the container mirrors. Usemargin-inline-start. - Using
justify-content: rightto mean "end". Useflex-endorend. - Assuming
column-gapis always horizontal. It follows the inline axis. - Using
orderor*-reverseto fix a layout, and breaking tab order. - Mixing a mirrored layout with an unmirrored gradient or shadow, so one edge looks wrong in RTL.
Exercise
A flex container has display: flex; flex-direction: row; justify-content: flex-start; and sits in an element with dir="rtl" and writing-mode: horizontal-tb. On which side of the container do the items gather, and what changes if you set writing-mode: vertical-rl with dir="ltr"?
Show answer
In the RTL horizontal case the main axis is the inline axis running right to left, so flex-start is the right edge and the items gather on the right. With vertical-rl and ltr, the inline axis runs top to bottom, so the items gather at the top.
Exercise
You write .promo { display: flex; justify-content: right; } and the promo does not mirror in an Arabic page. Why, and what is the fix?
Show answer
right is a physical value for justify-content, so it always means the right of the screen. Replace it with flex-end (or end), which refers to the main-end edge and mirrors with the direction.