开发者

Styling an <li> within another <li>?

开发者 https://www.devze.com 2023-01-13 20:16 出处:网络
I have the following: 开发者_运维技巧<ul id=\'foo\'> <li class=\'fooitem\'> <ul class=\'grok\'>

I have the following:

开发者_运维技巧
<ul id='foo'>
  <li class='fooitem'>
    <ul class='grok'>
      <li class='grokitem'></li>
    </ul>
  </li>
</ul>

I want to style the fooitem elements differently than the grokitem elements:

#foo li {
  background-color: red;
}

.grok li {
  background-color: green;
}

but the #foo li definition is overriding the .grok li definition, so all are appearing red. How do I define a style for the grok items?

Thanks


You need a slightly different style rule (currently the first, with an ID, has a greater level of specifity).
You can resolve it like this:

#foo li {
  background-color: red;
}

#foo .grok li {
  background-color: green;
}

You can give it a try here


You can do it by creating a more specific rule like so:

#foo li {
    background-color: red;
}

#foo .grok li {
    background-color: green;
}


Since your lis appear to have classes assigned to them, you can target them specifically, like so:

ul.grok li.grokitem
{
    /* your styles here */
}


#foo li {
  background-color: red;
}

#foo .grok li {
  background-color: green;
}


You don't need any classes in your example

#foo li { background-color: red; } // All <li>s in foo

#foo li li { background-color: green; } // All <li>s in <li>s in foo
0

精彩评论

暂无评论...
验证码 换一张
取 消