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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:56:07+00:00 2026-06-03T14:56:07+00:00

I’ve got a list with hidden and visible items. The visible items have got

  • 0

I’ve got a list with hidden and visible items.
The visible items have got a class called active. I am trying to apply :nth-child(odd) or –:nth-of-type(even) only to these specific visible items, but the result is totally random.

I expect: yellow, green, yellow, green, yellow.

Result: yellow, yellow, green, yellow, green, green.

I thought I would be able to filter on classname as well as node. But obviously that isn’t the case.

    <ul>
      <li>hide</li>
      <li class="active">visible</li>
      <li>hide</li>
      <li class="active">visible</li>
      <li class="active">visible</li>
      <li>hide</li>
      <li class="active">visible</li>
      <li class="active">visible</li>
      <li>hide</li>
      <li class="active">visible</li>
    </ul>

Style:

li {
   background-color:red;
   display:none;
}

li.active {
  display:block;
}

li.active:nth-child(odd) {
  background-color:yellow;
}

li.active:nth-child(even) {
  background-color:green;
}

Is there a way around this?

EDIT:
I cannot use jQuery or any other library, only native js.
In real life I have 50+ list items filled with images and text, therefore removing the invisible ones form the DOM and putting back is probably not a good idea.

  • 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-03T14:56:09+00:00Added an answer on June 3, 2026 at 2:56 pm
      <li></li>                // odd , display none   
      <li class="active"></li> // even                 green
      <li></li>                // odd , display none
      <li class="active"></li> // even                 green
      <li class="active"></li> // odd                  yellow
      <li></li>                // even, display none
      <li class="active"></li> // odd                  yellow
      <li class="active"></li> // even                 green
      <li></li>                // odd , display none
      <li class="active"></li> // even                 green
    

    what doesn’t make sense? What’re your empty <li>s for? Considering doing something different there… if they’re for spacing, add a “spacer” class to some LIs and put margin/padding on it.


    Assuming you want the row-colors to change dynamically depending on what’s visible, you can’t do this with pure CSS. It’s simple using jQuery though, something like:

    $( '#ul li:visible' ).each( function( i ){
        $( this )[ (1&i) ? 'addClass' : 'removeClass' ]( 'active' );
    } )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to loop through a bunch of documents I have to put
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.