Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

why does CSS get inherited 2

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
GB
Can someone tell me why I apply CSS to an unordered list with ul li { } , the nested ul li inherits the CSS

so I have to then create css code for ul li ul li { } to switch off the formating?


thanks 1DMF


"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Because
Code:
ul li { color: red; }
Means "apply this CSS to an <li> that is somewhere within a <ul> element" (which, of course, is all <li>s if your code is valid). The <ul> does not need to be at the top level of the document, the <li> does not need to be its direct descendent, this (invalid) code would still trigger the CSS:
Code:
<ul>
   <div>
      <li>I'm red</li>
   </div>
</ul>
[code]
(it might not do so in practice, depending on how individual browsers try to make sense of that code. That's why we validate!)

It'll be triggered here too:
[code]
<ul class="menu">
  <li>I'm red</li>
  <li>
    <ul>
      <li>So am I</li>
    </ul>
  </li>
</ul>
Both <li>s are inside a <ul>, so both trigger the CSS. Changing the CSS to [tt]ul.menu li {}[/tt] won't help either, they're both inside that ul.

If you want the inner <li> to have different properties to the outer ones, you have to come up with a rule that applies only to <li>s contained within other <li>s. Hence
Code:
  ul li li { color: blue; }

Does that make sense?

-- Chris Hunt
Webmaster & Tragedian
Extra Connections Ltd
 
ah of course the child selector, good move!

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
too kind too kind :p

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top