The dir attribute is a global HTML attribute that controls text direction within an element. Use it for left-to-right (LTR) languages like English, right-to-left (RTL) languages like Arabic and Hebrew, or let the browser detect direction automatically with auto.
01
Global
Most elements.
02
ltr
Left to right.
03
rtl
Right to left.
04
auto
Detect from text.
05
html + lang
Document level.
06
JavaScript
element.dir.
Fundamentals
Purpose of dir
The primary purpose of the dir attribute is to define how text flows within an HTML element. Browsers use direction to align text, position punctuation, order list markers, and lay out mixed LTR and RTL content correctly.
By default, most documents use LTR direction. For RTL languages, set dir="rtl" on the <html> element or on specific blocks that contain RTL text. Pair dir with the lang attribute so screen readers and search engines understand the language.
💡
Set direction at the right scope
Use dir on <html> for an entire RTL page. Use it on individual elements when only part of the page switches language or direction.
Foundation
📝 Syntax
Add dir to any element that needs explicit text direction:
dir.html
<pdir="rtl">مرحبًا! هذا نص عربي.</p><pdir="ltr">This paragraph flows left to right.</p>
Syntax Rules
Global attribute: valid on most HTML elements including <html>, <body>, <div>, and <p>.
Values must be ltr, rtl, or auto (case-insensitive).
Child elements inherit direction unless overridden.
Always combine with lang when marking a different language.
Reference
💎 Values
The dir attribute accepts three keyword values:
ltr — Left-to-right text direction. Default for English and most European languages.
rtl — Right-to-left text direction. Used for Arabic, Hebrew, Persian, Urdu, and similar scripts.
auto — Browser determines direction from the first strongly directional character in the content.
With auto, the browser inspects the first strong directional character. For entire Arabic or Hebrew sites, set both lang and dir on the root <html> element.
A11y
♿ Accessibility & UX
Pair dir with lang — Screen readers use both to pronounce text correctly (e.g. lang="ar" dir="rtl").
Set html dir for RTL sites — Ensures navigation, forms, and scroll behavior match user expectations.
Use auto for user-generated content — Helps mixed-language comments or names display correctly.
Prefer HTML dir over CSS alone — Semantic direction is exposed to assistive technology.
Test mirrored layouts — Icons, margins, and flex order may need logical properties (margin-inline-start) in RTL.
Content displays correctly for LTR and RTL readers.
Compatibility
Browser Support
The dir attribute is supported in all modern browsers. It has been part of HTML for decades and works consistently across platforms.
✓ HTML4+ · Fully supported
Universal text direction control
All major browsers honor dir on global elements including ltr, rtl, and auto.
99%Browser support
Google ChromeFully supported
Full support
Mozilla FirefoxFully supported
Full support
Apple SafariFully supported
Full support
Microsoft EdgeFully supported
Full support
dir attribute99% supported
Bottom line: Use dir confidently for multilingual and RTL content in all modern projects.
Pro Tips
💡 Best Practices
✅ Do
Set dir and lang on html for RTL pages
Use rtl for Arabic, Hebrew, and Persian content
Use auto for mixed or user-generated text
Test layout with both LTR and RTL content
Use logical CSS properties for spacing in RTL
❌ Don’t
Put English sentences in rtl without reason
Rely on CSS direction alone for language
Forget lang when changing dir
Assume icons and margins flip automatically
Use invalid values besides ltr, rtl, auto
Wrap Up
Conclusion
The dir attribute is a crucial tool for controlling text direction in HTML. With ltr, rtl, and auto, you can serve multilingual audiences and ensure content displays correctly for every writing system.
Set direction at the appropriate scope, pair it with lang, and test your pages in both LTR and RTL modes for a better user experience.