1

what is the difference between selector strategy 1 and 2? It seems the same to me

  1. div p (Selects all <p> elements inside <div> elements)
  2. div > p (Selects all <p> elements where the parent is a <div> element)
Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701

2 Answers2

2

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>
Asons
  • 84,923
  • 12
  • 110
  • 165
1

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.

https://www.w3.org/TR/css3-selectors/#selectors

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701