01 — The question
The question
Learning outcomes
By the end of this lesson, you can set text direction with dir and lang, isolate mixed-direction text with bdi, use bdo deliberately, and add ruby annotations that degrade gracefully.
02 — The idea
The idea
Terminology
dir: sets the base direction of an element: ltr (left to right), rtl (right to left), or auto (decided from the text).
bdi: isolates a span of text whose direction is unknown or may differ from the surrounding text.
bdo: overrides the bidirectional algorithm for its content, so the direction is forced.
ruby, rt, rp: a short annotation (rt) above a base text (ruby), with rp supplying fallback parentheses for browsers without ruby support.
Direction belongs to the text
Set dir on the html element for a page written in one direction, together with lang. A right-to-left Arabic page starts with html lang="ar" dir="rtl". Pages in one direction rarely need dir on every element.
Mixed content is where direction breaks. A user name such as Asha inside an Arabic sentence can move punctuation to the wrong side if the browser cannot tell its direction. bdi tells the browser to resolve that span on its own.
bdi and bdo are not the same
bdi says “this text may have its own direction; work it out.” bdo says “ignore the text’s natural direction and use this one.” Prefer bdi. Use bdo only when a specific direction must be forced, such as a fixed reference code that must display left to right inside Arabic text.
Ruby annotations
Ruby places a small reading above or beside base text. Modern browsers display rt above the base text. Browsers without ruby support show rt inline, which can be confusing, so rp supplies parentheses that only those browsers display.
03 — Worked example
Worked example
Set direction on a page and a name
Start with a complete page. The example below is English, so dir is ltr, but the name in the sentence may come from a right-to-left language.
Change the code, then look at the preview. The preview runs in a sandbox.
Run the code to see the preview.
The preview appears here.
Ruby with fallback
The annotation rt gives the reading. rp wraps it in parentheses for browsers without ruby support, so the text still reads correctly everywhere.
Change the code, then look at the preview. The preview runs in a sandbox.
Run the code to see the preview.
The preview appears here.
04 — Try it
Try it
Tiered exercises
Work through the levels in order.
Level 1: direction
Write a page whose language is Arabic. Set lang and dir on html, and add one English product name isolated with bdi.
Level 2: forced direction
Explain when bdo is correct and when bdi is the better choice, using one example of each.
Level 3: annotation
Mark up the reading of 漢字 with ruby, and confirm the text still reads sensibly with ruby support switched off.
Complete solutions
05 — Check yourself
Check yourself
Recap and exit questions
Direction belongs to the element that owns the text. Use bdi for unknown direction, bdo only to force one, and ruby with rt and rp for annotations.
06 — What’s next
What’s next
Official references
MDN: bdi element
MDN: bdo element
MDN: ruby element
MDN: dir global attribute
WHATWG HTML Living Standard: The bdi element
WHATWG HTML Living Standard: The ruby element
The idea to take with you
Direction belongs on the element that owns the text. Use bdi for text of unknown direction, bdo only to force a direction, and ruby with rt and rp for annotations that must still read correctly without ruby support.
A small marker for this page. Sign in to save your progress.