HTML DOM Element dir attribute

Definition and usage

dir Attribute sets or returns the element's dir attribute.

dir Attribute defines text direction.

See also:

Style direction Attribute

HTML dir Attribute

CSS direction Attribute

Example

Example 1

Change the text direction of "myP" to "right-to-left":

document.getElementById("myP").dir = "rtl";

Try it yourself

Example 2

Return the text direction of the document:

document.body.dir;

Try it yourself

Example 3

Set the text direction of the document:

document.body.dir = "rtl";

Try it yourself

Syntax

Return dir attribute:

element.dir

Set dir attribute:

element.dir = "ltr|rtl|auto"

Attribute value

Value Description
ltr Left-to-right text direction (default).
rtl Right-to-left text direction.
auto Let the browser determine.

Return value

Type Description
String the text direction of the element.

Browser support

All browsers support element.dir:

Chrome IE Edge Firefox Safari Opera
Chrome IE Edge Firefox Safari Opera
Support Support Support Support Support Support