HTML RTL and Language Tagging
Declare language and direction
lang identifies the language using a language tag such as fa or en. dir sets base text direction, not language. On a Persian page use both lang="fa" and dir="rtl" on html. This full document is static because the playground may wrap markup.
<!doctype html><html lang="fa" dir="rtl"> <head> <meta charset="utf-8"> <title>راهنمای HTML</title> </head> <body> <h1>راهنمای HTML</h1> <p>آموزش ساخت صفحه</p> </body></html>The root language is fa and computed direction is rtl.
Mark a language change
Use lang on passages in another language so language-aware tools can adapt pronunciation and other processing. When a known Latin phrase needs its own left-to-right base direction, set dir="ltr" separately. Do not infer direction solely from language tags.
<p lang="fa" dir="rtl"> عنوان: <span lang="en" dir="ltr">HTML Guide</span></p>The surrounding paragraph is RTL; the English phrase has its own LTR direction and language.
Isolate unpredictable names
bdi isolates a text run from surrounding bidirectional text, useful for user names and identifiers. dir="auto" uses a text-based heuristic, not language detection. bdo overrides normal direction and is rarely appropriate for ordinary multilingual content.
<ul dir="ltr"> <li> <bdi>مینا</bdi> : 12 points </li> <li> <bdi>Alex</bdi> : 9 points </li></ul>The two names keep their own inferred direction and do not reorder neighboring punctuation as part of the same run.
Common mistakes and practice
Keep logical reading order in the source; do not reverse strings by hand. For CSS layout prefer logical properties such as margin-inline-start (spacing at the writing direction’s start). Test mixed digits, names, punctuation and narrow layouts visually as well as with assistive technology.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
HTML RTL and Language Tagging — Knowledge Check
Apply the accessibility concepts taught in this lesson.
Which attribute identifies the document language?
Which value sets right-to-left base direction?
Which element isolates unknown-direction names?
Does dir="auto" detect the language?
How should text be stored in HTML?