Instead of targeting the nth-child
selectors in relation to the table
element, disregarding the tbody
, it is suggested to target them in relation to each tbody
. This approach provides more flexibility and control over styling.
The code snippet below caters to your current HTML structure, assuming there is an odd number of rows in the first tbody
. If this changes to an even number, adjustments in the selectors will be required.
tbody:nth-child(odd) tr:nth-child(odd) {
background-color: yellow;
}
tbody:nth-child(odd) tr:nth-child(even) {
background-color: red;
}
tbody:nth-child(even) tr:nth-child(odd) {
background-color: red;
}
tbody:nth-child(even) tr:nth-child(even) {
background-color: yellow;
}
table {
width: 100%;
max-width: 1000px;
margin-left: auto;
margin-right: auto;
border-spacing: 0px;
border-width: 1px;
border-right-style: dotted;
border-bottom-style: dotted;
border-color: green;
}
td {
padding: 10px;
}
.left {
min-width: 160px;
text-align: left;
padding-left: 10px;
-webkit-touch-callout: none;
}
.middle {
text-align: center;
margin-left: auto;
margin-right: auto;
}
.right {
min-width: 160px;
text-align: right;
padding-right: 10px;
}
.boldtitle {
color: green;
font-weight: bold;
padding: 0px;
margin: 0px;
}
tbody:nth-child(odd) tr:nth-child(odd) {
background-color: yellow;
}
tbody:nth-child(odd) tr:nth-child(even) {
background-color: red;
}
tbody:nth-child(even) tr:nth-child(odd) {
background-color: red;
}
tbody:nth-child(even) tr:nth-child(even) {
background-color: yellow;
}
td {
border-width: 1px;
border-left-style: dotted;
border-top-style: dotted;
border-color: green;
}
<table>
<tbody>
<tr class="header">
<td colspan="3" class="left">
<p class="boldtitle">Cars</p>
</td>
</tr>
<tr>
<td class="left"><a class="table_details" href="http://www.example.com">GM</a><span class="link_company">GM & </span></td>
<td class="middle">XXXXXXXXXX</td>
<td class="right">YYYYYYYYYY<br></td>
</tr>
<tr>
<td class="left"><a class="table_details" href="http://www.example.com">VW</a><span class="link_company">VW</span></td>
<td class="middle">XXXXXXXXXX</td>
<td class="right">YYYYYYYYYY<br></td>
</tr>
<tr>
<td class="left"><a class="table_details" href="http://www.example.com">BMW</a><span class="link_company">BMW</span></td>
<td class="middle">XXXXXXXXXX</td>
<td class="right">YYYYYYYYYY<br></td>
</tr>
<tr>
<td class="left"><a class="table_details" href="http://www.example.com">Citroen</a><span class="link_company">Citroen</span></td>
<td class="middle">XXXXXXXXXX</td>
<td class="right">YYYYYYYYYY<br></td>
</tr>
</tbody>
<tbody>
<tr class="header">
<td colspan="3" class="left">
<p class="boldtitle">Fruits</p>
</td>
</tr>
<tr>
<td class="left"><a class="table_details" href="http://www.example.com">Apple</a><span class="link_company">Apple</span></td>
<td class="middle">XXXXXXXXXX</td>
<td class="right">YYYYYYYYYY<br></td>
</tr>
<tr>
<td class="left"><a class="table_details" href="http://www.example.com">Banana</a><span class="link_company">Banana</span></td>
<td class="middle">XXXXXXXXXX</td>
<td class="right">YYYYYYYYYY<br></td>
</tr>
<tr>
<td class="left"><a class="table_details" href="http://www.example.com">Plump</a><span class="link_company">Plump</span></td>
<td class="middle">XXXXXXXXXX</td>
<td class="right">YYYYYYYYYY<br></td>
</tr>
</tbody>
</table>