dir

Baseline Widely available *

This feature is well established and works across many devices and browser versions. It’s been available across browsers since January 2023.

* Some parts of this feature may have varying levels of support.

The dir global attribute is an enumerated attribute that indicates the directionality of the MathML element.

Syntax

html
<math dir="ltr">
<math dir="rtl">

Values

ltr

Left to the right rendering of mathematical expressions (e.g., in English or Moroccan).

rtl

Right to the left rendering of mathematical expressions (e.g., in Arabic, Hebrew, Thaana, Maghreb, Machrek, and so on).

Description

The directionality controls whether math formulas are rendered left-to-right or right-to-left.

Mirroring and stretching MathML formulas in RTL mode

When rendering a written language that displayed right-to-left, the symbols in a MathML formula may need to be mirrored/flipped around the vertical axis, and potentially also stretched vertically.

Mirroring may be achieved using different approaches to find a "base glyph":

  • Character-level mirroring: replacing a character with the equivalent mirrored Unicode code point as defined by Unicode's Bidi_Mirrored property (for example, replacing > with <, or ] with [).
  • Glyph-level mirroring: replacing a character with a glyph that can represent it in a mirrored content (if no direct replacement exists). This requires that the font used supports the Right to left mirror forms (rtlm) font feature.

The base glyph may then be used as a key in the OpenType MathVariant table to get either a larger glyph or a glyph assembly.

Note that effective mirroring and stretching requires a font that includes both a OpenType MathVariant table and support for the rtlm font feature, such as XITS.

Prefer dir to the CSS direction property

This attribute can be overridden by the direction CSS property, if a CSS page is active and the element supports these properties. As the directionality of mathematics 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 formulas will display correctly even on a browser that doesn't support CSS or has the CSS deactivated.

Note: The dir attribute is often set to rtl in the Arabic-speaking world. However, languages written from right to left often embed mathematical content written from left to right. Consequently, the auto keyword from the HTML dir attribute is not recognized and by default the user agent stylesheet resets the direction property on the math element.

Examples

Basic usage

html
<!-- Moroccan style -->
<math display="block" dir="ltr">
  <msqrt>
    <mi>س</mi>
  </msqrt>
  <mo>=</mo>
  <msup>
    <mn>3</mn>
    <mi>ب</mi>
  </msup>
</math>

<!-- Maghreb/Machrek style -->
<math display="block" dir="rtl">
  <msqrt>
    <mi>س</mi>
  </msqrt>
  <mo>=</mo>
  <msup>
    <mn>٣</mn>
    <mi>ب</mi>
  </msup>
</math>

Mirroring and stretching

This example demonstrates the effects of ltr and rtl direction for a more complicated MathML formula, including the effects of setting largeop and stretchy on the <mo> element.

MathML

First we link a stylesheet for the XITS font, which supports the rtlm font feature and MathVariant table needed to properly mirror and stretch glyphs.

html
<link
  rel="stylesheet"
  href="https://fred-wang.github.io/MathFonts/XITS/mathfonts.css" />

The first two formulas set largeop and stretchy to false for the respective elements, and are displayed ltr and rtl.

html
<math dir="ltr" display="block">
  <mrow>
    <munderover>
      <mo largeop="false">∑</mo>
      <mrow>
        <mi>n</mi>
        <mo>=</mo>
        <mn>1</mn>
      </mrow>
      <mn>∞</mn>
    </munderover>
    <mfrac>
      <mn>1</mn>
      <msup>
        <mi>n</mi>
        <mn>2</mn>
      </msup>
    </mfrac>
  </mrow>
  <mo>∊</mo>
  <mrow>
    <mo stretchy="false">(</mo>
    <mfrac>
      <mn>3</mn>
      <mn>2</mn>
    </mfrac>
    <mo>,</mo>
    <mfrac>
      <mn>7</mn>
      <mn>4</mn>
    </mfrac>
    <mo stretchy="false">]</mo>
  </mrow>
</math>
html
<math dir="rtl" display="block">
  <mrow>
    <munderover>
      <mo largeop="false">∑</mo>
      <mrow>
        <mi>n</mi>
        <mo>=</mo>
        <mn>1</mn>
      </mrow>
      <mn>∞</mn>
    </munderover>
    <mfrac>
      <mn>1</mn>
      <msup>
        <mi>n</mi>
        <mn>2</mn>
      </msup>
    </mfrac>
  </mrow>
  <mo>∊</mo>
  <mrow>
    <mo stretchy="false">(</mo>
    <mfrac>
      <mn>3</mn>
      <mn>2</mn>
    </mfrac>
    <mo>,</mo>
    <mfrac>
      <mn>7</mn>
      <mn>4</mn>
    </mfrac>
    <mo stretchy="false">]</mo>
  </mrow>
</math>

This formula is also rtl, but sets largeop to true for the ∑ operator, which renders it larger. It also sets stretchy to true for the braces, which makes them stretch to the height of the contained fraction on supporting browsers.

html
<math dir="rtl" display="block">
  <mrow>
    <munderover>
      <mo largeop="true">∑</mo>
      <mrow>
        <mi>n</mi>
        <mo>=</mo>
        <mn>1</mn>
      </mrow>
      <mn>∞</mn>
    </munderover>
    <mfrac>
      <mn>1</mn>
      <msup>
        <mi>n</mi>
        <mn>2</mn>
      </msup>
    </mfrac>
  </mrow>
  <mo>∊</mo>
  <mrow>
    <mo stretchy="true">(</mo>
    <mfrac>
      <mn>3</mn>
      <mn>2</mn>
    </mfrac>
    <mo>,</mo>
    <mfrac>
      <mn>7</mn>
      <mn>4</mn>
    </mfrac>
    <mo stretchy="true">]</mo>
  </mrow>
</math>

Results

On a browser that supports RTL mirroring and stretching the output for the three formulas should appear as shown below. The first two show the formula left-to-right, and right-to-left without any stretching. The final formula uses rtl with stretching, and largeop for the sum symbol.

Screenshot of the three formula variants on Firefox, which supports both glyph mirroring and element stretching

On this browser, it looks like this:

Specifications

Specification
MathML Core
# dfn-dir

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
dir
Chrome – Full support
Chrome 109 (Release date: 2023-01-10)
footnote Full support
Edge – Full support
Edge 109 (Release date: 2023-01-12)
footnote Full support
Firefox – Full support
Firefox 70 (Release date: 2019-10-22)
footnote
footnote Prior to Firefox 70, the attribute was only accepted on a few elements, as specified in MathML 3.
footnote See https://hg.mozilla.org/mozilla-central/rev/3b30c8db537c
Opera – Full support
Opera 95 (Release date: 2023-02-01)
footnote Full support
Safari – Full support
Safari 8 (Release date: 2014-10-16)
footnote
footnote See https://trac.webkit.org/changeset/159504
Chrome Android – Full support
Chrome Android 109 (Release date: 2023-01-10)
footnote Full support
Firefox for Android – Full support
Firefox for Android 79 (Release date: 2020-07-28)
footnote
footnote Prior to Firefox for Android 79, the attribute was only accepted on a few elements, as specified in MathML 3.
footnote See https://hg.mozilla.org/mozilla-central/rev/3b30c8db537c
Opera Android – Full support
Opera Android 74 (Release date: 2023-03-13)
footnote Full support
Safari on iOS – Full support
Safari on iOS 8 (Release date: 2014-09-17)
footnote
footnote See https://trac.webkit.org/changeset/159504
Samsung Internet – Full support
Samsung Internet 21 (Release date: 2023-05-19)
footnote Full support
WebView Android – Full support
WebView Android 109 (Release date: 2023-01-10)
footnote Full support
WebView on iOS – Full support
WebView on iOS 8 (Release date: 2014-09-17)
footnote
footnote See https://trac.webkit.org/changeset/159504
Mirrors MathML operators in RTL, and stretches operators and grouping symbols
Chrome – Full support
Chrome 144 (Release date: 2026-01-13)
footnote Full support
Edge – Full support
Edge 144 (Release date: 2026-01-21)
footnote Full support
Firefox – Full support
Firefox 146 (Release date: 2025-12-09)
footnote Full support
Opera – Full support
Opera 128 (Release date: 2026-02-26)
footnote Full support
Safari – No support
Safari
footnote No support
Chrome Android – Full support
Chrome Android 144 (Release date: 2026-01-13)
footnote Full support
Firefox for Android – Full support
Firefox for Android 146 (Release date: 2025-12-09)
footnote Full support
Opera Android – Full support
Opera Android 95 (Release date: 2026-02-11)
footnote Full support
Safari on iOS – No support
Safari on iOS
footnote No support
Samsung Internet – No support
Samsung Internet
footnote No support
WebView Android – Full support
WebView Android 144 (Release date: 2026-01-13)
footnote Full support
WebView on iOS – No support
WebView on iOS
footnote No support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support
See implementation notes.

See also