This is an experimental technology
Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future versions of browsers as the specification changes.
The :dir
CSS pseudo-class matches elements based on the directionality of the text contained in it. In HTML, the direction is determined by the dir
attribute. For other document types there may be other document methods for determining the language.
/* Selects any content with right-to-left text */
:dir(rtl) {
background-color: red;
}
Note that the usage of the pseudo-class :dir()
is not equivalent of using the [dir=…]
attribute selectors. The latter matches a value of the dir
and doesn't match when no attribute is set, even if in that case the element inherits the value of its parent; similarly [dir=rtl]
or [dir=ltr]
won't match the auto
value that can be used on the dir
attribute. In the opposite, :dir()
will match the value calculated by the UA, being inherited or the auto
value.
Also :dir()
considers only the semantic value of the directionality, the one defined in the document, most of the time in HTML. It won't consider styling directionality, the one set by CSS properties like direction
which are purely stylistic.
Syntax
Formal syntax
:dir( ltr | rtl )
Parameters
- direction
- The text direction you want to select for.
Examples
<div dir="rtl"> <span>test1</span> <div dir="ltr">test2 <div dir="auto">עִבְרִית</div> </div> </div>
Given the above HTML, :dir(rtl)
will match the top level <div>
, <span>
containing test1
, and the <div>
with the hebrew characters. :dir(ltr)
will match the <div>
containing test2
:
:dir(ltr) {
background-color: yellow;
}
:dir(rtl) {
background-color: red;
}
The result is as follows:
Specifications
Specification | Status | Comment |
---|---|---|
WHATWG 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.