Possible Duplicate:
Are “div > p” & “div p” same?
Here's the page i'm using as a reference for CSS, please note that i only started learning HTML/CSS this morning.
I'm confused about two of the selectors, quoting the site, the "div p" selector selects all <p> elements inside <div> elements
, and the "div > p" selector selects all <p> elements where the parent is a <div> element
.
What is the difference between those two? Examples where these two selectors can't be used interchangably would be helpful, if possible.
[div > p] selects only the p that are children of the div. So if you had a div with lists or whatever inside that had their own p, their properties would not be affected by this selector. [div p] selects all descendant p in the div. So any p that is inside, or descendant, of a div would be affected.
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)
div > p
selects only the <p>
elements that are immediate children of a <div>
.
So:
div > p
will select this paragraph:
<div>
<p>This is a paragraph</p>
</div>
but will not select this paragraph:
<div>
<table>
<tr>
<td>
<p>This will not get selected</p>
</td>
</tr>
</table>
</div>
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