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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:17:08+00:00 2026-06-08T17:17:08+00:00

I have a collection of <li> elements which are aligned so that they form

  • 0

I have a collection of <li> elements which are aligned so that they form two columns. Using a combination of nth-last-child and nth-child(even) – or any other selector – is it possible to apply a style to EITHER: a) the last two (assuming there is an equal number of <li>‘s) OR b) the last one only (assuming there is an odd number of <li>‘s)

See my fiddle for an example or use the code below. I have highlighted in yellow the ones which should have the style applied.

I’m looking for no server side solution, html modifications nor Javascript, I am curious to know if it can be done solely with CSS. Browser compatibility is not an issue so CSS3 all the way.

Code

HTML

<ul>
<li>Hello World</li><!--
--><li>Hello World</li><!--
--><li>Hello World</li><!--
--><li>Hello World</li><!--
--><li>Hello World</li><!--
--><li>Hello World</li><!--
--><li style="background: yellow">Hello World</li><!--
--><li style="background: yellow">Hello World</li>
</ul>

<br />

<ul>
<li>Hello World</li><!--
--><li>Hello World</li><!--
--><li>Hello World</li><!--
--><li>Hello World</li><!--
--><li>Hello World</li><!--
--><li>Hello World</li><!--
--><li style="background: yellow">Hello World</li>
</ul>​

CSS

ul {
    padding: 10px;
    width: 400px;
    background: red;
}

li {
    width: 200px;
    display: inline-block;
    background: blue;
}   ​
  • 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-08T17:17:09+00:00Added an answer on June 8, 2026 at 5:17 pm

    Try this – http://jsfiddle.net/HFn9Q/4/

    /* set the last 2 items bg to yellow */
    li:nth-last-child(2) {
        background: yellow;
    }
    
    /* clean the pre-last item bg in case of odd <li> number */
    li:nth-child(even) {
        background: blue;
    }
    
    /* explicitly set the very last item bg to yellow for even <li> number, otherwise the previous rule will give it a blue bg */    
    li:last-child {
        background: yellow;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a collection of HTML elements that is structured and hierarchical. When an
How do I save a large collection with NHibernate which has elements that surpass
I have a collection of fairly large page elements which, when clicked, slide out
I have two select elements in my form, Category and Sub-category. At first, only
I have a collection of elements in a std::vector that are sorted in a
I am performing serialization using System.Runtime.Serialization.Formatters.Binary.BinaryFormatter . I have a collection which wraps a
I have an entity which has a collection as one of its attributes. That
I have a collection of elements, each one with a name and a subcollection
If I have a collection of div elements, I can use CSS to have
for example I have collection foo of documents like that: {tag_cloud:[{value:games, count:10}, {value:girls, count:500}]}

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.