Lesson 6 of 9 · 26m
RTL Layout and Bidirectional Text
Build direction-aware interfaces using document direction metadata, logical CSS, bidi isolation, and mirrored behavior where semantics demand it.
Right-to-left support is not translating the interface into Arabic and flipping every pixel. Some things mirror, some do not, and mixed-direction text can corrupt ordering even inside a nominally left-to-right product. Bidirectional text is a data-rendering problem: direction depends on the content, not just the selected UI language.
Direction starts as metadata
Declare direction at the root (<html lang="ar" dir="rtl">) and mark mixed user content at the smallest meaningful container, such as <p lang="he" dir="auto">. dir="auto" inspects strong directional characters; the heuristic is imperfect for strings beginning with numbers or punctuation, but it is far better than no metadata.
Logical CSS properties replace physical assumptions
Stop writing margin-left when what you mean is "outer edge in the inline direction":
.message-row {
margin-inline-start: 0.75rem;
border-inline-start: 3px solid transparent;
text-align: start;
}In LTR, inline-start is left. In RTL, it is right. This preserves layout relationships without duplicating a parallel stylesheet. Physical properties are still correct for genuinely physical geometry, but most layout intent is directional.
Mirror meaning, not machinery
Navigation direction usually mirrors: back points toward the start edge, forward toward the end edge. Playback controls, clocks, circular progress, and many real-world metaphors do not automatically mirror. Icons need semantic review:
mirror: arrow-forward, arrow-back, chevrons in navigation
do not flip: clock, microphone, scissors, text logos, playback symbols
contextual: checklists, undo/redo, reading progress, timeline directionMixed-direction strings need isolation
The classic failure is interpolating user-controlled Arabic or Hebrew content into an English sentence. Unicode bidi algorithms reorder runs, and punctuation can visually jump across boundaries. Wrap embedded values in isolating elements:
<span dir="auto"><bdi>{fileName}</bdi></span>or use Unicode isolates where HTML is unavailable. Avoid relying on raw left-to-right or right-to-left override characters; isolation is safer because it protects surrounding punctuation without forcing the inner direction.
Inputs and numbers are special cases
Email addresses, URLs, phone numbers, credit card numbers, and code are usually left-to-right even inside RTL interfaces. Give them explicit direction and alignment:
input[type="email"], .code-block {
direction: ltr;
text-align: left;
}Field notes
- Arabic and Hebrew are RTL, but numbers inside them are usually displayed left-to-right within the RTL flow.
dir="rtl"on the entire document does not make every nested piece correct; user-generated content still needs its own direction.- Do not reverse strings in code to simulate RTL. Unicode bidi reordering is not string reversal.
- Test Persian, Arabic, and Hebrew content with real native speakers when possible; each community notices different defects.
Was this lesson helpful?