I have a table which has a hover effect on each TR.
Is it possible to have the hover effect apply to the next TR under another based on a class?
For example:
<tr>
<td>text</td>
<td>more text</td>
</tr>
<tr>
<td>text</td>
<td>more text</td>
</tr>
<!-- have this TR and the one below it have the hover effect, but not the ones above ? -->
<tr>
<td>text</td>
<td>more text</td>
</tr>
<tr>
<td>text</td>
<td>more text</td>
</tr>
Assuming I understand what you want then no you can’t do that as CSS doesn’t work upwards. It can only work downwards on descendants or subsequent siblings. It cannot make changes to parent elements via its child’s actions.
You would need to use js if you want to make changes above an element in the DOM.
That’s not what you asked for. You said you wanted to hover the parent and change the colour of the children.
e.g.
tbody:hover tr{background:red}
However that won’t be compatible with your initial request because trs will only now be siblings within each tbody block.
I think we need a clear explanation of the process you want to achieve. If for example you have multiple rows in a table and you want to change the background of not only the next row in a table but also the previous row then you will need to script it.
I am trying to apply a background on hover for multiple rows in a table. Doing it using siblings is not working as when the second row is hovered over, the one above it does not have a background. I tried the method of wrapping them in a TBODY so all TRs in the TBODY have a background when hovered over.
Basically from what I’ve gathered from above, what I would like to do is to apply a background colour on all TRs inside a TBODY with a specific class on hover.
No that is not correct as I have given you the code for that already and you said it was not what you wanted ?
tbody:hover tr {background:red}
You can hover two (or three, or four etc…) rows together using the code already offered at the start of the thread but only from the current row downwards. You can’t go back up without a script unless you have fixed height rows and then you could absolutely place a background but would be very hacky and likely to break.