I have two HTML tables on my page with two totally different styles. I don’t want the style of first HTML table affecting the style of second HTML table, but somehow as soon as I define the style of second HTML table, the first HTML table gets affected too. below are the tables:
<table class="table table-bordered table-responsive table-hover">
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
</table>
<table class="aTable">
<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>Centro comercial Moctezuma</td>
<td>Francisco Chang</td>
<td>Mexico</td>
</tr>
</table>
stylesheet:
.table tr:nth-child(even) {
background: #FFF;
}
.table tr:nth-child(odd) {
background: #D8EBFB;
}
.table tr:first-child {
background: #add8e6;
}
.aTable{
width:85%;
}
.aTable th, td {
border: 1px solid black;
background-color: #DDD;
}
below is how the table look like with above style sheet:
The bottom table is fine, but top table start showing the vertical lines once I added the aTable stylesheet. Also, the bottom disappears too. If I remove the aTable stylesheet, the top table looks like this with no vertical lines.
How can I keep the top table as it is and bottom table with .atable stylesheet without affecting the tables with each other styles. I want the bottom table to look like this:
any help is appreciated.
2
Answers
Here is your mistake
.aTable th, td
this line in the style will set to alltd
s.Should be:
you just need to specify which table td you want to apply styling for.
Use
.aTable td
instead of just usingtd
as it will be applied for all td tags within your HTML