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

  • SEARCH
  • Home
  • 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 6988295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:03:40+00:00 2026-05-27T19:03:40+00:00

I have an unordered list, which can contain either an even or odd number

  • 0

I have an unordered list, which can contain either an even or odd number of items. I’m looking for a CSS-only way to remove the border from the last 2 li tags if the number of lis is even. The :last-child pseudo-selector removes the last one regardless.

li {
float: left;
border-bottom: 1px solid #000;
}

li:last-child{
border-bottom: none;
}

Works for Odd Numbers of lis

+============================================+
+          1          |          2           +
+--------------------------------------------+
+          3          |                      +
+============================================+

But for even numbers, I need to remove the bottom of cell #3

+============================================+
+          1          |          2           +
+--------------------------------------------+
+          3          |          4           +
+---------------------                       +
+============================================+

So I figured I could use li:nth-last-child() but I can’t figure out what should be the equation to grab the last odd child.

It’s not 2n+1, 2n-1, n-1, or anything I can think of. Please help.

  • 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-27T19:03:40+00:00Added an answer on May 27, 2026 at 7:03 pm

    nth-last-child counts backwards from the last child, so to grab the second to last, the expression is:

    li:nth-last-child(2)
    

    You can combine pseudo-selectors, so to select the 2nd to last child, but only when it’s odd, use:

    li:nth-last-child(2):nth-child(odd) {border-bottom: none;}
    

    And so, the whole thing should be:

    li:last-child,
    li:nth-last-child(2):nth-child(odd) {border-bottom: none;}
    

    In answer to @ithil’s question, here’s how I’d write it in SASS:

    li
      &:last-child,
      &:nth-last-child(2):nth-child(odd)
        border-bottom: none
    

    It’s not that much simpler, since the selection of the ‘second-to-last odd child’ is always going to require the ‘two step’ selector.

    In answer to @Caspert’s question, you can do this for arbitrarily many last elements by grouping more selectors (there feels like there should be some xn+y pattern to do this without grouping, but AFAIU these patterns just work by counting backwards from the last element).

    For three last elements:

    li:last-child,
    li:nth-last-child(2):nth-child(odd),
    li:nth-last-child(3):nth-child(odd) {border-bottom: none;}
    

    This is a place where something like SASS can help, to generate the selectors for you. I would structure this as a placeholder class, and extend the element with it, and set the number of columns in a variable like this:

    $number-of-columns: 3
    
    %no-border-on-last-row
     @for $i from 1 through $number-of-columns
       &:nth-last-child($i):nth-child(odd)
         border-bottom: none
    
    //Then, to use it in your layout, just extend:
    
    .column-grid-list li
      @extend %no-border-on-last-row
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an unordered list <ul> with list items <li> which I'd like to
I have an unordered list, which has maybe 30 items. When one of these
i have a unordered list of links, which are dynamically created by Ajax, and
I have this unordered list <ul> <li>two</li> <li>three</li> </ul> Is there a way I
how can i make it possible say i have a unordered list of 17
I have a simple unordered list with list items as menu item i created
I have an unordered list which is dynamically created, and I need to add
I have a nested unordered list which has main content on the left, and
I have a several pages which has the same ul (unordered list) with the
i have an unordered list of horizontal/vertical segments of length 1, which build one

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.