# :dir()

> The `:dir()` CSS pseudo-class selects elements based on the directionality of the text they contain.

## How do I style elements for right-to-left text in CSS?

The pseudo-class that matches elements by their text direction, 'ltr' or 'rtl'. Unlike the `[dir]` attribute selector, it takes into account the direction inherited from ancestors and detected automatically from content.

- Topic: Pseudo Classes
- Type: pseudo-class
- Browser support: Chrome 120+, Chrome Android 120+, Edge 120+, Firefox 49+, Firefox Android 49+, Safari 16.4+, Safari iOS 16.4+

## Example

Wrong:

```css
[dir='rtl'] .arrow {
  transform: scaleX(-1);
}
```

Right:

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

`:dir()` uses the computed direction, including inherited and 'auto' values, so the icon flips wherever text is right-to-left.

## Common mistakes

- Using `[dir='rtl']`, which doesn't match text marked `dir='auto'` that turns out right-to-left.

## Key points

- Direction styles use `:dir(rtl)` instead of `[dir='rtl']`, which misses inherited and 'auto' directions.
- Direction is set with the HTML `dir` attribute, since `:dir()` ignores the CSS `direction` property.
- Logical properties handle spacing, so `:dir()` is left for things like flipping arrow icons.

## Interview questions

### What is the difference between :dir() and :lang()?

`:dir()` matches by text direction, 'ltr' or 'rtl'. `:lang()` matches by language, and many languages share the same direction.

### Does :dir() follow the CSS direction property?

No. It uses the direction from the HTML `dir` attribute and its inheritance, so set direction in the markup.

## Related cards

- [\[dir\]](https://wonderium.dev/html/dir.md)
- [RTL](https://wonderium.dev/web/rtl.md)
- [Logical Properties](https://wonderium.dev/css/logical-properties.md)
- [:lang()](https://wonderium.dev/css/lang.md)
- [writing-mode](https://wonderium.dev/css/writing-mode.md)

---

- Level: [CSS](https://wonderium.dev/css.md)
- HTML version: https://wonderium.dev/css/dir
- Last updated: 2026-11-25
