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 )
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 | Status | Comment |
---|---|---|
HTML Living StandardThe definition of ':dir(ltr)' in that specification. | Living Standard | No changes. |
Selectors Level 4The definition of ':dir()' in that specification. | Working Draft | Initial definition. |
Browser compatibility
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
:dir()
|
Chrome
No support No |
Edge
No support No |
Firefox Full support 49 Full support 49 No support 17 — 53 Prefixed' Implemented with the vendor prefix: -moz- |
IE
No support No |
Opera
No support No |
Safari
No support No |
WebView Android
No support No |
Chrome Android
No support No |
Firefox Android Full support 49 Full support 49 No support 17 — 53 Prefixed' Implemented with the vendor prefix: -moz- |
Opera Android
No support No |
Safari iOS
No support No |
Samsung Internet Android
No support No |
Legend
- Full support
- Full support
- No support
- No support
- Requires a vendor prefix or different name for use.'
- Requires a vendor prefix or different name for use.
See also
:dir() by Mozilla Contributors is licensed under CC-BY-SA 2.5.