Table of Content
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 rightrtl: Text goes from right to leftauto: 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
dirto control text direction in HTML - Default is left-to-right unless you set it
- Use
rtlfor Arabic or Hebrew - Use
autoto let the browser decide - CSS
directioncan override HTMLdir
FAQs
What does the HTML <dir> attribute do?
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?
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?
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
The HTML ARIA attributes help users with disabilities use websites. These attributes describe roles and states for tools. Understand the…
The header tag defines the top part of a page or a section in HTML. It usually holds a heading…
The HTML <cite> tag is a small but important tool for marking up the title of a creative work. You…
HTML builds web pages. It does this with elements and tags. Many new learners confuse them, so you need to…
HTML is the main content for every web page. It helps you structure text, links, and media. You can build…
The <wbr> tag in HTML helps the browser decide where it can break a word. It does not force a…
The breadcrumb in HTML shows a path through a site and marks the place of a page inside it. It…
Use the HTML object tag to embed different types of external files into your webpage. It works for videos and…
The HTML title attribute adds extra info to any element. You can use it to show tooltips on hover. What…
Forms need instructions so users know what to enter. You use the HTML label tag to give names to input…