what is the difference between selector strategy 1 and 2? It seems the same to me
- div p (Selects all
<p
> elements inside<div
> elements) - div > p (Selects all
<p
> elements where the parent is a<div
> element)
what is the difference between selector strategy 1 and 2? It seems the same to me
<p
> elements inside <div
> elements)<p
> elements where the parent is a <div
> element)Using >
selects only the elements which is a direct child, in below case only span
which is a direct child of a div
div span {
color: red
}
div > span {
color: lime
}
<div>
<span>Span</span>
<span>Span</span>
<span>Span</span>
<span>
Span
<span>Span</span>
<span>Span</span>
</span>
</div>
Selector #1 (div p
) selects all paragraphs that are descendants of a div. The p
elements could be deeply nested in the div structure, and they will be selected.
Selector #2 (div > p
) selects only paragraphs that are the children (i.e., immediate descendants) of a div.
The first is known as a descendant combinator selector.
The second is a child combinator selector.