Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select all pseudo elements and classes

There are a bunch of pseudo elements and classes:

Pseudo Elements:

::after, ::before, ::first-letter, ::first-line, ::selection, ::backdrop

Pseudo Classes:

:active, :checked, :default, :dir(), :disabled, :empty, :enabled, :first, :first-child, :first-of-type, :fullscreen, :focus, :hover, :indeterminate, :in-range, :invalid, :lang(), :last-child, :last-of-type, :left, :link, :not(), :nth-child(), :nth-last-child(), :nth-last-of-type(), :nth-of-type(), :only-child, :only-of-type, :optional, :out-of-range, :read-only, :read-write, :required, :right, :root, :scope, :target, :valid, :visited

And there also others like ::-webkit-input-placeholder, ::-moz-placeholder and so on. I don't know what elements they are. But I think they are pseudo elements as it has double colons.

There's an asterisk selector * to select all elements that are inside DOM-Tree.

Now, I'm curious to know why there's no single selector for selecting all pseudo elements and pseudo classes that are outside the DOM-Tree even in css3 or css4?

*pseudo{
  color: red;
}
like image 918
Bhojendra Rauniyar Avatar asked Jul 10 '15 06:07

Bhojendra Rauniyar


People also ask

What are the pseudo-elements and pseudo-classes?

Pseudo-classes enable you to target an element when it's in a particular state, as if you had added a class for that state to the DOM. Pseudo-elements act as if you had added a whole new element to the DOM, and enable you to style that.

What are the pseudo-elements?

A CSS pseudo-element is a keyword added to a selector that lets you style a specific part of the selected element(s). For example, ::first-line can be used to change the font of the first line of a paragraph.

Which of these are pseudo-classes?

A CSS pseudo-class is a keyword added to a selector that specifies a special state of the selected element(s). For example, :hover can be used to change a button's color when the user's pointer hovers over it.


1 Answers

* selects any element regardless of its nature or state. In this way, it already covers all pseudo-classes, just with zero specificity.

For example, * will match any element regardless of whether it is :first-child, :last-child, or both (which itself can be expressed using either :only-child or :first-child:last-child). It will also match any link whether it is unvisited (:link) or visited (:visited), and whether or not it matches one or more of :hover/:active/:focus.

If you're looking for a way to override any and all CSS rules with pseudo-classes for a given element (which can be useful in the case of dynamic pseudo-classes such as the one for links), the only ways are to use an ID selector, an inline style attribute, or !important.

* doesn't match pseudo-elements because it is a simple selector, and a simple selector only matches actual elements. See my answer to this question for a detailed explanation.

The likely reason that there isn't a selector for matching all pseudo-elements is because it doesn't make sense to have one, since different pseudo-elements work differently and have different restrictions as to which CSS properties may be applied to them. For example, content and display don't apply to ::first-letter, ::first-line or ::selection. But the universal selector exists because elements themselves don't define what CSS properties are applicable (not usually, anyway); as far as CSS is concerned, every element is more or less equal.

like image 181
BoltClock Avatar answered Sep 29 '22 10:09

BoltClock