0

Im having a bit of trouble understanding how > works in CSS, I thought what ever element we are targeting, the children of that element would pass on the property. I was wondering if there is an exception to that rule?

<ul id = "outside"> 

          <li>A</li>
          <li>B</li>
          <li>C</li> 
            <ul>
              <li>D</li>
              <li>E</li>
              <li>F</li>
            </ul>
          <li>G</li>

</ul>
#outside > li{
  color: green;
}

A B C are green but D E F are not

<div id="three">
          three
          <div id="two">
            two
           <header id="one">
             one
             <footer id="zero">
              zero
             </footer>
           </header>
          </div>
        </div>
#three > #two {
  color: green;
}

two, one, zero are green but not three

ghost2092000
  • 347
  • 1
  • 4
  • 15

2 Answers2

1

The > operator only selects the direct (first gen) children of the element. In the case of color, the child elements of that targeted element inherit that style rule:

A B C are green but D E F are not

Because the li tags D,E, and F are not descendants of the li tags targeted in the rule:

#outside>li {
  color: green;
}
<ul id="outside">

  <li>A</li>
  <li>B</li>
  <li>C</li>
  <ul>
    <li>D</li>
    <li>E</li>
    <li>F</li>
  </ul>
  <li>G</li>

</ul>

two, one, zero are green but not three

#three is neither targeted nor inherited. Two is directly targeted by this rule, and #one and #zero are inheriting #two's rule:

#three>#two {
  color: green;
}
<div id="three">
  three
  <div id="two">
    two
    <header id="one">
      one
      <footer id="zero">
        zero
      </footer>
    </header>
  </div>
</div>
symlink
  • 11,984
  • 7
  • 29
  • 50
  • 1
    You helped clear up some ambiguity I had, I wasent aware that we were able to inherit to the children, I thought we would only be able to apply our changes to the selector we targeted. I played with the code and added an unordered list inside a
  • and the new unordered list items were green.
  • – ghost2092000 Dec 16 '19 at 21:15
  • @ghost2092000 Technically you were right before that you can only apply changes to the selector you targeted. When a child element is affected by a style rule (e.g. `color: green`) it's because it has *inherited* that rule, not because the rule has been applied to it. Seems like a trivial distinction, I know, but it's important to keep in mind. – symlink Dec 16 '19 at 21:22