Course contentsShow
← Web Development
Lesson 2517 of 2,98245. InternationalizationFree lesson

The dir Attribute

Set text direction using the dir attribute on HTML elements and understand auto, ltr, and rtl values.

The dir Attribute

You'll learn how to control text direction in HTML using the dir attribute to properly display left-to-right and right-to-left languages.

What Is the dir Attribute?

The dir attribute tells the browser which direction text should flow within an HTML element. While English and most European languages read left-to-right (LTR), languages like Arabic, Hebrew, and Persian read right-to-left (RTL). The dir attribute ensures content displays correctly for its intended audience.

Think of it like setting the reading direction for a book: Western books open from the left and pages flow right, while Arabic books open from the right and pages flow left. The dir attribute applies this same principle to web content.

The Three Values

The dir attribute accepts three values:

  • ltr (left-to-right): For languages like English, Spanish, French
  • rtl (right-to-left): For languages like Arabic, Hebrew, Urdu
  • auto: Lets the browser automatically detect direction based on the first strongly-typed directional character in the content

Where to Use It

You can apply dir to any HTML element. Setting it on <html> affects the entire document, while setting it on specific elements creates localized directional contexts:

<html dir="rtl">
  <!-- Entire page flows RTL -->
</html>
<div dir="ltr">
  <p>This paragraph flows left-to-right.</p>
  <p dir="rtl">هذه الفقرة تتدفق من اليمين إلى اليسار</p>
</div>

The auto value is particularly useful for user-generated content where you don't know the language in advance.

Key Takeaway: The dir attribute with ltr, rtl, or auto values controls text directionality, ensuring proper display for both left-to-right and right-to-left languages across your HTML elements.