-4

could anyone explain the difference between these two?

p a {
  color: red;
}

p > a {
  color: red;
}

Thanks.

rjtkoh
  • 201
  • 3
  • 11
  • 1
    "Anyone" is in this case the [official CSS specification](http://www.w3.org/TR/css3-selectors/#combinators), that does it best. – Boldewyn May 07 '15 at 12:40

1 Answers1

3

'p a' will select all 'a' elements that are contained within a p element (descendents), even if they are not immediate children.

'p>a' will only select immediate children of p that are also 'a' elements.

JSFIDDLE https://jsfiddle.net/seadonk/a9mfbbax/

HTML:

<p>
    <a>CHILD A1</a>
    <span><a>DESCENDENT A2</a></span>
    <a>CHILD A3</a>
</p>

CSS:

/* DESCENDENTS WILL BE RED */
p a{
    color: white;
    background-color: red;
}

/* CHILDREN WILL BE BLUE */
p>a {
    background-color: blue;
    color: white
}

See W3C Schools CSS Selector Reference.

Brino
  • 2,442
  • 1
  • 21
  • 36
  • Not sure, but I've updated my answer to include a fiddle to play around with, hope it helps. – Brino May 07 '15 at 13:01