dir
Set the text direction for content. Required for right-to-left languages like Arabic, Hebrew, Persian, and Urdu.
Overview
The dir attribute sets the text direction for an element's content. It is essential for right-to-left (RTL) languages including Arabic, Hebrew, Persian (Farsi), and Urdu. Without the correct dir value, text, punctuation, and layout will render incorrectly for these languages.
Set dir on the <html> element for the page's overall direction, and on inline elements when direction changes within the page.
Values
| Value | Behavior |
|---|---|
ltr | Left-to-right. The default for most languages. Text flows from left to right. |
rtl | Right-to-left. Text flows from right to left. Layout mirrors accordingly. |
auto | Browser detects direction from the first strong directional character in the content. Ideal for user-generated content. |
RTL Page
For a fully right-to-left page, set both dir="rtl" and the appropriate lang on <html>.
Mixed LTR and RTL Content
When a page mixes left-to-right and right-to-left content, use dir on individual elements to set the correct direction for each section.
In English, we say "Hello".
In Arabic: مرحبا
In Hebrew: שלום
The bdi Element
The <bdi> element (Bidirectional Isolate) isolates a span of text from its surrounding content so the bidirectional algorithm treats it independently. This is essential when inserting user-generated content with unknown direction into a sentence.
User Alice left a comment.
User إيمان left a comment.
User מירב left a comment.
Why bdi Matters
Without <bdi>, an RTL name in an LTR sentence can cause the browser's bidirectional algorithm to reorder the surrounding text. The result is broken, unreadable sentences.
Rule of thumb: Always wrap user-provided names, labels, or any text of unknown direction in <bdi>.
The bdo Element
The <bdo> element (Bidirectional Override) forces a specific direction on its content, overriding the browser's bidirectional algorithm entirely. Use it sparingly — it is meant for cases where the algorithm produces the wrong result.
dir="auto"
The auto value tells the browser to detect direction from the first strong directional character in the content. This is the best choice for user-generated content where you do not know the language in advance.
CSS direction vs the dir Attribute
CSS has a direction property, but the dir HTML attribute is almost always the correct choice.
| Approach | Affects CSS | Affects Copy/Paste | Affects Accessibility | Semantic |
|---|---|---|---|---|
dir="rtl" (attribute) | Yes | Yes | Yes | Yes |
direction: rtl (CSS) | Yes | No | No | No |
The dir attribute affects the browser's bidirectional algorithm, text selection order, copy-paste behavior, form submission, and assistive technology. The CSS property only changes visual rendering.
CSS Logical Properties
When building layouts that need to work in both LTR and RTL, use CSS logical properties instead of physical ones. VB uses logical properties throughout so that components work correctly regardless of text direction.
| Physical (LTR-only) | Logical (works in both) |
|---|---|
margin-left | margin-inline-start |
margin-right | margin-inline-end |
padding-left | padding-inline-start |
padding-right | padding-inline-end |
border-left | border-inline-start |
text-align: left | text-align: start |
text-align: right | text-align: end |
float: left | float: inline-start |
float: right | float: inline-end |
width | inline-size |
height | block-size |