Im trying to apply a class to every 3 list items beginning with the 4th (3n+4). This works fine using CSS when all list items are visible.
Unfortunately, when some list items are hidden (display: none), the nth-child selector applies to those hidden list items as well.
I need the CSS to apply to only visible list items, so I did some research and tried using jQuery and the visible selector to achieve the desired result. This didn't work either.
Please see my current code below for an example.
Any help will be much appreciated. Thank you
http://codepen.io/anon/pen/xuqwf
HTML
<ul>
<li>1</li>
<li style="display: none;">2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li style="display: none;">6</li>
<li>7</li>
<li>8</li>
</ul>
CSS
ul li {
background: yellow;
border: 1px orange solid;
margin: 5px;
}
ul li:nth-child(3n+4) {
background: green;
}
.clear {
background: red;
}
jQuery (Tried and failed)
$('ul li:visible:nth-child(3n+4)').addClass('clear');