I am trying to revise for my exam on HTML, JavaScript and CSS3; and I am a little confused at CSS selectors and which take priority.
I have the following CSS:
table { border: 1px solid black; }
tr:nth-child(odd) { background-color: red; }
tr td:nth-of-type(even) { background-color: blue; }
td { background-color: green; }
I thought that whatever comes last takes priority, so in my opinion all cells in the table would be green.
However the even cells are still blue, as per the nth-of-type selector. Even when I put this to the top and remove the green td line, the blue is still shown in the middle with only the odd cells showing in red.
Can someone explain why the nth-of-type seems to take precedent over everything else?
Here is the example:
table { border: 1px solid black; }
tr:nth-child(odd) { background-color: red; }
tr td:nth-of-type(even) { background-color: blue; }
td { background-color: green; }
<table style="width: 100%;">
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
<td>80</td>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
<td>80</td>
</tr>
</table>