Home > database >  how to style first element of every even li with css only if possible`` ul li:nth-child(odd)>a{..
how to style first element of every even li with css only if possible`` ul li:nth-child(odd)>a{..

Time:02-21

I have HTML code like this

<ul>
   <li>
      <a>first</a>
      <ul>
         <li><a>sub-1</a></li>
         <li><a>sub-2</a></li>
      </ul>
   </li>
   <li><a>..</a></li>
</ul>

and i want to style every 'a' of odd 'li' only of first 'ul' i am doing it like this

ul>li:nth-child(odd)>a{background:#000}

what is mistake here ??? because it also taking 'a' of sub 'ul'

CodePudding user response:

Test if the parent is not an li

:not(li) > ul > li:nth-child(odd) > a {
  background: red
}
<ul>
  <li>
    <a>first</a>
    <ul>
      <li><a>sub-1</a></li>
      <li><a>sub-2</a></li>
    </ul>
  </li>
  <li><a>..</a></li>
</ul>

  • Related