References

Beginner-friendly references for web development, with live, editable examples.

The HTML dir attribute

Attribute All modern browsers Updated
Quick answer

The HTML dir attribute sets the base text direction of the element's content. It is used on any HTML element (it is a global attribute).

Overview

The dir attribute sets the base text direction of the element's content. It applies to any HTML element (it is a global attribute).

This is the same directionality attribute documented under Global Attributes as dir — it can be used on every element. Pair it with lang for right-to-left content.

Syntax

<p dir="rtl">مرحبا</p>

Values

Value
ltr | rtl | auto

Example

Live example
<p dir="rtl" lang="ar">مرحبا بالعالم — Hello World</p>

Best practices

  • Set dir="rtl" for right-to-left languages such as Arabic and Hebrew.
  • Set the document-wide direction on the <html> element.
  • For isolating text of unknown direction, use <bdi> rather than forcing a direction.
  • Use the value auto to let the browser infer direction from the content.

Frequently asked questions

What does the dir attribute do?
Sets the text direction of content.
How do I set right-to-left text?
Use dir="rtl" on the element (or on <html> for the whole page).
What is the difference between dir and the bdi element?
dir sets a direction; <bdi> isolates text of unknown direction so it does not disturb its surroundings.
Which elements use the dir attribute?
It is an element-specific attribute, used on elements whose text direction you set, notably <bdo>.