I have a table and I want to color every other row, except the rows that have the noColor
class, and on top of that, I don’t want those rows to count towards the 2n
part of the CSS.
HTML:
<table>
<tr> <!-- normal color -->
<td>whatever</td>
</tr>
<tr> <!-- css color -->
<td>whatever</td>
</tr>
<tr class="noColor"> <!-- normal color -->
<td>whatever</td>
</tr>
<tr class="noColor"> <!-- normal color -->
<td>whatever</td>
</tr>
<tr class="noColor"> <!-- normal color -->
<td>whatever</td>
</tr>
<tr> <!-- again normal color because the rows before shouldn't count -->
<td>whatever</td>
</tr>
</table>
CSS:
<style>
table tr:nth-child(2n) td{
background-color: rgba(219, 219, 219, 0.75) !important;
}
</style>
I tried changing my css to this:
table tr:not(.noColor):nth-child(2n) td{
background-color: rgba(219, 219, 219, 0.75) !important;
}
/* also tried this but the result is the same*/
table tr:nth-child(2n):not(.noColor) td{
background-color: rgba(219, 219, 219, 0.75) !important;
}
This prevents the color change for the rows, but it counts them as part of the 2n
.
1
What you’re trying to accomplish could be done using :nth-child’s of <selector>
syntax.
So in your case it should be:
table tr:nth-child(2n of table tr:not(.noColor)) td {
background-color: rgba(219, 219, 219, 0.75) !important;
}
Documentation: https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-child#the_of_selector_syntax