Spec-Zone.ru › CSS

`:dir()` CSS pseudo-class

Baseline Widely available

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

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

:dir([ltr | rtl]) {
  /* ... */
}

Parameters

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

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

Examples

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
HTML
# selector-ltr
HTML
# selector-rtl
Selectors Level 4
# dir-pseudo

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
120
120
49
17–53
106
16.4
120
49
17–53
80
16.4
25.0
120
16.4

See also

  • Language-related pseudo-classes: :lang()
  • HTML lang attribute
  • HTML translate attribute

© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Selectors/:dir

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API