I have an HTML table with odd rows colored gray.
Some rows are hidden with <tr style="display: none">
and that creates the possibility for 2 consecutively displayed rows to be of the same color, as seen in the snipped below:
.table thead th{
background: #f6f6f6;
}
.table>tbody>tr:nth-of-type(even)>td {
background: #f6f6f6;
}
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"/>
<table class="table">
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr>
<td>John</td>
<td>Doe</td>
<td>[email protected]</td>
</tr>
<tr>
<td>Jane</td>
<td>Doe</td>
<td>[email protected]</td>
</tr>
<tr style="display: none">
<td>Mary</td>
<td>Moe</td>
<td>[email protected]</td>
</tr>
<tr>
<td>July</td>
<td>Dooley</td>
<td>[email protected]</td>
</tr>
</tbody>
</table>
That means that I need a way to select all rows that are odd and do not have style="display: none"
.
How can I achieve this result?
2
Answers
This is impossible with CSS only. CSS works with the DOM structure and not with the elements displayed. So you will need JS to write this logic.
You can loop each element and keep track of the visible rows.
Using the new
:nth-child(An + B of S)
syntax. In your caseDemo: