:dir()
Quick Summary for :dir
The :dir() CSS pseudo-class matches elements based on the directionality of the text contained in them.
Code Usage for :dir
/* Selects any element with right-to-left text */ :dir(rtl) {   background-color: red; } 
More Details for :dir

:dir()

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

See also

Language-related pseudo-classes: :lang, :dir HTML lang attribute HTML translate attribute

Last modified: Aug 12, 2021, by MDN contributors

Select your preferred language English (US)DeutschEspañolFrançais日本語Русский中文 (简体) Change language

No Items Found.

Add Comment
Type in a Nick Name here
 
Other Categories in CSS
css
Search CSS
Search CSS by entering your search text above.
Welcome

This is my test area for webdev. I keep a collection of code here, mostly for my reference. Also if i find a good link, i usually add it here and then forget about it. more...

You could also follow me on twitter. I have a couple of youtube channels if you want to see some video related content. RuneScape 3, Minecraft and also a coding channel here Web Dev.

If you found something useful or like my work, you can buy me a coffee here. Mmm Coffee. ☕

❤️👩‍💻🎮

🪦 2000 - 16 Oct 2022 - Boots
Random Quote
When I realized that, no individual step is hard in any process. Building this airport I'm standing in right now started with a guy writing the architectural plans on paper. That's not hard for him to do. Then laying the first beam isn't had. The whole thing is really hard. So, just take each step kind of piece by piece and when I was able to do that and stop trying to chase this prize and started putting in the work, things just started coming together.
Unknown
Random CSS Property

@counter-style

The @counter-style CSS at-rule lets you define counter styles that are not part of the predefined set of styles. A @counter-style rule defines how to convert a counter value into a string representation.
@counter-style css reference