dir #
::: section-content
The dir
global attribute is an
enumerated
attribute that indicates the directionality of the element's text.
:::
Try it #
::: section-content ::: iframe ::: {.output-header .border-rounded-top}
HTML Demo: dir #
Reset :::
::: {#warning-no-script .warning-container} ::: warning The interactive example cannot be shown because JavaScript is disabled. ::: :::
::: {#warning-mathml-not-supported .warning-container .hidden} ::: warning The interactive example cannot be shown because MathML is not supported by your browser. ::: :::
::: {#editor-container .editor-container .tabbed-standard .hidden .border-rounded-bottom editor-type=“tabbed”} ::: {#tab-container .section .tabs} ::: {#tablist .tab-list role=“tablist”} HTML
CSS
JavaScript :::
::: {#html-panel .section .hidden tabindex=“0” role=“tabpanel” aria-labelledby=“html” aria-hidden=“true”} ::: {#html-editor}
This paragraph is in English but incorrectly goes right to left.
This paragraph is in English and correctly goes left to right.
<hr />
<p>هذه الفقرة باللغة العربية ولكن بشكل خاطئ من اليسار إلى اليمين.</p>
<p dir="auto">هذه الفقرة باللغة العربية ، لذا يجب الانتقال من اليمين إلى اليسار.</p>
::: :::
::: {#css-panel .section .hidden tabindex=“0” role=“tabpanel” aria-labelledby=“css” aria-hidden=“true”} ::: {#css-editor} ::: :::
::: {#js-panel .section .hidden tabindex=“0” role=“tabpanel” aria-labelledby=“js” aria-hidden=“true”} ::: {#js-editor} ::: ::: :::
::: {#output .output-container}
Output #
::: :::
::: {.section .console-container .hidden aria-hidden=“true”}
Console Output #
![] clear console
::: {#console .console} ::: :::
::: {#html-output .output .editor-tabbed} %html-content% ::: :::
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.
::: {#sect1 .notecard .note}
Note: This attribute is mandatory for the
<bdo>
element where it has a different semantic meaning.
- This attribute is not inherited by the
<bdi>
element. If not set, its value isauto
. - This attribute can be overridden by the CSS properties
direction
andunicode-bidi
, if a CSS page is active and the element supports these properties. - As the directionality of the text is semantically related to its content and not to its presentation, it is recommended that web developers use this attribute instead of the related CSS properties when possible. That way, the text will display correctly even on a browser that doesn't support CSS or has the CSS deactivated.
- The
auto
value should be used for data with an unknown directionality, like data coming from user input, eventually stored in a database. :::
::: {#sect2 .notecard .note}
Note: Browsers might allow users to change the directionality of
<input>
and
<textarea>
s
in order to assist with authoring content. Chrome and Safari provide a
directionality option in the contextual menu of input fields while
Legacy Edge uses the key combinations [Ctrl]{.kbd} + [Left Shift]{.kbd}
and [Ctrl]{.kbd} + [Right Shift]{.kbd}. Firefox uses
[Ctrl]{.kbd}/[Cmd]{.kbd} + [Shift]{.kbd} + [X]{.kbd} but does NOT update
the dir
attribute value.
:::
:::
Specifications #
::: _table #
Specification #
HTML Standard
[#
the-dir-attribute]{.small}
:::
Browser compatibility #
::: _table Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
dir
1 79 1 No 15 ≤4 4.4 18 4 14 ≤3.2 1.0
:::
See also #
::: section-content
- All global attributes.
HTMLElement.dir
that reflects this attribute. :::
::: _attribution
© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5
or later.
https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/dir{._attribution-link}
:::