Suppose I have the HTML below. Is there any way to prevent the background-color cascading into the inner table? I.E. leaving the HTML exactly as it is and only modifying the CSS class can I achieve this:

.myClass table {
background-color: red;
}
<body>
<div class="myClass">
<table>
<tr>
<td>
<div>
<table>
<tr>
<td>Hello</td>
</tr>
</table>
</div>
</td>
<td>World</td>
</tr>
</table>
</div>
</body>
I created a code snippet for you:
.myClass table td:nth-child(2) {
background-color: red;
}
<body>
<div class="myClass">
<table>
<tr>
<td>
<div>
<table>
<tr>
<td>Hello</td>
</tr>
</table>
</div>
</td>
<td>World</td>
</tr>
</table>
</div>
</body>
Basically you tell CSS to make the background of the 2nd TD in the table the red background:
.myClass table td:nth-child(2){
background-color:red;
}
More info on nth-child() here.
Use this instead:
.myClass > table { background: red; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With