Sub topic 2
Writing mode and direction
This chapter covers the two CSS settings that decide how text and boxes flow: writing-mode and direction. I assume you know the box model, can write basic HTML, and have used width, height, margin and padding. Every logical property in later chapters depends on what these two settings say, so it is worth getting them right first.
Two questions a layout must answer
Any layout engine has to know two things before it can place content:
- Which way do lines of text run? Left to right, right to left, top to bottom, or bottom to top.
- Which way do new lines or blocks stack after the first one?
CSS answers the first with direction (and writing-mode) and the second with writing-mode. Physical properties like margin-left ignore both answers. Logical properties read them.
The writing-mode property
writing-mode sets whether lines are horizontal or vertical, and which way blocks stack. It takes these main values:
| Value | Lines run | Blocks stack | Typical use |
|---|---|---|---|
| `horizontal-tb` | Horizontally | Top to bottom | English, Arabic, Hebrew, most scripts |
| `vertical-rl` | Vertically | Right to left | Traditional Chinese, Japanese |
| `vertical-lr` | Vertically | Left to right | Mongolian, some Japanese layouts |
horizontal-tb is the initial value. The tb stands for top to bottom, which is the block direction. The rl and lr in the vertical values also describe the block direction. In vertical-rl, the first line sits at the right edge and each later line is added to its left. The text inside each line reads from top to bottom.
CSS also defines sideways-rl and sideways-lr, which rotate all text sideways. Browser support is more limited than for the three values above, so check current support before relying on them.
Writing mode does not change width and height
Setting writing-mode: vertical-rl does not swap width and height. width still means the horizontal extent and height still means the vertical extent. What changes is how content flows inside the box. This is why a stylesheet full of physical properties looks wrong in a vertical layout: the boxes keep their physical sizes while the text flows a different way.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Writing modes</title>
<style>
.sample {
border: 2px solid steelblue;
padding: 0.5rem;
margin-block-end: 1rem;
}
.vertical {
writing-mode: vertical-rl;
height: 12rem;
}
</style>
</head>
<body>
<p class="sample">Horizontal text flows left to right.</p>
<p class="sample vertical">Vertical text flows top to bottom.</p>
</body>
</html>The vertical paragraph needs an explicit height. In a vertical mode, lines wrap along the vertical axis, so the browser needs a limit there. Without one, each paragraph would stretch to a single long column.
Text orientation in vertical modes
In a vertical mode, text-orientation controls how individual characters sit. The default, mixed, keeps characters from vertical scripts such as Japanese upright and rotates characters from horizontal scripts such as Latin 90 degrees clockwise. upright sets every character upright. sideways rotates every character as if the line were rotated.
.label {
writing-mode: vertical-rl;
text-orientation: upright;
}The direction property
direction sets the inline base direction, meaning the way a line of text starts and progresses. It has two values: ltr (the initial value) and rtl. In a horizontal writing mode, rtl makes lines start at the right edge and progress leftward.
In a vertical writing mode, the same two values change meaning, because there is no left or right along a vertical line:
ltrmeans the line starts at the top and progresses downward.rtlmeans the line starts at the bottom and progresses upward.
Every combination
| writing-mode | direction | Inline direction | Block direction |
|---|---|---|---|
| `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-rl` | `rtl` | Bottom to top | Right to left |
| `vertical-lr` | `ltr` | Top to bottom | Left to right |
| `vertical-lr` | `rtl` | Bottom to top | Left to right |
Notice that direction never changes the block direction. Only writing-mode does. The next chapter names these two directions the inline and block axes and builds on this table.
Set direction in HTML, not CSS
For right-to-left content, use the dir attribute in HTML instead of the direction property in CSS. The attribute carries meaning even when styles fail to load, and browsers apply it with proper bidirectional text isolation. The CSS specification itself advises authors to use markup for direction where markup exists.
<!DOCTYPE html>
<html lang="ar" dir="rtl">
<head>
<meta charset="utf-8">
<title>مثال</title>
</head>
<body>
<p>هذا نص عربي يبدأ من اليمين.</p>
<p dir="ltr" lang="en">This paragraph overrides the page and starts at the left.</p>
</body>
</html>Three details matter here:
- Put
diron the<html>element to set the whole document, and override it on single elements where needed. - Pair
dirwithlang. They are separate settings:langtells browsers and screen readers which language the text is in, anddirtells the layout which way it flows. dir="auto"lets the browser pick the direction from the first strong character in the text. It suits user-generated content whose language you cannot predict.
Mixed-direction text
A right-to-left sentence often contains left-to-right pieces, such as a brand name or a number. The browser runs a bidirectional algorithm to order these runs. Problems appear when a piece of text with unknown direction, such as a username, sits next to punctuation, because the punctuation can end up on the wrong side. Wrap such text in a <bdi> element, which isolates it from its surroundings:
<p dir="rtl" lang="ar">المستخدم: <bdi>John (admin)</bdi> سجّل الدخول.</p>Elements with a dir attribute are isolated by default, through unicode-bidi: isolate in the browser's built-in stylesheet. You rarely need to set unicode-bidi yourself.
Inheritance and scope
Both writing-mode and direction inherit, so setting them on a container applies them to everything inside. You can change them again on any descendant, which is how a vertical Japanese caption can sit inside a horizontal page. The writing-mode value on the root element also applies to the viewport, so setting it on <html> makes the whole page flow vertically, including scrollbars that run along the block axis.
html {
writing-mode: horizontal-tb;
}
.caption {
writing-mode: vertical-rl;
block-size: 8rem;
}Some writing-mode changes inside a document create a new block formatting context, so a vertical element laid out inside a horizontal parent behaves as an independent box. Floats and margins do not collapse across that boundary.
Common errors
- Using
text-align: rightfor right-to-left text. Browsers already start right-to-left lines at the right. Addingrightbreaks the page when the content switches back. Chapter 10 showsstartandendas the better choice. - Setting
direction: rtlin CSS on a document with nodirattribute. Screen readers and other tools that ignore CSS get the wrong direction. - Forgetting
lang. Without it, font choice, hyphenation and screen reader pronunciation may be wrong, even when the direction is right. - Giving a vertical box no
height. A vertical line has nothing to wrap against, so the text runs as one very tall column. - Expecting
writing-modeto swapwidthandheight. It does not. Chapter 5 covers the logical sizing properties that follow the flow.
Exercise
An element has writing-mode: vertical-lr and direction: rtl. In which physical direction does a line of text start and progress, and where does the second line appear relative to the first?
Show answer
Lines start at the bottom and progress upward, because rtl in a vertical mode means bottom to top. The second line appears to the right of the first, because vertical-lr stacks blocks from left to right.
Exercise
A page in Hebrew sets direction: rtl in its CSS but has no dir attribute on html. Name two reasons to prefer dir="rtl" on the html element instead.
Show answer
The attribute still applies when the stylesheet fails to load or when a tool ignores CSS, such as some screen readers and email clients. The browser also treats elements with a dir attribute as bidirectional isolates, which prevents neighbouring text from being reordered incorrectly.