Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the precedence of the + operator in CSS selectors?

From the CSS selector spec:

E + F matches any F element immediately preceded by a sibling element E.

What about operator precedence? What does #id1 #id2 + #id3 match? What about #id1 + #id2 #id3?

Is there a selector that means #id1 (#id2 + #id3) or (#id1 + #id2) #id3? (I'm assuming ( and ) aren't really allowed in CSS selectors, I'm not seeing them in the spec)

like image 885
ripper234 Avatar asked Nov 15 '11 11:11

ripper234


People also ask

What is the order of precedence for CSS selector?

Inline CSS has a higher priority than embedded and external CSS. So final Order is: Value defined as Important > Inline > id nesting > id > class nesting > class > tag nesting > tag.

Which CSS selector has the highest precedence?

Id selector has highest priority because of unique nature of the ID attribute definition. We have two classes with one ID selector here it will apply font-size:12px and font-weight:500 due to the specificity rule as ID selector has highest priority after inline CSS.

How do I order CSS selectors?

From Generic to Specific error strong , sort by the first simple selector, then the next, &c. ❧ When sorting rules, use the first selector of each rule to determine the rule's location, both within a section and within the style sheet. Do this after all the rules' selectors have been sorted.

Which takes precedence in CSS class or ID?

The ID selector #first has priority over the class and tag selectors.


1 Answers

Every sequence of simple selectors and combinators is read by browsers from right to left, in a linear fashion. Combinators do not affect the ordering in any way. The rightmost selector, after the last combinator if any, is known as the key selector (see the reference links below for more), and that identifies the element that the rule applies to (also known as the subject of the selector, although note that the key selector may not always represent the subject of the selector, since different applications implement selectors differently).

The selector #id1 #id2 + #id3 means

Select element #id3
if it directly follows as a sibling of #id2
that is a descendant of #id1.

A DOM structure in which #id3 would match the selector would look like this:

#id1   ... any level of nesting     #id2     #id3 

While #id1 + #id2 #id3 means

Select element #id3
if it is a descendant of #id2
that directly follows as a sibling of #id1.

And a DOM structure in which #id3 would match the selector would look like this:

#id1 #id2   ... any level of nesting     #id3 

Notice the difference in the position of element #id2 in this DOM structure, as compared to the one above.

There isn't much of a precedence issue here since the descendant and sibling combinators go in different directions in the DOM. Both selector sequences read right to left either way.

Related answers:

  • CSS combinator precedence?
  • Are parentheses allowed in CSS selectors?
  • CSS Adjacent Selector / Meaning
like image 61
BoltClock Avatar answered Sep 19 '22 12:09

BoltClock