Imagine having a table where rows can be dynamically assigned classes such as .hidden
, which hide those rows using CSS. The rows are styled with alternating colors, like this:
tr:nth-child(even) {
background-color: $light-grey;
}
But here's the catch - I want every even row that is not hidden to still have shading. This means hidden rows should not be included when applying the :nth-child(even)
rule in order to maintain a consistent pattern. I tried the following code but it didn't achieve the desired result.
tr:not(.hidden):nth-child(even) {
background-color: $light-grey;
}
The issue is that :nth-child()
refers to the original row indices, rather than considering the selection scope specified by tr:not(.hidden)
. It's like the two rules are stacked on top of each other without interacting properly.
Is there a concept similar to :nth-of-scope/selection()
(or maybe just :nth()
) in CSS? Are there any alternatives or workarounds available?
Or do I need to turn to Javascript for help?
(Just to clarify, I also have the option of using jQuery)