Doug Fir
Doug Fir

Reputation: 21212

trouble selecting the first child of nested elements

I'm trying to select the first line item (<li>) with the class "subnav".

I just cannot seem to get it:

html:

<nav>
<ul class="navigation">

 <li class="logo"><ul>
    <li><a href="index.htm"><img src="images/rh_logo_v5.png" alt="roundhaus logo"/></a></li>
</ul></li>
<li class="subnav"><ul>
    <li><a href="index.htm">home</a></li>
   </ul></li>
<li class="subnav"><ul>
    <li><a href="reclaimedwood.htm">reclaimed wood</a></li>
       <li><a href="design.htm">design</a></li>
   </ul></li>
  <li class="subnav"><ul>
    <li><a href="flooring.htm">flooring</a></li>
    <li><a href="paneling.htm">paneling</a></li>
    <li><a href="beams.htm">beams</a></li>
</ul></li>
<li class="subnav"><ul>
    <li><a href="shelving.htm">shelving</a>
    </li><li><a href="mantels.htm">mantels</a></li>
</ul></li>
<li class="subnav"><ul>
    <li><a href="news.htm">news</a></li>
</ul></li>
<li class="subnav"><ul>
    <li><a href="woodtypes.htm">wood types</a></li>
    <li><a href="phrases.htm">phrases</a></li>
</ul></li>

</ul>
</nav>

What would be the CSS selector be for the first instance of the line item with the class "subnav"?

Upvotes: 1

Views: 1455

Answers (2)

Kosta
Kosta

Reputation: 1867

Just use

"li.subnav:first"

If you are using jQuery, then you can get first subnav with:

$("li.subnav:first")

Hope this helps.

Upvotes: 0

BoltClock
BoltClock

Reputation: 723668

If the first .subnav item starts right after a single .logo item, you can use

li.logo:first-child + li.subnav

Notice that the :first-child in your markup is actually .logo, and not one of the .subnav items. If you were trying to use li.subnav:first-child, that's the reason why it doesn't work.

Upvotes: 1

Related Questions