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의 더 많은 용어
응답의 문자 인코딩을 선언하는 HTTP 헤더 매개변수(Content-Type: text/html; charset=utf-8). 문서 내 인코딩 …
::before 및 ::after 의사 요소를 통해 유니코드 이스케이프를 사용하여 생성된 콘텐츠를 삽입하는 …
HTML에서 문자를 텍스트로 표현하는 방식. 세 가지 형태: 이름(&), 십진수(&), 16진수(&). HTML …
ECMAScript Internationalization API providing locale-aware string comparison (Collator), number formatting (NumberFormat), date …
유니코드 도메인 이름을 ASCII 호환 인코딩으로 변환하여 xn-- 접두사가 붙은 ASCII 문자열로 …
CSS supports Unicode via escape sequences (\2713 for ✓), the content property …
XML 버전의 숫자 문자 참조: ✓ 또는 ✓. XML에는 명명된 엔티티가 5개(& …
비ASCII 유니코드 문자를 포함하는 도메인 이름으로, 내부적으로는 Punycode(xn--...)로 저장되지만 사용자에게는 유니코드로 표시됩니다. …
U+2060. 줄 바꿈을 방지하는 너비 없는 문자. 너비 없는 줄 바꿈 없는 …
사람이 읽기 쉬운 이름을 사용하는 HTML 엔티티: © → ©, — → …