Skip to content
Tutorials Center
My learning

Topics

CSS Reference ยท Pseudo-classes

The :dir() pseudo-class

Reference for the CSS :dir() pseudo-class: match elements by their text direction, how it differs from the dir attribute selector, and when to use logical properties.

The :dir() pseudo-class matches elements by their text directionality: :dir(ltr) for left-to-right and :dir(rtl) for right-to-left. The direction is the one the browser actually uses, which comes from the dir attribute, including inherited values and dir="auto", not just the literal attribute on the element.

It is handy for mirroring icons, borders and shadows in right-to-left languages like Arabic and Hebrew.

CSS
.arrow:dir(rtl) {
  transform: scaleX(-1);
}

Try it

Mirror an arrow for right-to-left text

The arrow flips and the row changes colour whenever the effective direction is right-to-left, including the third row, which only inherits dir from its parent.

Syntax

CSS
:dir( ltr | rtl ) { }
  • Arguments: exactly one keyword, ltr or rtl.
  • Specificity: (0, 1, 0).

Values

Syntax and arguments Description
ltr The element's text direction is left-to-right.
rtl The element's text direction is right-to-left.

Usage notes

  • Not the same as [dir="rtl"]. The attribute selector only sees the element's own attribute. :dir() follows inheritance, handles dir="auto" (direction from the content) and works on elements with no attribute.
  • Prefer logical properties. margin-inline-start, padding-inline-end and text-align: start adapt to direction automatically, without any :dir() rules. Use :dir() for the leftovers, such as flipping icons or gradient angles.
  • Direction is not language. A page can be in Arabic with dir="ltr" for a code block, so match on direction when the issue is direction. Use :lang() for language-driven styling.
  • Only two values. There is no :dir(auto); the pseudo-class matches the resolved direction.
  • Writing modes. In vertical writing modes the inline direction still follows direction; :dir() reports text direction, not the physical axis.
  • Set direction in HTML. Use the dir attribute rather than the CSS direction property for document-level direction, so it stays in the markup.

Browser support

Supported in current Chrome and Edge (120+), Firefox (49+) and Safari (16.4+), so it is Baseline for modern browsers. Older Chrome versions need [dir="rtl"] fallbacks.