HTML dir Attribute

Beginner
⏱️ 5 min read
📚 Updated: Jun 2026
🎯 3 Examples
Global & i18n

What You’ll Learn

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.

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.

📝 Syntax

Add dir to any element that needs explicit text direction:

dir.html
<p dir="rtl">مرحبًا! هذا نص عربي.</p>



<p dir="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.

💎 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.
dir-values.html
<html lang="ar" dir="rtl"></html>



<p dir="auto">Hello world</p>

<p dir="auto">مرحبًا</p>

⚡ Quick Reference

ValueDirectionTypical use
ltrLeft to rightEnglish, Spanish, French
rtlRight to leftArabic, Hebrew, Persian
autoAuto-detectedMixed or unknown content
DefaultUsually ltrWhen dir is omitted
JS propertyelement.dirDynamic language switch
Pair withlangAccessibility & SEO

Applicable Elements

ElementSupported?Notes
<html>YesSets document-wide direction
<body>, <div>, <p>YesCommon block-level use
<span>, <a>YesInline RTL/LTR snippets
<input>, <textarea>YesForm field text direction
<script>, <img>Yes (global)Rarely needed on these

dir vs CSS direction

ApproachHTML dirCSS direction
Semantic meaningYes — exposes direction to ATPresentation only
Where to setHTML attributeStylesheet
Best practicePrefer for language/contentUse for visual tweaks only
Valuesltr, rtl, autoltr, rtl

Examples Gallery

LTR and RTL paragraphs, JavaScript toggling, and auto detection with document-level direction.

Example — ltr and rtl

Set explicit direction on each paragraph:

dir-basic.html
<p dir="ltr">This paragraph flows left to right.</p>



<p dir="rtl">مرحبًا! هذا فقرة باتجاه من اليمين إلى اليسار.</p>
Try It Yourself

How It Works

The first paragraph uses default LTR flow. The second uses dir="rtl" so Arabic text aligns from the right edge.

Dynamic Values with JavaScript

Change direction at runtime based on user choice or locale:

dir-dynamic.html
<p id="dynamicElement"></p>



<script>

  var element = document.getElementById("dynamicElement");

  element.dir = useRtl ? "rtl" : "ltr";

</script>
Try It Yourself

How It Works

The dir IDL property accepts ltr, rtl, or auto. Updating it immediately reflows the element’s text direction.

Example — auto and Document Direction

Use auto for mixed content, and set dir on <html> for full RTL pages:

dir-auto.html
<p dir="auto">Hello — LTR detected</p>

<p dir="auto">مرحبًا — RTL detected</p>



<html lang="ar" dir="rtl"></html>

How It Works

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.

♿ 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.

🧠 How dir Works

1

Author sets dir

ltr, rtl, or auto on an element or html.

Markup
2

Browser applies bidi

Unicode bidirectional algorithm orders characters.

Layout
3

Text aligns correctly

Punctuation, lists, and inputs follow direction.

Render
=

Readable multilingual pages

Content displays correctly for LTR and RTL readers.

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 Chrome Fully supported
Full support
Mozilla Firefox Fully supported
Full support
Apple Safari Fully supported
Full support
Microsoft Edge Fully supported
Full support
dir attribute 99% supported

Bottom line: Use dir confidently for multilingual and RTL content in all modern projects.

💡 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

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.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about dir

Bookmark these before building multilingual pages.

5
Core concepts
02

ltr / rtl

Explicit flow.

Values
🔍 03

auto

Detect direction.

Mixed text
📝 04

html + lang

Document RTL.

i18n
05

Accessibility

Pair with lang.

A11y

❓ Frequently Asked Questions

It sets text direction for an element: left-to-right (ltr), right-to-left (rtl), or auto-detected (auto).
ltr, rtl, and auto. Invalid values fall back to ltr in most browsers.
Yes. You can use it on almost any HTML element, including <html>, <p>, and <span>.
Use auto when content language is unknown or mixed, such as user comments, names, or feeds with multiple scripts.
Prefer HTML dir for semantic text direction. CSS direction is for presentation; combine with logical properties for layout.
Yes. Assign element.dir = "rtl" (or ltr / auto) to switch direction dynamically.

Build multilingual-friendly pages

Practice dir="ltr", rtl, and auto with Arabic and English examples in the Try It editor.

Try ltr and rtl example →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

3 people found this page helpful