How to select first a element of li with CSS?

css, css-selectors, nested-lists

Solution

You want to use `div.footermenu li.expanded > a:first-child`

jsFiddle

http://jsfiddle.net/eRTV6/

div.footermenu li.expanded > a:first-child {
    font-weight: bold;    
}

Your original selector will select all anchor elements which are first-children of `li.expanded`, by adding a direct descendant selector, `>`, you specify that you only want to select the first, direct descendant of `li.expanded` which are anchors.

Problem

I'm trying to select the first anchor tag of a nested li menu tree: ``` <div class="footermenu"> <ul class="menu"> <li class="expanded first"> <a href="link.html">First menupoint</a> <ul class="menu"> <li class="first"><a href="#">First submenupoint</a></li> <li><a href="#">Second submenupoint</a></li> <li><a href="#">Third submenupoint</a></li> <li class="last"><a href="#">Fourth submenupoint</a></li> </ul> </li> <li class="expanded last"> <a href="link.html">Second menupoint</a> <ul class="menu"> <li class="first"><a href="#">First submenupoint</a></li> <li><a href="#">Second submenupoint</a></li> <li><a href="#">Third submenupoint</a></li> <li class="last"><a href="#">Fourth submenupoint</a></li> </ul> </li> </ul> ``` What I'm trying to accomplish is to select only the first anchor tag of the main menupoints. My css is: ``` div.footermenu li.expanded a:first-child { font-weight: bold; } ``` The problem is that this CSS still selects the submenupoints and I don't know why.

Original source