I have a table displaying a tree structure (super- and subcategories). When a user clicks on a supercategory, the display property of the child elements is sent.
Now I want to add an alternating background color for every second row of the table, but, of course, only consider those that are currently visible. The following is a simplified structure example:
<table>
<tr data-level="0"><td>Super 1</td></tr>
<tr class="hide" data-level="1"><td>Sub 1</td></tr>
<tr data-level="0"><td>Super 2</td></tr>
<tr class="hide" data-level="1"><td>Sub 2</td></tr>
<tr class="hide" data-level="1"><td>Sub 3</td></tr>
<tr class="hide" data-level="1"><td>Sub 4</td></tr>
</table>
When the user clicks on the Super 2 element, the hide classes are removed from the child elements.
I tried several selectors, for example:
tr:nth-child(2n)
{
background-color: grey;
}
tr:visible:nth-child(2n)
{
background-color: grey;
}
tr:not(.hide):nth-child(2n)
{
background-color: grey;
}
I hope I understand what I want to do.
Is this possible with CSS or should I write a JS script that recounts even and odd lines whenever something changes? Thanks in advance for any tips!