Why in this CSS code the background color in (#customers tr:hover) selector is not working for the first tr element which contains the th element and have green background.
#customers {
font-family: Arial, Helvetica, sans-serif;
border-collapse: collapse;
width: 100%;
}
#customers td,
#customers th {
border: 1px solid #ddd;
padding: 8px;
}
#customers tr:nth-child(even) {
background-color: #f2f2f2;
}
#customers tr:hover {
background-color: #ddd;
}
#customers th {
padding-top: 12px;
padding-bottom: 12px;
text-align: left;
background-color: #04aa6d;
color: white;
}
<h1>A Fancy Table</h1>
<table id="customers">
<tr>
<th>Company</th>
<th>Contact</th>
<th>Country</th>
</tr>
<tr>
<td>Alfreds Futterkiste</td>
<td>Maria Anders</td>
<td>Germany</td>
</tr>
<tr>
<td>Berglunds snabbköp</td>
<td>Christina Berglund</td>
<td>Sweden</td>
</tr>
<tr>
<td>Centro comercial Moctezuma</td>
<td>Francisco Chang</td>
<td>Mexico</td>
</tr>
<tr>
<td>Ernst Handel</td>
<td>Roland Mendel</td>
<td>Austria</td>
</tr>
<tr>
<td>Island Trading</td>
<td>Helen Bennett</td>
<td>UK</td>
</tr>
<tr>
<td>Königlich Essen</td>
<td>Philip Cramer</td>
<td>Germany</td>
</tr>
<tr>
<td>Laughing Bacchus Winecellars</td>
<td>Yoshi Tannamuri</td>
<td>Canada</td>
</tr>
<tr>
<td>Magazzini Alimentari Riuniti</td>
<td>Giovanni Rovelli</td>
<td>Italy</td>
</tr>
<tr>
<td>North/South</td>
<td>Simon Crowther</td>
<td>UK</td>
</tr>
<tr>
<td>Paris spécialités</td>
<td>Marie Bertrand</td>
<td>France</td>
</tr>
</table>
Here is the link: https://www.w3schools.com/css/tryit.asp?filename=trycss_table_fancy
When i read this code from w3schools i was expecting that all tr elements will have a background color of #ddd on hover, but it didn't work for the first one.
is there some kind of restriction on the first tr !?
I am quite new to HTML/CSS so it might be obvious, but it would be nice with some help. Thanks.
You did not declare a background color for
td, onlyth. Therefore, when hover changes the background color oftr, thethbackground color does not change. (this on top oftr, so thetrbackground color is not visible.) To change the background color of allths in atrwhen anythin the row is hovered, just include an additional selector:#customers tr:hover thHmm, I explained the issue and provided a working solution but got downvoted... Here's more examples to prove my point.
In this example, background-color is declared only for
tr:hoverIn this example,
trbackground-color is overruled by the background-color ofthandtdThis example uses higher specificity with
tr:hover thand is able to change the background-color on hover.