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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:27:27+00:00 2026-05-28T02:27:27+00:00

I need to make a ul that changes background colors in groups of 4

  • 0

I need to make a ul that changes background colors in groups of 4 li’s (zebra striping but I want to be able to apply the background color once in the css and then access it with JQuery). I don’t want to just use something like nth-child because li’s may need to be removed periodically, and I want the layout to remain without having to go in and change the class for each li. I’m not sure if this makes sense, but I’ve put together an example html to try to show what I’m talking about.

I’d appreciate any possible suggestions for how to change the style (from white to grey background) at every fourth element (li).

Here are the styles:

li.grey {
background-color: #b9b9b9;
}

li.white {
background-color: #ffffff;
} 

.items_layout {
padding: 0 0 0 0;
width: 580px;
}  

.item_container {
color: #212D31;
overflow: hidden;
width: 580px;
}

.item_container li {
float: left;
width: 131px;
height: 130px;
padding: 2px; 5px 5px 5px;
border-style:solid;
border-width:2px;
border-color: red;
list-style: none;
}

 .sample_header {
  font-weight: bold;
  padding: 2px 0 1px 0;
  color: #0071B5;
  }

And here’s the html:

 <div class="items_layout">
    <ul class="item_container">

                    <li><p class="sample_header">Header</p>                     
                    White background<br />(background-color: #ffffff)</li>

                    <li><p class="sample_header">Header</p>                     
                    White background<br />(background-color: #ffffff)</li>

                    <li><p class="sample_header">Header</p>                     
                    White background<br />(background-color: #ffffff)</li>

                    <li><p class="sample_header">Header</p>                     
                    White background<br />(background-color: #ffffff)</li>

                    <li><p class="sample_header">Header</p>                     
                    Grey background<br />(background-color: #b9b9b9;)<br />starts here</li>

                    <li><p class="sample_header">Header</p>                     
                    Grey background<br />Should go here</li>

                    <li><p class="sample_header">Header</p>                     
                    Grey background<br />Should go here</li>

                    <li><p class="sample_header">Header</p>                     
                    Grey background<br />Should go here</li>

                    <li><p class="sample_header">Header</p>                     
                    Back to<br /> white background here</li>

                    <li><p class="sample_header">Header</p>                     
                    Back to<br /> white background here</li>
    </ul>
</div>

I appreciate any help or thoughts on this.
Thanks!

  • 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-28T02:27:28+00:00Added an answer on May 28, 2026 at 2:27 am

    A simple iterate over all li elements and set the background seems to be the easier. You may need to call this method whenever you add/remove elements from the list.

    DEMO here.

    JS Function:

    var isWhite = true;
    $('ul.item_container > li'). each (function (index) {
        isWhite = (index != 0 && index % 4 ==0)? !isWhite:isWhite;
        if(isWhite == false) {
            $(this).removeClass('white').addClass('grey');
        } else {
            $(this).removeClass('grey').addClass('white');
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We need to make changes to an app that will cause all its URLS
I need to make sure that user can run only one instance of my
I need to make so that my application can have only one instance running
I need to make sure that the height of the header of table is
So basically I need to make feature that if you select something from first
In my C#/MVC4/Razor Application I need to make sure that user enter not only
I'm writing an application using WPF, and I need to make something that looks
I need to make a script that will take files from a user and
I need to make a database that is highly localized. For almost all entities
I need to make a tv that shows the user the channel and the

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.