The HTML dir attribute
The HTML dir attribute sets the base text direction: ltr (left-to-right), rtl (right-to-left, for languages like Arabic and Hebrew), or auto (let the browser infer it from the content). It is a global attribute.
Overview
The dir attribute declares the base writing direction of an element's text. Left-to-right languages use ltr (the default), while right-to-left scripts such as Arabic, Hebrew and Persian use rtl, which flips the text flow and the alignment of punctuation and surrounding layout.
The auto value asks the browser to determine direction from the first strongly-typed character — ideal for user-generated content whose language you cannot predict. Set dir on <html> to establish the page direction and pair it with the lang attribute, which describes the language itself.
Syntax
<html lang="ar" dir="rtl">
<!-- Unknown direction (e.g. user content) -->
<bdi dir="auto">…</bdi>
Values
| Value |
|---|
| ltr | rtl | auto |
Example
<p dir="rtl" lang="ar">مرحبا بالعالم</p>
Best practices
- Set
diron<html>to match the page's primary language direction. - Use
dir="auto"for user-generated content whose direction you cannot know in advance. - Pair
dirwith the lang attribute — they describe different things. - Prefer the
dirattribute over the CSSdirectionproperty for semantic, content-driven direction.
Accessibility
Correct directionality is part of presenting content accurately to everyone. When right-to-left text is marked with dir="rtl", punctuation, mirrored layout and reading order line up as readers expect; without it, mixed-direction text can become jumbled and hard to follow.
Frequently asked questions
What does the dir attribute do?
When should I use dir="rtl"?
What does dir="auto" do?
Do I still need dir if I set lang?
lang describes the language; dir describes the writing direction. Set both for right-to-left content.