Using the dir attribute in HTML

using dir attribute html
09 December 2024

An Introduction to Using the dir Attribute in HTML


Hello! Today we want to talk about the dir attribute in HTML. You might be wondering what this attribute does and whether it is still useful in modern web development. In fact, the dir attribute is used to specify the text direction, allowing you to specify whether the text is oriented from right to left (RTL) or from left to right (LTR). This topic is very important for languages that are written from right to left, such as Persian or Arabic.


In recent years, the use of this attribute has decreased, as many browsers and search engines are able to automatically determine how the text should be displayed. However, if you still need to clearly define these specifications, the dir attribute can help you.


Here, I'll show you some examples of using this attribute. You can see how to use this attribute in different HTML elements. Similarly, I will provide examples of tags and categories that indicate modern and appropriate methods for using this attribute.


If you are ready, let's start with some codes and understand this attribute better. Don't forget to carefully go through each step to fully grasp this concept!


Example Code


<p dir="rtl">سلام، این یک متن نمونه است.</p>
<p dir="ltr">Hello, this is a sample text.</p>
<div dir="rtl">
<p>این یک پاراگراف دیگر با جهت راست به چپ است.</p>
</div>

Code Explanations


<p dir="rtl">سلام، این یک متن نمونه است.</p>
This line gives you a paragraph that its text is from right to left.


<p dir="ltr">Hello, this is a sample text.</p>
This line indicates that the text is displayed from left to right.


<div dir="rtl">
This line indicates a division (div) that allows you to keep the text within it aligned from right to left.


<p>این یک پاراگراف دیگر با جهت راست به چپ است.</p>
This line shows another paragraph that is written from right to left.


</div>
This line tells you that the division has been closed.


FAQ

?

Why should I use the dir attribute?

?

Do I still need to use dir?

?

How can I show right to left text using dir?

?

Does using dir on
work as well?