Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the ">" (greater-than sign) CSS selector mean?

For example:

div > p.some_class {   /* Some declarations */ } 

What exactly does the > sign mean?

like image 310
Misha Moroshko Avatar asked Jul 12 '10 04:07

Misha Moroshko


People also ask

What is '$' in CSS?

$ initiates a variable in SASS which is a language extension that compiles to CSS (Similar to how CoffeeScript compiles to JavaScript) since CSS does not feature variables on its own.

What does the & symbol mean in CSS?

A nested & selects the parent element in both SASS and LESS. It's not just for pseudo elements, it can be used with any kind of selector. e.g.

What will be the Selctor in CSS?

A CSS selector is the first part of a CSS Rule. It is a pattern of elements and other terms that tell the browser which HTML elements should be selected to have the CSS property values inside the rule applied to them.


2 Answers

> is the child combinator, sometimes mistakenly called the direct descendant combinator.1

That means the selector div > p.some_class only matches paragraphs of .some_class that are nested directly inside a div, and not any paragraphs that are nested further within. This implies that every element matching div > p.some_class necessarily also matches div p.some_class, with the descendant combinator (space), so the two are understandably often confused.

An illustration comparing the child combinator with the descendant combinator:

div > p.some_class {      background: yellow; }  div p.some_class {      color: red; }
<div>     <p class="some_class">Some text here</p>     <!-- [1] div > p.some_class, div p.some_class -->     <blockquote>         <p class="some_class">More text here</p> <!-- [2] div p.some_class -->     </blockquote> </div>

Which elements are matched by which selectors?

  1. Matched by both div > p.some_class and div p.some_class
    This p.some_class is located directly inside the div, hence a parent-child relationship is established between both elements. Since "child" is a type of "descendant", any child element is by definition also a descendant. Therefore, both rules are applied.

  2. Matched by only div p.some_class
    This p.some_class is contained by a blockquote within the div, rather than the div itself. Although this p.some_class is a descendant of the div, it's not a child; it's a grandchild. Therefore, only the rule with the descendant combinator in its selector is applied.


1Many people go further to call it "direct child" or "immediate child", but that's completely unnecessary (and incredibly annoying to me), because a child element is immediate by definition anyway, so they mean the exact same thing. There's no such thing as an "indirect child".

like image 164
BoltClock Avatar answered Sep 25 '22 10:09

BoltClock


> (greater-than sign) is a CSS Combinator.

A combinator is something that explains the relationship between the selectors.

A CSS selector can contain more than one simple selector. Between the simple selectors, we can include a combinator.

There are four different combinators in CSS3:

  1. descendant selector (space)
  2. child selector (>)
  3. adjacent sibling selector (+)
  4. general sibling selector (~)

Note: < is not valid in CSS selectors.

enter image description here

For example:

<!DOCTYPE html> <html> <head> <style> div > p {     background-color: yellow; } </style> </head> <body>  <div>   <p>Paragraph 1 in the div.</p>   <p>Paragraph 2 in the div.</p>   <span><p>Paragraph 3 in the div.</p></span> <!-- not Child but Descendant --> </div>  <p>Paragraph 4. Not in a div.</p> <p>Paragraph 5. Not in a div.</p>  </body> </html> 

Output:

enter image description here

More information about CSS Combinators

like image 26
Premraj Avatar answered Sep 23 '22 10:09

Premraj