HTML Language and Bidirectional Text: lang and dir
Declare language and direction separately
The lang attribute identifies the language using a language tag such as en or fa. It can help pronunciation and language-sensitive processing. The dir attribute controls the base text direction: ltr, rtl, or auto. Language alone does not set the direction.
<p lang="fa" dir="rtl">سلام، به بایتمایند خوش آمدید.</p><p lang="en" dir="ltr">Welcome to ByteMind.</p>The two paragraphs declare different languages and base directions.
Isolate unknown text direction
Mixed-direction content can reorder punctuation or numbers unexpectedly. bdi isolates a phrase such as a username from the surrounding text. dir="auto" estimates direction from strong directional characters; it is not a language detector.
<p dir="ltr"> User: <bdi>علی</bdi> — 12 points</p><p dir="auto">سلام دنیا</p>The username is isolated and the second paragraph chooses a base direction automatically.
Use overrides only when intentional
bdo overrides text direction and requires an explicit dir of ltr or rtl. This is different from isolation and usually unnecessary for normal multilingual text. On a full page, declare the main language and direction on html, then override only where needed.
The visible letter order is reversed; the underlying text still contains ABC.
Practice and next steps
Create a Persian paragraph containing an English username and a score. Compare the result with and without bdi.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Language and bidirectional text — Knowledge Check
Apply the concepts from Language and bidirectional text.
Which attribute declares a text language?
Which element isolates a user name of unknown direction?
What does dir="auto" detect?
A Persian paragraph has lang="fa" but inherits LTR direction. What explicitly sets its base direction?
Which element intentionally overrides rather than isolates text direction?