HTML - attributes - dir

revision:


Content

"dir" attribute : specifies the text direction of the content syntax some examples


"dir" attribute : specifies the text direction of the content

top

The dir attribute is a Global Attribute, and can be used on any HTML element. This attribute is mandatory for the <bdo> element where it has a different semantic meaning.


syntax

top

<element dir= "ltr | rtl | auto "></element>

It can have the following values:

ltr, which means left to right and is to be used for languages that are written from the left to the right (like English);
rtl, which means right to left and is to be used for languages that are written from the right to the left (like Arabic);
auto, which lets the user agent decide.

It uses a basic algorithm as it parses the characters inside the element until it finds a character with a strong directionality, then applies that directionality to the whole element.


some examples

top

Write this text right-to-left!

Write this text left-to-right!

This paragraph is in English but incorrectly goes right to left.

This paragraph is in English and correctly goes left to right.

code:
                    <p class="spec" dir="rtl">Write this text right-to-left!</p>
                    <p class="spec" dir=ltr">Write this text left-to-right!</p>
                    <p dir="rtl">This paragraph is in English but incorrectly goes right to left.</p>
                    <p dir="ltr">This paragraph is in English and correctly goes left to right.</p>
                

Example of the HTML "dir" attribute:

This is normal text.

This text would get displayed from left to right.

codes:

                    <p  class="spec" dir="rtl">Example of the HTML "dir" attribute:</p>
                    <p class="spec">This is normal text.</p>
                    <p class="spec" dir="rtl">This text would get displayed from left to right.</p>