CSS selector markup: element element vs element> element
3 answers
The difference is depth. What the w3schools site doesn't explain well is that it E>F
only matches the closest children E
, but E F
matches the descendants of any depth.
Using your CSS example, consider the following HTML snippet:
<div>
<p id="paragraph1">I'm paragraph 1</p>
<ul>
<li><p id="paragraph2">I'm paragraph 2</p></li>
</ul>
</div>
The first set of rules div p
will match both blocks p
. The second,, div > p
will only match paragraph1
.
+13
source to share
/*This one applies to all childrens (`span`) inside parent*/
div span {
color: red;
}
/*This one says child (`span`) needs to be a direct descendent of parent*/
div > span {
color: green;
}
<div>
<!--(`div`) does not represent an obstacle in both selectors-->
<div>
<span>I live in Duckburg.</span>
</div>
<ul>
<li>
<span>I live in Duckburg.</span>
</li>
</ul>
<span>I live in Duckburg.</span><br>
<span>I live in Duckburg.</span>
</div>
+1
source to share