Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS '>' selector; what is it? [duplicate]

I've seen the "greater than" (>) used in CSS code a few times, but I can't work out what it does. What does it do?

like image 509
Bojangles Avatar asked Dec 16 '10 10:12

Bojangles


People also ask

What does '>' mean in CSS?

The greater than sign (>) selector in CSS is used to select the element with a specific parent. It is called as element > element selector. It is also known as the child combinator selector which means that it selects only those elements which are direct children of a parent.

Is CSS selector unique?

The CSS id SelectorThe id of an element is unique within a page, so the id selector is used to select one unique element! To select an element with a specific id, write a hash (#) character, followed by the id of the element.

When multiple conflicting CSS rules match the same element?

What is Specificity? If there are two or more CSS rules that point to the same element, the selector with the highest specificity value will "win", and its style declaration will be applied to that HTML element.


1 Answers

> selects immediate children

For example, if you have nested divs like such:

<div class='outer'>     <div class="middle">         <div class="inner">...</div>     </div>     <div class="middle">         <div class="inner">...</div>     </div> </div> 

and you declare a css rule in your stylesheet like such:

.outer > div {     ... } 

your rules will apply only to those divs that have a class of "middle" since those divs are direct descendants (immediate children) of elements with class "outer" (unless, of course, you declare other, more specific rules overriding these rules). See fiddle.

div {    border: 1px solid black;    padding: 10px;  }  .outer > div {    border: 1px solid orange;  }
<div class='outer'>    div.outer - This is the parent.    <div class="middle">      div.middle - This is an immediate child of "outer". This will receive the orange border.      <div class="inner">div.inner - This is an immediate child of "middle". This will not receive the orange border.</div>    </div>    <div class="middle">      div.middle - This is an immediate child of "outer". This will receive the orange border.      <div class="inner">div.inner - This is an immediate child of "middle". This will not receive the orange border.</div>    </div>  </div>    <p>Without Words</p>    <div class='outer'>    <div class="middle">      <div class="inner">...</div>    </div>    <div class="middle">      <div class="inner">...</div>    </div>  </div>

Side note

If you, instead, had a space between selectors instead of >, your rules would apply to both of the nested divs. The space is much more commonly used and defines a "descendant selector", which means it looks for any matching element down the tree rather than just immediate children as the > does.

NOTE: The > selector is not supported by IE6. It does work in all other current browsers though, including IE7 and IE8.

If you're looking into less-well-used CSS selectors, you may also want to look at +, ~, and [attr] selectors, all of which can be very useful.

This page has a full list of all available selectors, along with details of their support in various browsers (its mainly IE that has problems), and good examples of their usage.

like image 66
Spudley Avatar answered Nov 08 '22 10:11

Spudley