Firstly: The issue arises when you change the tr
on hover
but modify the td
s with the slider, causing the original background-color
of the table row to be obscured by the new color applied to the cells. To rectify this, define the hover effect for the cells as follows:
#indexOverviewTable tr:hover td { ... }
Moreover, the CSS styles are being overridden by inline styles set using JavaScript. Therefore, it is recommended to define the styles with CSS for a class (e.g., body.dark
) and then toggle that class with JavaScript.
Here is an example of CSS implementation:
.dark #indexOverviewTable th {
background-color: #2a0059;
}
Additionally, defining a background-color
for #indexOverviewTable tr.header
is unnecessary as it is not visible in this instance - the th
elements are covering it.
Here is a working example: (I simplified by removing :nth-child(even) and :nth-child(odd) as it was only one row)
function CheckSlider() {
var slider = document.getElementById("colorSlider");
if (slider.checked == true) {
document.body.classList.add('dark');
}
else {
document.body.classList.remove('dark');
}
}
body {
background-color: whitesmoke;
}
body.dark {
background-color: #2e2e2e;
}
#indexOverviewTable {
padding-top: 1em;
border: 2px solid white;
font-family: Cenury Gothic;
}
#indexOverviewTable th {
background-color: blue;
color: white;
font-size: 115%;
}
.dark #indexOverviewTable th {
background-color: #2a0059;
}
#indexOverviewTable td {
background-color: white;
color: black;
}
.dark #indexOverviewTable td {
background-color: #474747;
color: whitesmoke;
}
#indexOverviewTable tr:hover td {
background-color: #999999;
}
.switch {
position: relative;
display: inline-block;
width: 60px;
height: 34px;
}
...
<label class="switch">
<input type="checkbox" id="colorSlider" onclick="CheckSlider()">
<span class="slider round"></span>
...
</table>