The HTML dir attribute specifies the text directionality, allowing content to be displayed from left-to-right (ltr), right-to-left (rtl), or determined automatically (`auto`) by the browser based on content.
Supported Tags: It supports all HTML elements.Â
Syntax
<element dir = "ltr | rtl | auto">
Attribute Value
This attribute contains three values which are listed below:Â
| Value | Description |
|---|
| ltr | Default value. Text direction left-to-right. |
| rtl | Text direction right-to-left. |
| auto | Text direction determined by browser based on content. |
HTML dir Attribute examples
Example 1:Â In this example we demonstrates the use of the dir attribute set to “rtl” for right-to-left text direction in the paragraph element.
html
<!DOCTYPE html>
<html>
<head>
<title>dir attribute</title>
</head>
<body>
<h1>GeeksforGeeks</h1>
<h2>dir attribute</h2>
<p dir="rtl">
GeeksforGeeks: A
computer science portal for geeks
</p>
</body>
</html>
Output:Â

HTML dir Attribute example output
Example 2:Â In this example we demonstrates the dir attribute set to “ltr” for left-to-right text direction in a paragraph element, displaying content accordingly.
html
<!DOCTYPE html>
<html>
<head>
<title>dir attribute</title>
</head>
<body>
<h2>dir attribute</h2>
<p dir="ltr">
GeeksforGeeks: A computer science
portal for geeks
</p>
</body>
</html>
Output:Â

HTML dir Attribute example output
Supported Browsers
The browser supported by dir attribute are listed below:Â
HTML dir Attribute – FAQs
Can the dir attribute be used globally in HTML?
Yes, the dir attribute is a global attribute and can be used with any HTML element, including <html>, <body>, <p>, <div>, and more.
How does the dir=”auto” value work?
When dir=”auto” is used, the browser automatically determines the text direction based on the content of the element. It checks the first character with a defined directionality (left-to-right or right-to-left) to set the direction.
What is the default text direction in HTML if the dir attribute is not specified?
The default text direction is left-to-right (ltr) unless otherwise specified by the dir attribute or inherited from a parent element.
How does the dir attribute affect text alignment in CSS?
The dir attribute affects the default alignment of text in elements. For example, with dir=”rtl”, text will start from the right side of the container. However, explicit CSS rules (text-align) will override this behavior.
Is the dir attribute inherited by child elements?
Yes, if a parent element has a dir attribute, its value is inherited by all child elements unless they have their own dir attribute specified.