JavaScript Document dir Property

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline Widely available
Instance property

What You’ll Learn

Document.dir is an instance property (string) for the text directionality of the document: "ltr" (left to right) or "rtl" (right to left). Learn how to read and set it, how it relates to the HTML dir attribute, and five examples with try-it labs.

01

Kind

Read / write

02

Type

string

03

Values

ltr / rtl

04

Default

Often ltr

05

Related

html dir

06

Status

Baseline widely

Introduction

Languages are not only different words—they also flow in different directions. English and many others read left to right (ltr). Arabic, Hebrew, and others often use right to left (rtl).

MDN: Document.dir is a string representing that directionality for the document. Possible values are "rtl" and "ltr". Setting it updates how the page lays out text and many UI directions (start/end edges, scrollbars in some cases, and bidirectional text).

💡
Markup first when you can

Prefer declaring direction in HTML: <html lang="ar" dir="rtl">. Use document.dir when the user switches language/layout at runtime. See also the dir global attribute on MDN.

Related Document tutorials: designMode, defaultView, Document constructor.

Understanding Document.dir

An instance property on Document. You can read the current direction and assign a new one.

  • "ltr" — left to right (MDN; common default).
  • "rtl" — right to left (MDN).
  • Document-wide — sets the page’s base direction (typically via the root).
  • Per-element overrides — use the dir attribute on nested elements when needed.
  • i18n pair — often change lang and dir together when switching locales.

📝 Syntax

JavaScript
// Read
document.dir

// Write
document.dir = "ltr";
document.dir = "rtl";

Value

A string: typically "ltr" or "rtl" (MDN).

Toggle example

JavaScript
document.dir = document.dir === "rtl" ? "ltr" : "rtl";

⚡ Quick Reference

GoalCode / note
Read directiondocument.dir
Left to rightdocument.dir = "ltr"
Right to leftdocument.dir = "rtl"
Check RTLdocument.dir === "rtl"
HTML default<html dir="rtl" lang="ar">
MDN statusBaseline Widely available

🔍 At a Glance

Four facts about document.dir.

Type
string

Direction

Access
get + set

Read / write

Values
ltr | rtl

MDN

Status
baseline

Standard API

📋 When to use ltr vs rtl

ValueMeaningTypical languages
ltrLeft to rightEnglish, Spanish, Hindi (script), many others
rtlRight to leftArabic, Hebrew, Persian, Urdu, …

Always set a correct lang as well—direction alone is not localization.

Examples Gallery

Examples follow MDN Document: dir. Use View Output or Try It Yourself for each case.

📚 Getting Started

Read the current direction and switch to RTL.

Example 1 — Read document.dir

Log the document’s current text direction.

JavaScript
console.log(document.dir);
// Often "" or "ltr" depending on markup / browser defaults
Try It Yourself

How It Works

If no direction was set, some browsers may report an empty string while still laying out as LTR—setting an explicit value is clearer.

Example 2 — Set Right-to-Left

Flip the document to RTL and confirm the property.

JavaScript
document.dir = "rtl";
console.log(document.dir); // "rtl"
Try It Yourself

How It Works

Watch alignment and logical CSS (margin-inline-start, etc.) follow the new base direction in supporting layouts.

📈 Toggle, HTML Root & Locales

UI toggles and keeping markup in sync.

Example 3 — Toggle LTR / RTL

A simple direction switch for demos or language pickers.

JavaScript
function toggleDir() {
  document.dir = document.dir === "rtl" ? "ltr" : "rtl";
  console.log("Now:", document.dir);
}

toggleDir();
Try It Yourself

How It Works

Treat empty string as LTR if your UI needs a strict boolean: (document.dir || "ltr") === "rtl".

Example 4 — Compare with <html dir>

Document.dir and the root element’s dir usually stay aligned.

JavaScript
document.dir = "rtl";

console.log("document.dir:", document.dir);
console.log("html.dir:", document.documentElement.dir);
console.log(
  "attribute:",
  document.documentElement.getAttribute("dir")
);
Try It Yourself

How It Works

Setting either the Document property or the root dir is the usual way to change page direction.

Example 5 — Apply Direction from a Locale Code

Map a few RTL language tags, then set lang and dir together.

JavaScript
function applyLocale(locale) {
  const rtl = /^(ar|he|fa|ur)([-_]|$)/i.test(locale);
  document.documentElement.lang = locale;
  document.dir = rtl ? "rtl" : "ltr";
  console.log(locale, "→", document.dir);
}

applyLocale("ar");
applyLocale("en-US");
Try It Yourself

How It Works

Real apps should use a maintained locale→direction table (or Intl metadata) instead of a tiny regex—this is a teaching sketch.

🚀 Common Use Cases

  • Language switchers — flip dir when the user picks an RTL locale.
  • Multilingual sites — keep document direction in sync with active language.
  • Design previews — toggle LTR/RTL to test mirrored layouts.
  • Component libraries — read document.dir to choose start/end icons.
  • Accessibility — correct direction helps screen readers and selection behavior.
  • Prefer HTML for defaults — set dir on <html> for first paint.

🧠 How Document Direction Applies

1

Direction is declared

Via <html dir> or document.dir = ....

Set
2

Base direction is established

The document root becomes LTR or RTL.

Root
3

Layout & bidi follow

Text flow, alignment, and many logical properties adapt.

Layout
4

Nested dir can override

A subsection can use a different direction without changing the whole page.

📝 Notes

  • MDN: Baseline Widely available (since March 2017) — no Deprecated / Experimental / Non-standard banner.
  • Documented values: "ltr" and "rtl".
  • Pair with lang for real localization.
  • Use logical CSS (inline-start / inline-end) so layouts mirror correctly.
  • Related: designMode, defaultView, Document constructor.

Universal Browser Support

Document.dir is marked Baseline Widely available on MDN (since March 2017). Logos use the shared browser-image-sprite.png sprite from this project.

Baseline · Widely available

Document.dir

Read/write string for document text directionality — "ltr" or "rtl".

Universal Widely available
Google Chrome Full support · Desktop & Mobile
Full support
Mozilla Firefox Full support · Desktop & Mobile
Full support
Apple Safari Full support · macOS & iOS
Full support
Microsoft Edge Full support · Chromium
Full support
Opera Full support · Modern versions
Full support
Internet Explorer Supported (legacy)
Full support
Document.dir Excellent

Bottom line: Use document.dir to read or set page direction. Prefer

for the default, and logical CSS for mirrored layouts.

Conclusion

Document.dir is the Document API for page text direction (ltr / rtl). Set it in markup when possible, and use the property when users switch languages or you need to mirror a layout at runtime.

Continue with doctype, designMode, defaultView, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Set dir on <html> for the default language
  • Update lang and dir together in locale switchers
  • Use logical CSS properties for mirrored UIs
  • Override direction only where a subsection needs it
  • Test both LTR and RTL early in design

❌ Don’t

  • Hard-code left/right margins for multilingual layouts
  • Assume every browser reports "ltr" when unset (may be "")
  • Forget nested dir for mixed-language snippets
  • Treat direction as a substitute for translation
  • Ignore keyboard and focus order after flipping RTL

Key Takeaways

Knowledge Unlocked

Five things to remember about document.dir

Document text direction — ltr or rtl.

5
Core concepts
02

Status

baseline

Standard
🔒03

Access

get + set

DOM
🌐04

Pair with

lang

i18n
📄05

Also

html dir

Markup

❓ Frequently Asked Questions

A string for the document's text directionality: "ltr" (left to right, default for many languages) or "rtl" (right to left, e.g. Arabic or Hebrew).
No. MDN marks Document.dir as Baseline Widely available (since March 2017). It is a standard Document instance property.
Yes. Assign "ltr" or "rtl" to change the document direction. That typically updates the underlying html dir used for layout and bidirectional text.
They work together. The HTML dir global attribute (often on <html>) expresses direction in markup; document.dir is the Document API for reading and writing that document-level directionality (see also MDN dir attribute).
No. Set document (or <html>) direction for the page default. Use the dir attribute on specific elements when only a subsection needs a different direction.
The HTML attribute supports auto for heuristic direction. document.dir's documented API values on MDN are "ltr" and "rtl" — prefer those when setting from JavaScript unless you intentionally manage auto in markup.
Did you know?

CSS logical properties such as margin-inline-start and padding-inline-end automatically flip when document.dir is rtl. That is why modern responsive designs prefer logical sides over hard-coded left / right.

Next: doctype

Learn how to read the DocumentType for the document DTD.

doctype →

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.

6 people found this page helpful