Writing Modes and Logical Properties
How writing-mode and direction change text and block flow, and how logical properties like margin-inline follow that flow instead of fixed screen sides.
Writing Modes and Logical Properties
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Text flows along the inline axis, and blocks stack along the block axis. In English, inline is left to right and block is top to bottom. `writing-mode` and `direction` change that.
- Logical properties name sides by flow, not by screen: `margin-inline-start` is the left margin in English, the right margin in Arabic, and the top margin in vertical Japanese.
- The mapping: `width`/`height` become `inline-size`/`block-size`, `margin-left`/`margin-top` become `margin-inline-start`/`margin-block-start`, and `top`/`left` become `inset-block-start`/`inset-inline-start`.
- Shorthands such as `margin-inline: auto`, `padding-block: 1rem` and `inset: 0` cover both ends of an axis in one declaration.
- Flexbox and Grid already follow the writing mode: a flex row runs along the inline axis, so it flips in right-to-left languages with no extra CSS.
Left is not a universal word
Every property you have used so far hides an assumption: that text starts at the left and runs right, and that new lines go below. Arabic and Hebrew start at the right. Traditional Chinese, Japanese and Korean can run in vertical columns. A site with margin-left and padding-right hard-coded is correct for exactly one of those families of language, and translating the content leaves the spacing wrong.
CSS fixes this by splitting the page into two axes defined by the text, and letting you refer to them instead of to screen edges.
The two axes
- The inline axis is the direction text runs inside a line.
- The block axis is the direction lines (and block boxes) stack.
In English they are horizontal and vertical. In Arabic the inline axis is horizontal and reversed. In vertical Japanese the inline axis is vertical, and the block axis runs horizontally, right to left.
Two properties set them:
.vertical { writing-mode: vertical-rl; } /* lines run top to bottom, stack right to left */
.rtl { direction: rtl; } /* inline axis reversed */
writing-mode accepts horizontal-tb (the default), vertical-rl and vertical-lr. Text direction in HTML should normally come from the dir attribute, not the CSS direction property, because it carries meaning for the document, not only appearance. In a vertical mode, text-orientation decides whether Latin letters stand upright or are rotated sideways.
Logical properties
Each physical property has a logical twin:
| Physical | Logical |
|---|---|
width, height |
inline-size, block-size |
margin-left, margin-right |
margin-inline-start, margin-inline-end |
margin-top, margin-bottom |
margin-block-start, margin-block-end |
padding-*, border-* |
the same pattern: padding-inline, border-block-end, … |
top, right, bottom, left |
inset-block-start, inset-inline-end, … |
text-align: left |
text-align: start |
float: left |
float: inline-start |
“Start” and “end” are in flow order. I tested this in Chromium. In a default horizontal box, inline-size: 300px made the width 300px and margin-inline-start: 11px the left margin. In an element with dir="rtl", margin-inline-start: 11px showed up as a right margin. In a writing-mode: vertical-rl box, inline-size: 300px made the box 300px tall and block-size: 100px made it 100px wide; margin-inline-start: 11px landed on the top, and margin-block-start: 22px on the right.
Shorthands that save typing
.card {
padding-block: 1rem; /* start and end of the block axis */
padding-inline: 1.5rem; /* start and end of the inline axis */
margin-inline: auto; /* centre, in any writing mode */
inset: 0; /* all four sides of a positioned box */
}
margin-inline: auto with a max-inline-size is the modern way to centre a column. inset is the shorthand for the four offset properties (top, right, bottom, left), and has been supported in Chrome 87, Firefox 66 and Safari 14.1 together with the other logical margin shorthands.
Layout systems already do it
Flexbox’s main and cross axes follow the writing mode, so flex-direction: row runs along the inline axis, and a flex row of nav items reverses in a right-to-left document without any extra CSS. Grid works the same way. The old tricks of mirroring a layout by hand are only needed for physical properties.
When physical values are right
A logical property mirrors with the language. Some things should not: a notification toast fixed to the bottom-right corner of the screen, a box shadow that imitates light coming from the top left. Those describe the screen, not the text, so right and top stay correct. The test is whether the element should flip when the page flips.
Adopting it gradually
You don’t need to rewrite a stylesheet. Use logical properties in new code and replace physical ones when you touch a component. The size ones, inline-size and block-size, are the least urgent, since they only differ from width and height in vertical modes. The ones that matter most for a multilingual site are margin, padding, border, text-align and inset on the inline axis.
What the video covers
Kevin Powell’s “Write better CSS using logical properties” is about 21 minutes and is from July 2021. Its chapters: writing direction and languages (1:22), text-align (6:28), margin, border and padding with logical properties (7:26), floats (11:58), size (13:33), using margin-inline to avoid problems (14:45), and inset (17:13). That covers the same ground as the table above. The Chromium measurements are my own tests; I could not read a transcript. The runner-up from CSS Weekly (2023) also treats vertical and right-to-left writing modes.
Where this fits
This ends the typography group: you can now choose and load fonts, set them readably, size them fluidly, wrap them, and flow them in any language. The colour and effects group is next, starting with colours and colour spaces. The logical properties here also underlie Flexbox and Grid, and the box-size names return in the box model.
Check yourself
A button has `margin-left: 1rem` to space it from an icon. What goes wrong when the page is translated to Arabic (right to left), and what is the fix?
The icon moves to the other side of the button, but the margin stays on the left, so the space is now on the wrong side of the icon. `margin-inline-start: 1rem` refers to the start of the text flow, which is the left in English and the right in Arabic, so it follows.
With `writing-mode: vertical-rl`, which physical sides do `margin-inline-start` and `margin-block-start` map to?
`margin-inline-start` is the top and `margin-block-start` is the right, because text runs top to bottom and lines stack right to left. I measured margin-top 11px and margin-right 22px for 11px and 22px values of those two properties.
What is `inline-size` for a box with `writing-mode: vertical-rl`, in physical terms?
The height. The inline axis is vertical in that mode, so `inline-size: 300px` gives a box 300px tall, and `block-size` gives its width. In my test the box measured 100 wide and 300 tall.
Which physical-direction CSS is still reasonable to write even when you use logical properties elsewhere?
Things tied to the physical screen rather than to text: a toast pinned to the bottom right of the viewport, or a drop shadow offset that mimics a fixed light source. For anything that should mirror with the language, use logical properties.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.