I have an html table where I want to, using only CSS:
- show the last td of each row when hovering on the row, and
- show the last td of each column when hovering on that column.
This is my solution for rows:
/* Hide last cell in each row by default */
tr td:last-child {
display: none;
}
/* Show last cell in row on row hover */
tr:hover td:last-child {
display: block;
}
<table>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
</table>
But for the life of me I cannot make it work for columns.
NOTE: Any method to show/hide cells is game (display/visibility, etc)
2
Answers
You can use the
:has()
rule (currently not supported by FF) to show the lasttd
in a column, if one of thetd
elements in that column id hovered. You’ll need to supply a specific rule for each column:If you have a large amount of columns, or the number of columns is dynamic, you can generate the rules for the columns using JS:
A way to do this with JS code, working on ALL browsers (FireFox doesn’t support CSS
:has()
)Less than ten lines of JS code (and 2 lines of css)