HTML dir Attribute: How to Set Text Direction

html dir attribute

The dir attribute tells the browser which way text should go in HTML. You use it when a page or part of it needs left-to-right or right-to-left layout.

What Is the HTML dir Attribute?

The dir attribute controls text direction in HTML. It sets the writing flow. You use it to support languages that go from right to left like Arabic or Hebrew.

It also helps when you show mixed languages on the same page. Some go from left to right, others from right to left. So you must tell the browser how to handle that.

By default, HTML uses left-to-right direction. This suits English and other similar languages. But when the page includes right-to-left content, the layout may break. That is where dir helps.

Here is an example:

<tag dir="ltr|rtl|auto">Content here</tag>
  • ltr: Text goes from left to right
  • rtl: Text goes from right to left
  • auto: The browser picks direction based on content

You can add dir to the whole page using the <html> tag. You can also use it on smaller parts like a paragraph, span, or div.

Here is another example:

<html dir="rtl">
<body>
  <p>This text goes from right to left.</p>
</body>
</html>

This makes the whole page flow right to left. You can also do this in smaller blocks:

<p dir="rtl">مرحبا بك في الصفحة</p>
<p dir="ltr">Welcome to the page</p>

Here, each paragraph flows in its own direction.

The dir attribute only controls direction. It does not fix font, spacing, or alignment and also does not support mixed directions in the same word.

It does not work well when the content direction changes inside the same line. Use it only when full blocks of content change direction.

The dir vs CSS Direction Property in HTML

If you use both dir and CSS direction, the CSS rule wins. The browser gives more weight to styles than HTML attributes.

HTML dir is easier for quick changes. CSS direction works better in big projects. Use dir when you need to switch text flow in one tag and CSS when you want full control.

Use dir for layout in small parts andCSS when you need many rules. Avoid mixing both without reason. It can confuse the layout and cause errors.

Examples of dir Attribute in HTML

Set Right-to-Left on Full Page:

<html dir="rtl">
  <body>
    <p>هذا نص باللغة العربية</p>
  </body>
</html>

This sets the entire page to flow from right to left. Arabic text lines up to the right side. It shows how to handle full-page direction change.

Mixed Paragraph Directions on One Page:

<p dir="rtl">هذا النص باللغة العربية</p>
<p dir="ltr">This text is in English</p>

This example shows two languages on one page. Arabic text flows right. English text flows left. Each paragraph keeps its own direction using dir.

Auto Direction for Dynamic Content:

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

This example lets the browser decide the direction. The browser checks the first strong letter. Then it picks the right flow. This helps when content comes from user input.

Wrapping Up

In this article, you learned what the dir attribute does and where to use it. You also saw how to use ltr, rtl, and auto with real examples.

Here is a quick recap:

  • Use dir to control text direction in HTML
  • Default is left-to-right unless you set it
  • Use rtl for Arabic or Hebrew
  • Use auto to let the browser decide
  • CSS direction can override HTML dir

FAQs

What does the HTML <dir> attribute do?

The dir attribute in HTML sets the text direction within an element. It controls the flow of text either from left to right or right to left.
<p dir="rtl">This text flows right to left</p>
<p dir="ltr">This text flows left to right</p>

How to use the <dir> attribute in HTML for right-to-left languages?

Use the dir attribute with the value rtl to make text flow from right to left. This is common in Arabic, Hebrew, or Persian text.
<div dir="rtl">
  <p>مرحبا بك</p>
</div>

Can the <dir> attribute override CSS direction rules?

Yes. The HTML dir attribute can override direction set in CSS. Inline dir takes precedence unless overwritten by !important in CSS.
<div dir="rtl">This overrides any CSS unless !important is used</div>

<style>
  div { direction: ltr !important; }
</style>

Similar Reads

HTML dialog Tag: How to Create Dialog Boxes

The <dialog> tag is used to build modal popups in HTML. It shows alerts and messages without third-party scripts. Understand…

HTML Global Attributes Overview

HTML global attributes work on most elements. They do not depend on the tag name. You can use them with…

HTML Blockquote Tag: How to Create Quotes

The HTML blockquote tag defines a long quote from another source. It sets the quoted text apart from other content.…

HTML Document Structure: How to Create DOCTYPE in HTML

The DOCTYPE ensures that your HTML is readable by browsers and ensures that pages follow modern standards. Understand the DOCTYPE…

How to Use sub Tag for Subscript Text in HTML

The sub tag in HTML shows text in subscript form. It places the text slightly below the normal baseline of…

HTML p Tag: How to Create a Paragraph in HTML

Paragraphs help group-related text. They build clear, readable pages. You use them to split ideas or topics. The HTML p…

HTML track Tag: How to Add Subtitles to Videos

The track tag in HTML adds text tracks like subtitles and captions to media. It improves video access and helps…

HTML details Tag: How to Toggle Hidden Content

The <details> tag hides content and shows it when needed. It helps organize long pages. The tag shows extra info…

HTML sup Tag: How to Add Superscript in Web Pages

The HTML sup tag puts text above the normal line. Writers use it for math power and marks. So, readers…

Comments in HTML: How They Work with Examples

Comments in HTML help you explain your code. You can add notes without showing anything in the browser. Understand How…

Previous Article

HTML lang Attribute: How it Works with Examples

Next Article

HTML autocomplete Attribute: How to Use it with form and input

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.