I have some CSS code:
.welcome div { font-size: 20px; }
which does what I want it to do, but also writing it like
.welcome > div { font-size: 20px; }
will do the very same.
Is there any reason to use one over the other or are they just two different ways of doing the same thing?
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.
A space in a CSS selector has very special meaning. It means that the part of the selector that occurs right of the space is within (a child of) the part of the selector to the left.
Simple selectors (select elements based on name, id, class) Combinator selectors (select elements based on a specific relationship between them) Pseudo-class selectors (select elements based on a certain state)
No they are completely different, using >
selects a child element whereas using a space will select a nested element at any level.
For example…
Using ␣
/space in the selector…
<div class="welcome"> <section> <div>This will be selected</div> </section> <div>This will be selected as well</div> </div>
So here, the selector having space will target the div
at any nested level of the parent element.
Demo (Using ␣
/space)
.welcome div { font-size: 20px; color: #f00; }
Using >
<div class="welcome"> <section> <div>This won't be selected</div> </section> <div>This will be selected</div> </div>
Whereas here, the selector will target your div
which is a child of the element having .welcome
but it won't select the div
which is nested inside section
tag as it is a grandchild (but not a child) of the outer div
.
Demo 2 (Using >
)
.welcome > div { font-size: 20px; color: #f00; }
From MDN : (For ␣
)
The
␣
combinator (that's meant to represent a space, or more properly one or more whitespace characters) combines two selectors such that the combined selector matches only those elements matching the second selector for which there is an ancestor element matching the first selector. Descendant selectors are similar to child selectors, but they do not require that the relationship between matched elements be strictly parent-child.
From MDN : (For >
)
The
>
combinator separates two selectors and matches only those elements matched by the second selector that are direct children of elements matched by the first. By contrast, when two selectors are combined with the descendant selector, the combined selector expression matches those elements matched by the second selector for which there exists an ancestor element matched by the first selector, regardless of the number of "hops" up the DOM.
They mean two different things.
.welcome div
means select any div
which is a descendant of .welcome
. So it would select all of these div
elements:
<section class="welcome"> <div>Me</div> <div>And me <div>And me <div>And me too!</div> </div> </div> </section>
.welcome > div
only selects a direct child div
of .welcome
. So:
<section class="welcome"> <div>It selects me</div> <div>And me <div>But not me <div>And not me either!</div> </div> </div> </section>
Have a read of http://css-tricks.com/child-and-sibling-selectors/ and https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Getting_started/Selectors
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With