This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

The :dir() CSS pseudo-class matches elements based on the directionality of the text contained in them.

/* Selects any element with right-to-left text */
:dir(rtl) {
  background-color: red;
}

The :dir() pseudo-class uses only the semantic value of the directionality, i.e., the one defined in the document itself. It doesn't account for styling directionality, i.e., the directionality set by CSS properties such as direction.

Note: Be aware that the behavior of the :dir() pseudo-class is not equivalent to the [dir=…] attribute selectors. The latter match the HTML dir attribute, and ignore elements that lack it — even if they inherit a direction from their parent. (Similarly, [dir=rtl] and [dir=ltr] won't match the auto value.) In contrast, :dir() will match the value calculated by the user agent, even if inherited.

Note: In HTML, the direction is determined by the dir attribute. Other document types may have different methods.

Syntax

The :dir() pseudo-class requires one parameter, representing the text directionality you want to target.

Parameters

ltr
Target left-to-right elements.
rtl
Target right-to-left elements.

Formal syntax

:dir( ltr | rtl )

Example

HTML

<div dir="rtl">
  <span>test1</span>
  <div dir="ltr">test2
    <div dir="auto">עִבְרִית</div>
  </div>
</div>

CSS

:dir(ltr) {
  background-color: yellow;
}

:dir(rtl) {
  background-color: powderblue;
}

Result

Specifications

Specification Status Comment
HTML Living Standard
The definition of ':dir(ltr)' in that specification.
Living Standard No changes.
Selectors Level 4
The definition of ':dir()' in that specification.
Working Draft Initial definition.

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Edge Opera Safari
Basic support No support[1] 17 (17) -moz
49 (49)[2]
No support No support[3] No support[1] No support[4]
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support No support[1] 17.0 (17) -moz
49.0 (49)[2]
No support No support No support[4]

[1] This feature is not implemented yet. See Chromium bug 576815.

[2] Because the standardized property is implemented since Gecko 49.0 (Firefox 49.0 / Thunderbird 49.0 / SeaMonkey 2.46), the -moz prefixed variant was removed in Gecko 53.0 (Firefox 53.0 / Thunderbird 53.0 / SeaMonkey 2.50).

[3] Microsoft Edge feature request on UserVoice.

[4] This feature is not implemented yet. See WebKit bug 64861.

See also

  • Language-related pseudo-classes: :lang, :dir

Document Tags and Contributors

 Last updated by: mfluehr,