The Wayback Machine - https://web.archive.org/web/20241011135211/https://www.geeksforgeeks.org/html-dir-attribute/
Open In App

HTML dir Attribute

Last Updated : 28 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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: 

ValueDescription
ltrDefault value. Text direction left-to-right.
rtlText direction right-to-left.
autoText 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: 

dirAttribute

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: 

dirAttribute2

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.


Previous Article
Next Article

Similar Reads

HTML | <dir> compact Attribute
The HTML <dir> compact Attribute is used to define the list should be smaller than the normal by reducing the space between the list items and the indentation of the list. It is a Boolean attribute.Syntax: <dir compact> Note: The HTML <dir> compact attribute is not supported by HTML 5 instead of using this we can use CSS line-heig
1 min read
What is dir Attribute in HTML ?
This is an HTML attribute that is used to specify the text direction of the element content. HTML dir attribute is a global attribute that means it can be applied on any HTML tags for the direction of the element. Syntax: <element dir = "ltr | rtl | auto"> Attribute Value: This attribute contains three values which are listed below. ltr: It i
1 min read
HTML <bdo> dir Attribute
The HTML <bdo> element's dir attribute specifies the text direction within the element. It can have values such as "ltr" (left-to-right) or "rtl" (right-to-left), allowing you to override the default text direction for content. Syntax <bdo dir="ltr|rtl">Attribute Valuesltr: It defines the text direction from left-to-right.rtl: It define
2 min read
HTML5 MathML dir Attribute
This attributes holds the direction value. It holds two types of direction values ltr for left to right and rtl for right to left. This attribute is accepted by <math>, <mi>, <mo>, <mrow>, <ms>, and <mtext>. Syntax: <element dir="ltr|rtl"> Attribute Values: ltr: This value holds the direction from left to r
1 min read
HTML DOM dir Property
The DOM dir Property is used to set or return the value of a dir attribute in an Element. It defines the direction of the text in an Element Content. Syntax HTMLElementObject.dir Return Values: It returns the direction of the text. HTMLElementObject.dir = "ltr|rtl|auto" Properties: ltr: It is a default value and is used to return the text-direction
2 min read
HTML <dir> Tag
The HTML <dir> tag is used to make a list of directory titles. It is not supported in HTML4 as in HTML5 <ul> or CSS are used instead of <dir> tag. The <dir> tag is deprecated & no longer supported in HTML5 Syntax<dir> Lists... </dir>ValuesValues Descriptions ltr Used for languages that read from left to right
3 min read
HTML DOM Bdo dir Property
The Bdo dir Property in HTML DOM is used to set or return the value of the text direction of an element. The dir attribute defines the text direction of an element. Syntax:It is used to return the text direction property. bdoObject.dir It is used to set the text direction property. bdoObject.dir = "ltr|rtl"Property Values:ltr: It defines the left-t
2 min read
PHP | dir() (Get instance of the Directory)
The dir() function in PHP used to find the instance of a Directory class. This function read directory, which includes the following: The given directory is opened. The two properties handle and path of dir() are available. The handle property can be used with other directory functions such as readdir(), rewinddir(), closedir(). The path property i
2 min read
PHP | dir() Function
The dir() function in PHP is an inbuilt function which is used to return an instance of the Directory class. The dir() function is used to read a directory, which includes the following: The given directory is opened. The two properties handle and path of dir() are available. Both handle and path properties have three methods: read(), rewind(), and
2 min read
Node.js console.dir() Method
The console.dir() method is used to get the list of object properties of a specified object. These object properties also have child objects, from which you can inspect for further information. Syntax: console.dir( object ) Parameters: This method accepts single parameter which holds the object element. Example 1: <!DOCTYPE html> <html>
1 min read
Difference between console.dir and console.log
Console Object: The Console object provides access to the browser's debugging console, which can be seen using F12 or ctrl+shift+j. Console object contains many methods, log() and dir() are most used among. The console.log() method prints out a toString representation of the object in the console to the user. Syntax: console.log(object) or console.
2 min read
Node.js fs.Dir.read() Method
The fs.Dir.read() method is an inbuilt application programming interface of class fs.Dir within File System module which is used to read each next directory (dirent) one by one asynchronously. Syntax: const fs.Dir.read(callback) Parameter: This method takes a callback function as a parameter that has the following arguments. err: if any error occur
2 min read
Node.js fs.Dir.close() Method
The fs.Dir.close() method is an inbuilt application programming interface of class fs.Dir with in File System module which is used to close the directory's underlying resource handle asynchronously. Syntax: const dir.close() Parameter: This method does not accept any parameter.Return Value: This method returns the promise which is just an enhanceme
2 min read
Node.js fs.Dir.readSync() Method
The fs.Dir.readSync() method is an inbuilt application programming interface of class fs.Dir within File System module which is used to read each next dirent of directory.Syntax: const dir.readSync() Parameter: This method does not accept any parameter.Return Value: This method returns the dirent of the directory.Below programs illustrates the use
2 min read
Node.js fs.Dir.closeSync() Method
The fs.Dir.closeSync() method is an inbuilt application programming interface of class fs.Dir within File System module which is used to close the directory's underlying resource handle synchronously. Syntax: const dir.closeSync() Parameter: This method does not accept any parameter.Return Value: This method returns an undefined object Below progra
2 min read
Node.js fs.Dir.path() Method
The fs.Dir.path() method is an inbuilt application programming interface of class fs.Dir within the File System module which is used to get the path of the directory. Syntax: const dir.path Parameter: This method does not accept any parameter. Return Value: This method returns the path of the directory. Example 1: The below programs illustrate the
2 min read
CSS :dir Selector
:dir is used to select and change the properties of the text element having direction left to right or right to left. Syntax: :dir(ltr|rtl) Parameters: It accepts the following two parameters. ltr: This keyword basically looks for elements having direction from left to right.rtl: This keyword basically looks for elements having direction from right
2 min read
How href attribute is different from src attribute in HTML ?
In HTML5, the href and src attributes play distinct roles in defining paths or URLs, each associated with specific HTML elements. The href attribute, commonly found in an anchor (<a>) elements, points to the destination of hyperlinks, facilitating navigation. The src attribute, used with elements like <img> and <script>, specifies
1 min read
When to use the class attribute and the id attribute ?
The class attribute is used when multiple HTML elements share a common style or behaviour, allowing the application of a shared style to several elements. It promotes code reusability and is suitable for elements with similar characteristics. The id attribute is utilized when a unique identifier is needed for a specific HTML element. This identifie
1 min read
How to use the target attribute and the rel attribute in the <a> Tag ?
The target and rel attributes in the <a> (anchor) tag is used to control the behaviour of hyperlinks, specifying how the linked content should be opened and establishing relationships between the current and linked documents. Here's how to use these attributes: target Attribute:The target attribute determines where the linked content will be
2 min read
HTML | <html> xmlns Attribute
The HTML <html> xmlns Attribute is used to specify the xml namespace for a document. Important Note: This attribute is mainly required in XHTML, not valid in HTML 4.01, and optional in HTML 5. Syntax: <html xmlns="http://www.w3.org/1999/xhtml"> Attribute Values: https://www.geeksforgeeks.org/ It defines the namespace to use (for XHTML d
1 min read
HTML | scoped Attribute
The HTML scoped attribute is a boolean attribute that is used to specify that the styles only apply to this element's parent element and that element's child elements (not the entire document).Note : It is a deprecated attribute and it is not supported by HTML5. Supported tag: style Example: C/C++ Code <!DOCTYPE html> <html> <head
1 min read
HTML | <th> valign Attribute
The HTML <th> valign Attribute is used to specify the vertical alignment of text content in a header cell. It is not supported by HTML 5. Syntax: <th valign="top | middle | bottom | baseline"> Attribute Value: top: It sets the table header content to top-align. middle: It sets the table header content to middle-align. bottom: It sets th
1 min read
HTML | <col> align Attribute
The HTML <col> align Attribute is used to set the horizontal alignment of text content inside the col element. It is not supported by HTML 5. Syntax: <col align="left | right | center | justify | char"> Attribute Values: left: It sets the text left-align. right: It sets the text right-align. center: It sets the text center-align. justif
1 min read
HTML | poster Attribute
The HTML poster Attribute is used to display the image while video downloading or when the user clicks the play button. If this image not set then it will take the first frame of video as a poster image.Applicable: It is mainly used in <video> element. Syntax: <video poster="URL"> Attribute Values: It contains a single value URL which s
1 min read
HTML | <area> target Attribute
The HTML <area> target Attribute is used to specify where to open the linked document. It works only when href attribute is present in the area elements. Syntax: <area target="_blank|_self|_parent|_top|framename"> Attribute Values: _blank: It opens the link in a new window._self: It is the default value. It opens the linked document in
1 min read
HTML | <select> autocomplete Attribute
The HTML <select> autocomplete attribute is used to specify that the form has autocompleted on or off. When the autocomplete attribute is set to on the browser will automatically complete the values based on which the user entered before. Syntax: <select autocomplete="on | off"> Attribute Values: on: It is the default value. When the au
1 min read
HTML | <table> bgcolor Attribute
The HTML <table> bgcolor Attribute is use to specify the background color of a table.Syntax: <table bgcolor="color_name | hex_number | rgb_number"> Attribute Values: color_name: It sets the text color by using the color name. For example "red".hex_number: It sets the text color by using the color hex code. For example "#0000ff".rgb_numb
1 min read
HTML | srcdoc Attribute
The HTML srcdoc attribute is used to specify the HTML content of the page to show in the inline frame. This attribute is anticipated to be used beside the sandbox and seamless attributes. If a browser supports the srcdoc attribute, it'll override the content laid out in the src attribute (if present). If a browser doesn't support the srcdoc attribu
1 min read
HTML <video> crossorigin Attribute
The HTML crossorigin attribute is used in the <video> element that supports a CORS request when we tried to fetch out the video files from the third-party servers or from another domain. Syntax: <video crossorigin="anonymous | use-credentials"> Attribute Values: anonymous: It has a default value. It defines a CORS request that will be s
1 min read