HTML dir Attribute

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The dir global attribute sets an element’s base text direction and isolates that content from surrounding text. Valid values are ltr, rtl, and auto.

Use it for multilingual pages (Arabic, Hebrew, mixed LTR/RTL). Pair it with lang. Prefer HTML dir over CSS direction when you can — direction is part of the content’s meaning.

Baseline · Widely available.

Not deprecated and not experimental. Stable across modern browsers since about July 2015.

lang ≠ direction.

lang names the language; it does not set base direction. Set dir explicitly, especially for RTL pages.

HTML Syntax

syntax.html
<element dir=" ltr | rtl | auto">

If omitted or invalid, direction is usually inherited from the parent (default ltr). Omitting dir does not enable auto-detection.

Attribute Values

Enumerated values (only these three):

ValueDescriptionTry it
ltrLeft-to-right base direction (English, most Western scripts)Try it
rtlRight-to-left base direction (Arabic, Hebrew, and similar)Try it
autoBrowser picks direction from the first strongly directional characterTry it
(omitted)Inherit from parent — not the same as autoTry it

Live Preview

Change dir on the sample text and watch alignment and punctuation placement.

Applies to the paragraph below. Try rtl with Arabic text selected via auto samples in the examples.

Paragraph

Hello world. مرحبا. Punctuation follows the base direction!

dir="ltr"

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Explicit ltr and rtl

Set base direction on each block. Punctuation follows that base.

HTML
<p dir="ltr" lang="en">
  This sentence is in English and reads left to right.
</p>
<p dir="rtl" lang="ar">
  هذه الجملة باللغة العربية وتُقرأ من اليمين إلى اليسار.
</p>
Try It Yourself

How It Works

  1. dir="ltr" sets a left-to-right base for English.
  2. dir="rtl" sets a right-to-left base for Arabic.
  3. lang labels the language for assistive tech and browsers.
  4. Alignment and punctuation follow each paragraph’s base direction.

Example 2 — RTL Page with an LTR Snippet

Set dir on <html> for the whole document; override blocks that differ.

HTML
<!DOCTYPE html>
<html dir="rtl" lang="ar">
  <head>
    <meta charset="utf-8">
    <title>صفحة عربية</title>
  </head>
  <body>
    <p>محتوى الصفحة باللغة العربية.</p>
    <p dir="ltr" lang="en">This paragraph is in English.</p>
  </body>
</html>
Try It Yourself

How It Works

  1. dir="rtl" on html makes the page’s base direction right-to-left.
  2. lang="ar" declares Arabic for the document.
  3. Override the English block with dir="ltr" lang="en".
  4. Children inherit until a nested element sets its own dir.

Example 3 — auto for Unknown Direction

Good for user-generated text when you do not know LTR vs RTL ahead of time.

HTML
<p dir="auto">This comment is in English.</p>
<p dir="auto">هذا التعليق باللغة العربية.</p>
Try It Yourself

How It Works

  1. dir="auto" inspects the first strongly directional character.
  2. Latin letters → LTR base; Arabic letters → RTL base.
  3. Heuristic, not language detection — a comment starting in English then switching to Arabic may stay LTR.
  4. Prefer explicit dir when you already know the language.

Edge Cases & Pitfalls

Most layout bugs come from missing dir on RTL pages or confusing inheritance with auto.

inherit

Omitted ≠ auto

Without dir, the element inherits. It does not auto-detect. Use auto only when you want detection.

rtl

Set document direction for RTL sites

Default base is LTR. Arabic/Hebrew pages should set dir="rtl" on <html>.

inline

Wrap opposite-direction phrases

Isolate an English title inside Arabic with <cite dir="ltr"> (or <bdi>) so punctuation stays with the phrase.

forms

Submit direction with dirname

For inputs, dirname can send the field’s direction with the form data.

css

Prefer markup over CSS

CSS direction can override display, but HTML dir keeps meaning correct without stylesheets.

Browser Support

dir is an HTML global attribute in the browser. It is not a Node.js, Deno, or Bun API.

✓ Baseline · Widely available

dir

Widely available since about July 2015. All modern browsers support ltr, rtl, and auto on HTML elements.

97% Global support (approx.)
Google Chrome All modern
Full support
Mozilla Firefox All modern
Full support
Apple Safari All modern
Full support
Microsoft Edge All modern
Full support
Opera All modern
Full support
Samsung Internet Modern
Full support
Internet Explorer 5+
Supported
dir Widely supported

Bottom line: Safe to use everywhere. Always set dir (and lang) explicitly on RTL documents rather than relying on the LTR default.

Key Takeaways

  • Values: ltr, rtl, or auto — a global enumerated attribute.
  • RTL pages: set dir="rtl" (and lang) on <html>.
  • auto: for unknown / user content; explicit values when known.
  • Prefer HTML dir over CSS for semantic direction.

One line: dir sets base text direction — use ltr/rtl when known, auto when not.

Frequently Asked Questions

It sets an element’s base text direction and isolates that content from surrounding text. Values are ltr (left to right), rtl (right to left), and auto (detect from content).
Yes. You can put dir on almost any HTML element, including html, body, p, and span. Descendants inherit the direction unless they set their own.
Use auto when the direction is unknown in advance — for example user comments or external text. Prefer an explicit ltr or rtl when you know the language.
For pages mainly in Arabic, Hebrew, or other RTL scripts, set dir="rtl" (and lang) on html so the whole document has the correct base direction.
No. lang declares the language. dir sets base direction. Use both together — lang does not imply direction by itself.
Prefer the HTML dir attribute when possible. Direction is part of the content’s meaning, so markup keeps text correct even if CSS is off.

Next: dirname

Send the text direction of an input or textarea with the form.

dirname tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

8 people found this page helpful