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 7458723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T13:29:14+00:00 2026-05-29T13:29:14+00:00

I am looking for a CSS selector that lets me select the penultimate child

  • 0

I am looking for a CSS selector that lets me select the penultimate child of a list.

<ul>
     <li>1</li>
     <li>2</li>
     <li>3</li>
     <li>4</li>
     <li>5</li> <!-- select the pre last item dynamically no matter how long this list is -->
     <li>6</li>
</ul>

Static method:

ul li:nth-child(5)

Dynamic method:

ul li:last-child(-1)

which of course doesn’t validate, also nth-last-child doesn’t seem to provide a dynamic way.. I can fallback to javascript but I’m wondering if there is a css way I overlooked

  • 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-05-29T13:29:14+00:00Added an answer on May 29, 2026 at 1:29 pm

    You can use :nth-last-child(); in fact, besides :nth-last-of-type() I don’t know what else you could use. I’m not sure what you mean by “dynamic”, but if you mean whether the style applies to the new second last child when more children are added to the list, yes it will. Interactive fiddle.

    ul li:nth-last-child(2)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking at the jquery site at the contains selector. $(div:contains('John')).css(text-decoration, underline); How
I am testing some weird-looking CSS code that I wrote (I'm using a mix
I am looking to generate some CSS files dynamically in my Content folder. At
In the markup below, I'm looking for a way (perhaps using css selector's) to
I have been looking for a CSS selector function other than Sizzle and I
I am looking to select the last td in each row and using this
I'm looking for examples of open-source CSS selector engines written in C. As an
Looking for the best way to GET the xpath and css selector of a
I'm looking for CSS library that hold all common css best practices. For example
I am looking for a CSS-based web page template where the main content div

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.