CSS Text Direction
CSS properties (direction, writing-mode, unicode-bidi) controlling text layout direction. Works with Unicode Bidi Algorithm for mixed LTR/RTL content in web pages.
What is CSS Text Direction?
CSS text direction properties control how text flows within a document — left-to-right, right-to-left, or vertically — and how the browser handles text from scripts with mixed directionality. These properties are critical for Arabic, Hebrew, Persian, and Urdu content (right-to-left), as well as traditional Chinese and Japanese typesetting (vertical text).
The direction Property
The direction property sets the base text direction for an element's content:
/* Left-to-right (default for most Latin, CJK text) */
.english { direction: ltr; }
/* Right-to-left (Arabic, Hebrew, Persian) */
.arabic { direction: rtl; }
The direction property only affects block-level elements and inline elements that have a defined display context. For inline text embedded within paragraphs, the Unicode Bidirectional Algorithm (UBA) often handles direction automatically based on the characters present, but direction provides an explicit override.
The HTML dir attribute (<p dir="rtl">) is generally preferred over CSS direction for semantic reasons, as it communicates directionality to assistive technologies and search engines.
The writing-mode Property
writing-mode determines the direction in which lines of text stack:
/* Default: lines stack top to bottom, text flows left or right */
.normal { writing-mode: horizontal-tb; }
/* Vertical: lines stack right to left, text flows top to bottom */
.japanese-vertical { writing-mode: vertical-rl; }
/* Vertical: lines stack left to right, text flows top to bottom */
.mongolian { writing-mode: vertical-lr; }
Traditional Japanese and Chinese books use writing-mode: vertical-rl, where the first column is on the right side of the page and subsequent columns flow leftward. The CSS text-orientation property works alongside writing-mode to control how individual glyphs are rotated within vertical lines.
unicode-bidi — Fine-Grained Bidirectional Control
The unicode-bidi property gives developers fine-grained control over how the Unicode Bidirectional Algorithm applies to an element:
/* Isolate: treat the element's content as an isolated bidi sequence */
.inline-rtl { unicode-bidi: isolate; direction: rtl; }
/* Override: force all characters to display in the declared direction */
.force-ltr { unicode-bidi: bidi-override; direction: ltr; }
/* Embed: add a bidi embedding level */
.embed { unicode-bidi: embed; direction: rtl; }
isolate is the safest modern value — it creates a new bidi context that does not affect the surrounding text. It is the CSS equivalent of the HTML dir attribute with the <bdi> element.
Logical Properties — Layout Independent of Direction
Traditional CSS physical properties (margin-left, padding-right, border-top) break down when the same stylesheet must serve both LTR and RTL layouts. CSS Logical Properties replace physical references with flow-relative ones:
/* Physical (breaks in RTL) */
.button { margin-left: 8px; border-left: 2px solid; }
/* Logical (works in both LTR and RTL) */
.button {
margin-inline-start: 8px;
border-inline-start: 2px solid;
}
| Physical | Logical |
|---|---|
left / right |
inline-start / inline-end |
top / bottom |
block-start / block-end |
margin-left |
margin-inline-start |
padding-right |
padding-inline-end |
Quick Facts
| Property | Key Values |
|---|---|
direction |
ltr, rtl |
writing-mode |
horizontal-tb, vertical-rl, vertical-lr |
unicode-bidi |
normal, embed, isolate, bidi-override, isolate-override |
text-orientation |
mixed, upright, sideways |
| Logical margin | margin-inline-start, margin-inline-end |
| Logical padding | padding-block-start, padding-block-end |
| HTML equivalent | dir attribute, <bdi>, <bdo> |
| Bidi algorithm | Unicode Bidirectional Algorithm (UAX#9) |
関連用語
Web & HTML のその他の用語
レスポンスの文字エンコーディングを宣言するHTTPヘッダーパラメータ(Content-Type: text/html; charset=utf-8)。ドキュメント内のエンコーディング宣言より優先されます。
::beforeおよび::after疑似要素でUnicodeエスケープを使って生成コンテンツを挿入するCSSプロパティ:content: '\2713'は✓を挿入します。
HTMLで文字をテキスト表現する方式。3つの形式:名前(&)・十進数(&)・16進数(&)。HTMLの構文と衝突する文字に必須です。
ECMAScript Internationalization API providing locale-aware string comparison (Collator), number formatting (NumberFormat), date …
Unicode ドメイン名をxn--プレフィックス付きのASCII文字列に変換するASCII互換エンコーディング。münchen.de → xn--mnchen-3ya.de。
CSS supports Unicode via escape sequences (\2713 for ✓), the content property …
XMLバージョンの数値文字参照:✓または✓。XMLには名前付きエンティティが5個(& < > " ')しかありませんが、HTML5は2,231個あります。
デフォルトの絵文字表示の代わりに、通常は異体字セレクター15(U+FE0E)を使って文字をモノクロのテキストグリフでレンダリングすること。
URLの非ASCII文字と予約文字を各バイトを%XXで置き換えてエンコードします。まずUTF-8に変換し、各バイトをパーセントエンコードします:é → %C3%A9。
U+2060。改行を防ぐゼロ幅文字。ゼロ幅ノーブレークスペースとしてのU+FEFF(BOM)の現代的な代替です。