Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7875081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:55:25+00:00 2026-06-03T02:55:25+00:00

I have a page menu being generated that will be similar in structure to

  • 0

I have a page menu being generated that will be similar in structure to the following nested list:

<ul>
    <li>Page 1</li>
    <li>Page 2</li>
    <li>
        Page 3
        <ul>
            <li>Subpage 1</li>
            <li>Subpage 2</li> <!-- Can I target ONLY this element? -->
        </ul>
    </li>
</ul>

Note that this list is dynamic. The number of items and number of levels are not predictable. So, something like this wouldn’t be flexible enough:

/* I know this does not have great browser support - just an example */
ul > li > ul > li:last-child {
    /* CSS here */
}

I realize that I could modify my server code to add a class to the last item, but I would first like to know if this is possible in CSS.

To make matters more complicated, I would like to target all major browsers along with IE 7+.

Is it possible to target the very last list item using CSS?

Basically, I need this Fiddle solved (via @Pumbaa80).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T02:55:26+00:00Added an answer on June 3, 2026 at 2:55 am

    The CSS3 Way (IE9+)

    If you know exactly how deep your last item is, you can repeat your calls to :last-child:

    li:last-child li:last-child {
        color: red;
    }
    

    This pseudo-class selector isn’t supported in IE prior to version 9, according to http://caniuse.com/#search=last-child. Of course, if you don’t know how deep this list item will be, then this method isn’t really helpful.

    The JavaScript Way

    You can target the very last list item via JavaScript as well:

    var items = document.getElementById("mylist").getElementsByTagName("li"),
        _item = items[ items.length - 1 ];
    
    _item.className == "" 
      ? _item.className = "last" 
      : _item.className += " last" ;
    

    This is raw JavaScript, which requires no additional frameworks or libraries. If you’re using a popular framework like jQuery, this could be even simpler:

    $("#mylist li:last").addClass("last");
    

    I wouldn’t suggest you use jQuery just for something like this. Raw JavaScript would be far faster, and far less bloat.

    The Preprocessor way

    Depending on how your navigational menu is constructed, you may be able to use a server-side language to identify the last list-item, and mark it as such. For example, we could perform the following using the DOMDocument class in PHP:

    $d = new DOMDocument();
    $d->loadHTML( $html );
        
    $a = $d->getElementsByTagName("li");
    $l = $a->item( $a->length - 1 );
    
    $c = $l->getAttribute("class");
    
    empty( $c ) 
      ? $l->setAttribute("class", "last") 
      : $l->setAttribute("class", "last $c");
    
    echo $d->saveHTML( $l );
    

    This finds the last list item in the HTML and adds a new class of "last" to it. The benefit to this is that it doesn’t require complicated, and often times poorly-supported, CSS3 selectors. Further, it doesn’t require the addition of large JavaScript libraries to do trivial things.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following page structure: toppage -page1 -page2 -page3 -page4 - (menu separator)
I have the following menu structure (created by Wordpress, so can't change the IDs/Classes)
So on my page I have a menu that is fixed to the top
I have a list menu in my php page. In this list menu i
I have a drop down menu that looks great when the page it scrolled
I have created a user page with a menu that contains a logout button.
I have menu button that is being put in the Navigation controller on every
I have a simple form that has a list (dropdown list generated from a
I have a master page that contains an ASP.NET server side Menu control (System.Web.UI.WebControls.Menu)
I have the following code to layout the menu for my page: //Header Conainer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.