Currently working on a project that involves the use of ul and li elements.
I need to change the background for every 3rd li element in alternating patterns. Since the li elements are generated from the backend, I do not know how many there will be.
I attempted using CSS3 child classes but was unsuccessful. Is there a way to achieve this using CSS or jQuery?
.list-item li
{
width:30%;
display:inline-block;
}
<ul class="list-item">
<li>list item 1</li>
<li>list item 2</li>
<li>list item 3</li>
<li>list item 4</li>
<li>list item 5</li>
<li>list item 6</li>
<li>list item 7</li>
<li>list item 8</li>
<li>list item 9</li>
</ul>
The goal is to have the same background color for list items 1, 2, 3, 7, 8, and 9, and a different color for list items 4, 5, and 6.
In the provided image, you can see that a table is used to achieve this effect by utilizing nth-child(even)/(odd) properties. Due to limitations, a table cannot be used in this case, so the solution must involve manipulating the li elements.