All geek questions in one place

    How to select the first li element with CSS?

    I am trying to select the first anchor tree tag of a sub menu:

    <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:

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

    The problem is that this CSS still selects the submenu, and I don't know why.

    +4
    css css-selectors nested-lists
    Moha Jul 19 '13 at 9:27
    source share
    2 answers

    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; } 

    In the source selector, select all the anchor elements that are the first children of li.expanded by adding a direct descendant selector, > , you indicate that you want to select only the first, direct descendant of li.expanded , which are anchors.

    +9
    ediblecode Jul 19 '13 at 9:33
    source share

    Try the following:

     <html> <head> <style TYPE="text/css"> div.footermenu li.expanded>a{ font-weight: bold; background: Red; } </style> </head> <body> <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> </div> </body> </html> 
    +4
    Martin milan Jul 19 '13 at 9:39
    source share

    Source: https://habr.com/ru/post/1492259/

    More articles:

    • Creating a selection menu using Cocoa Touch and Objective-C - objective-c
    • Playing Audio Files in Android Logs A Strange Error - android
    • What is the function google.maps.event.addDomListener (window, "download", initialization) ;? - javascript
    • https://translate.googleusercontent.com/translate_c?depth=1&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1492257/how-to-add-custom-data-attributes-and-classes-to-htmleditorfor&usg=ALkJrhhED0fBHPtwQYyClVNIcYBywLQNAQ
    • Sonar Periodic Email - Email
    • How to read data sent by the Client using Spark? - javascript
    • Activemerchant Paypal Recurring payment error on PaypalExpressGateway - ruby-on-rails
    • With AFNetworking, how can I read the error message contained in request.responseString - ios
    • Library to connect Java client to nodejs sockjs server? - java
    • Change user IDs to assign additional features - c

    All Articles

    Geek Questions | 2019