lang declares a text language; dir and bdi control how bidirectional text is embedded in surrounding copy.
HTML lang, dir, and bdi: keep text pronunciation and direction stable
Use it for a real task
An English receipt page includes a customer name whose direction may differ from the sentence. Mark the known language on the name and isolate its direction with bdi, so punctuation around the inserted name does not shift unexpectedly. The document-level language still belongs on html.
<html lang="en">
<body>
<main>
<h1>Receipt owner</h1>
<p>Approved for <bdi lang="ar">ليلى</bdi> on 23 October.</p>
</main>
</body>
</html>What the markup guarantees
If the language is unknown, do not guess it from a person's name. Bidi isolation handles direction interaction; translation and pronunciation still need correct language metadata where known. A fragment inserted by an application should not overwrite the whole page's lang.
Cost and limits
The markup has negligible runtime cost. Text-direction bugs tend to appear only with real mixed-language data, so test a few right-to-left names and punctuation cases.
Common Mistakes
- Do not replace bdi with manual left-to-right marks in every record.
- Set lang where the language actually changes.
- Direction metadata does not translate the content.
Connected lessons
- HTML document skeleton: declare language, encoding, and a real title
- HTML character references: escape data before inserting it into markup
- HTML focus order: use native controls and keep DOM order usable
Related: HTML ruby: attach short pronunciation notes to text.
Related: HTML bdi: isolate an unknown text direction.
Related: HTML dirname: submit text direction beside the user's words.
Related: HTML alternate language links: identify equivalent page versions.
Related: HTML multilingual case page project: preserve names and a clear reading order.
Continue with HTML translate attribute: protect identifiers while localizing the surrounding text.
Further CSS decisions
- CSS hyphens: wrap prose with a declared language
- CSS writing-mode: test layout axes rather than rotating text
Related CSS decisions
- CSS :lang(): style text from its declared language
- CSS text-transform: change display case without corrupting source data
