2

W3C is introducing a new pseudo-class for direction detection in Selectors 4. I am wondering what is the difference between that and a normal attribute selector:

CSS2 - attribute selector

E[dir="rtl"] { ... }

Selectors 4 - dir pseudo-class

E:dir(rtl) { ... }

Is there a specific reason for creating a new pseudo-class for that? Are these selectors identical or do they behave differently? Are there any performance or specificity implications?

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Aziz
  • 7,685
  • 3
  • 31
  • 54
  • 7
    The same reason why a :lang() pseudo exists as opposed to the [lang] attribute selector: http://stackoverflow.com/questions/8916360/whats-the-difference-between-htmllang-en-and-htmllangen-in-css – BoltClock Jan 03 '17 at 15:18

2 Answers2

7

Is there a specific reason for creating a new pseudo-class for that?

The same reason the :lang() pseudo-class was introduced alongside attribute selectors in CSS2.1 See What's the difference between html[lang="en"] and html:lang(en) in CSS?

Are these selectors identical or do they behave differently?

See my answer to the linked question. Here's the relevant quotation from Selectors 4 for the sake of completeness:

The difference between :dir(C) and ''[dir=C]'' is that ''[dir=C]'' only performs a comparison against a given attribute on the element, while the :dir(C) pseudo-class uses the UAs knowledge of the document’s semantics to perform the comparison. For example, in HTML, the directionality of an element inherits so that a child without a dir attribute will have the same directionality as its closest ancestor with a valid dir attribute. As another example, in HTML, an element that matches ''[dir=auto]'' will match either :dir(ltr) or :dir(rtl) depending on the resolved directionality of the elements as determined by its contents. [HTML5]

To drive the point home on the similarities between :dir() and :lang(), if you look closely the first sentence is in fact a word-for-word copy of the same paragraph in the section describing :lang().

Much of the rest of the text for :lang() is new, however, because along with :dir(), Selectors 4 also introduces enhanced functionality for :lang().

Are there any performance or specificity implications?

Since the answer to your previous question is that they behave differently, performance is irrelevant.

No specificity implications because pseudo-classes and attribute selectors are equally specific.


1 It's not clear to me why it took almost 15 years for :dir() to be added to Selectors, but there you go.

Community
  • 1
  • 1
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
2

According to MDN, some subtle differences exist:

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.

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.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360