I created the following code. In this case, we thought that tr:nth-child(odd)
works as follows
- caption-> not tr
- tr-> not odd
- tr-> background red!
- tr-> not odd
- tr-> background red!
But this worked this way
- caption-> not tr
- tr-> no background red
- tr-> no background red
- tr-> background red!
- tr-> no background red
table {
border-collapse: collapse;
color: #000000;
width: 80%;
}
td,
th {
border: 1px solid #000000;
background-color: rgba(0, 0, 0, 0.3);
text-align: center;
padding: 3px;
}
th {
background-color: #31A9EE;
}
tr:nth-child(odd) {
background: red;
}
<table border="1">
<caption>caption</caption>
<tr>
<th>head1</th>
<th>head2</th>
<th>head3</th>
</tr>
<tr>
<td>1</td>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>2</td>
<td>2</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>3</td>
<td>3</td>
</tr>
</table>
Why does nth-child
not work as desired? I want to do this:
table {
border-collapse: collapse;
color: #000000;
width: 80%;
}
td,
th {
border: 1px solid #000000;
background-color: rgba(0, 0, 0, 0.3);
text-align: center;
padding: 3px;
}
th {
background-color: #31A9EE;
}
tr:nth-child(odd) {
background: red;
}
<table border="1">
<caption>caption</caption>
<tr>
<th>head1</th>
<th>head2</th>
<th>head3</th>
</tr>
<tr style="background: red;">
<td>1</td>
<td>1</td>
<td>2</td>
</tr>
<tr style="background: transparent;">
<td>2</td>
<td>2</td>
<td>2</td>
</tr>
<tr style="background: red;">
<td>3</td>
<td>3</td>
<td>3</td>
</tr>
</table>