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) |
คำศัพท์ที่เกี่ยวข้อง
เพิ่มเติมใน เว็บและ HTML
การแทนค่าอักขระในรูปแบบข้อความใน HTML สามรูปแบบ: ชื่อ (&), ทศนิยม (&), เลขฐานสิบหก (&) จำเป็นสำหรับอักขระที่ขัดแย้งกับไวยากรณ์ HTML
ชื่อโดเมนที่มีอักขระ Unicode ที่ไม่ใช่ ASCII เก็บไว้ภายในเป็น Punycode (xn--...) แต่แสดงเป็น Unicode ให้ผู้ใช้เห็น ความกังวลด้านความปลอดภัย: การโจมตีแบบ …
ECMAScript Internationalization API providing locale-aware string comparison (Collator), number formatting (NumberFormat), date …
การเข้ารหัสที่เข้ากันได้กับ ASCII สำหรับชื่อโดเมน Unicode แปลงป้ายกำกับที่ถูก internationalize เป็นสตริง ASCII ที่มีคำนำหน้า xn-- münchen.de → …
CSS supports Unicode via escape sequences (\2713 for ✓), the content property …
เวอร์ชัน XML ของการอ้างอิงอักขระเชิงตัวเลข: ✓ หรือ ✓ XML มีเพียง 5 entity ที่มีชื่อ (& …
HTML entity ที่ใช้ชื่อที่อ่านง่าย: © → ©, — → — HTML5 กำหนด 2,231 …
HTML entity ที่ใช้หมายเลข code point Unicode: ทศนิยม (© → ©) หรือเลขฐานสิบหก (© …
การเข้ารหัสอักขระที่ไม่ใช่ ASCII และอักขระที่สงวนไว้ใน URL โดยแทนที่แต่ละไบต์ด้วย %XX ใช้ UTF-8 ก่อน แล้วเข้ารหัส percent แต่ละไบต์: …
การเรนเดอร์อักขระด้วย glyph ข้อความสีเดียวธรรมดาแทนที่จะเป็น emoji แบบสี มักใช้ Variation Selector 15 (U+FE0E) เพื่อแทนที่การแสดงผล emoji …