Skip to content

The dir Attribute

Global Attribute

The dir attribute specifies the text directionality of an element’s content. It’s essential for supporting right-to-left (RTL) languages like Arabic, Hebrew, and Persian, and can also handle bidirectional text containing mixed LTR and RTL content.

<element dir="value">Content</element>
ValueDescription
ltrLeft-to-right text direction (default for most languages)
rtlRight-to-left text direction (for Arabic, Hebrew, etc.)
autoBrowser determines direction based on content
Result
Result
Result

The dir attribute is inherited by child elements. Setting it on the <html> element affects the entire document:

<html dir="rtl">
<!-- All content defaults to RTL -->
<body>
<p>This text is RTL</p>
<p dir="ltr">This text overrides to LTR</p>
</body>
</html>

When using dir="auto", the browser examines the content:

  1. Finds the first strong directional character
  2. Sets direction based on that character
  3. Ignores neutral characters (numbers, punctuation, spaces)
<!-- Detected as LTR (starts with 'H') -->
<p dir="auto">Hello مرحبا</p>
<!-- Detected as RTL (starts with Arabic) -->
<p dir="auto">مرحبا Hello</p>
<!-- Detected as LTR (ignores numbers/spaces) -->
<p dir="auto">123 Hello World</p>

Always set the dir attribute on the <html> element for RTL languages:

<!DOCTYPE html>
<html lang="ar" dir="rtl">
<!-- Document content -->
</html>

When content direction is unpredictable (comments, social media posts), use dir="auto":

<div class="comment" dir="auto">
{{ userComment }}
</div>

Ensure language and direction are consistent:

<!-- Correct -->
<p lang="ar" dir="rtl">مرحبا</p>
<p lang="he" dir="rtl">שלום</p>
<p lang="en" dir="ltr">Hello</p>
<!-- Inconsistent - avoid -->
<p lang="ar" dir="ltr">مرحبا</p>

Use CSS logical properties for better RTL support:

/* Physical properties - require separate RTL rules */
.box {
margin-left: 20px;
padding-right: 10px;
}
/* Logical properties - work automatically with dir */
.box {
margin-inline-start: 20px;
padding-inline-end: 10px;
}

Always test with bidirectional content:

<!-- Test both directions -->
<p dir="auto">English text with عربي words</p>
<p dir="auto">نص عربي with English words</p>

The dir attribute helps screen readers:

  • Pronounce text correctly with proper intonation
  • Announce direction changes to users
  • Handle abbreviations and numbers appropriately
<!-- Screen reader knows to read RTL -->
<p lang="ar" dir="rtl">
قراءة النص بشكل صحيح
</p>

Direction affects keyboard navigation:

  • RTL mode: Arrow keys navigate right-to-left
  • Text selection: Shift+Arrow selects in the correct direction
  • Caret movement: Cursor moves according to text direction

The dir attribute has universal browser support:

ValueChromeFirefoxSafariEdge
ltrAllAllAllAll
rtlAllAllAllAll
auto20+10+6+79+
  • lang - Specifies the language of content (should be used with dir)
  • translate - Controls whether content should be translated

The CSS direction property can also control text direction, but the dir attribute is preferred:

/* CSS approach - less semantic */
.rtl-text {
direction: rtl;
}
/* HTML approach - recommended */
<p dir="rtl">نص عربي</p>

Why prefer HTML dir attribute:

  • More semantic and accessible
  • Works even when CSS is disabled
  • Properly inherited by form elements
  • Better screen reader support