Text Direction and Language
You'll learn how to tell the browser which language your text is in and which way it should flow on the page.
Why This Matters
Not all languages read left-to-right like English. Arabic and Hebrew read right-to-left, and some languages like Chinese can flow vertically. The browser needs your help to display text correctly and make it accessible to screen readers.
The lang Attribute
The lang attribute tells browsers and assistive technologies what language your content is written in:
<p lang="en">This is English text.</p>
<p lang="es">Este es texto en español.</p>
<p lang="ar">هذا نص عربي</p>
You should set lang on the <html> element for the whole page, then override it on specific elements when switching languages. Use standard language codes like "en" (English), "fr" (French), or "ar" (Arabic).
The <bdo> Element: Forcing Direction
BDO stands for Bi-Directional Override. It forces text to flow in a specific direction, overriding the default:
<bdo dir="rtl">This English text will appear right-to-left</bdo>
Use dir="ltr" for left-to-right or dir="rtl" for right-to-left.
The <bdi> Element: Isolating Direction
BDI means Bi-Directional Isolate. It isolates text whose direction might differ from surrounding content—useful when mixing usernames or data from different languages:
<p>User <bdi>مستخدم123</bdi> posted a comment.</p>
Without <bdi>, mixed-direction text can scramble unexpectedly.
Key Takeaway: Use the lang attribute to declare your content's language, <bdo> to force text direction, and <bdi> to safely isolate text with uncertain direction—critical for internationalization and accessibility.