Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What’s the difference between the CSS selectors “div p” and “div > p”? [duplicate]

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.

like image 773
dreta Avatar asked Jul 03 '12 23:07

dreta


People also ask

What's the difference between div p and div p?

[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.

What are the 3 different kinds of selectors in CSS?

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)


1 Answers

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>
like image 167
andyzinsser Avatar answered Sep 28 '22 10:09

andyzinsser