1

I need when last child's color is yellow need to add :nth-last-child(2) as RED background color without JavaScript

td, th {
  border: 1px solid #dddddd;
  text-align: left;
  padding: 8px;
}
tr th[style='color: yellow;']:nth-child(2) {
  background-color: red;
}
<table>
  <tr>
    <th>Company</th>
    <th>Contact</th>
    <th class="last" style="color: yellow;">Country</th>
  </tr>
  <tr>
    <td>Alfreds Futterkiste</td>
    <td>Maria Anders</td>
    <td>Germany</td>
  </tr>
</table>
  • 1
    This is impossible to do with CSS currently, given the way that it is parsed in browsers. You can only go forward down the document tree, you can't go back up the DOM based on any criteria – Zach Jensz Mar 17 '22 at 06:45
  • See https://stackoverflow.com/questions/1817792/is-there-a-previous-sibling-selector – Zach Jensz Mar 17 '22 at 06:54

0 Answers0