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
أسماء نطاقات تحتوي على أحرف Unicode غير ASCII، مخزنة داخليًا كـ Punycode …
ECMAScript Internationalization API providing locale-aware string comparison (Collator), number formatting (NumberFormat), date …
ترميز متوافق مع ASCII لأسماء نطاقات Unicode، محولاً التسميات الدولية إلى نصوص …
CSS supports Unicode via escape sequences (\2713 for ✓), the content property …
ترميز أحرف غير ASCII والأحرف المحجوزة في URLs باستبدال كل بايت بـ …
خاصية CSS لإدراج محتوى مُولّد عبر عناصر pseudo ::before و::after باستخدام Unicode …
عرض الحرف بشكل إيموجي ملون، عادةً باستخدام Variation Selector 16 (U+FE0F). بعض …
عرض الحرف بشكل رسومي نصي أحادي اللون بدلاً من إيموجي ملون، عادةً …
تمثيل نصي لحرف في HTML. ثلاثة أشكال: مسمى (&)، عشري (&)، ست …
معامل ترويسة HTTP يعلن ترميز الأحرف للاستجابة (Content-Type: text/html; charset=utf-8). يتجاوز أي …