table, td, th {
border: 1px solid black;
}
table {
width: 100%;
}
th {
height: 50px;
}
td:last-child {
text-align: right;
}
<table>
<tr>
<th idx="0"><div>Firstname</div></th>
<th idx="1"><div>Lastname</div></th>
<th idx="2"><div>Savings</div></th>
</tr>
<tr>
<td>Peter</td>
<td>Griffin</td>
<td>$100</td>
</tr>
<tr>
<td>Lois</td>
<td>Griffin</td>
<td>$150</td>
</tr>
<tr>
<td>Joe</td>
<td>Swanson</td>
<td>$300</td>
</tr>
<tr>
<td>Cleveland</td>
<td>Brown</td>
<td>$250</td>
</tr>
</table>
Run codeHide resultI need if the title of the Savings table , then I need to target idx = 2 using the css attribute selector, for example table th[idx="2"] {text-align: right;}, but the problem here is the header values are variable. This value, that is, the savings in the header can be anywhere in the header, so I need to choose based on the values. Any CSS based solutions? If not from CSS, maybe JavaScript ?
Many thanks.
source
share