Changing the background color of a row in a table on mouse over is pretty simple using CSS:
.HighlightableRow:hover
{
background-color: lightgray;
}
And some HTML:
<table>
<tr class=HighlightableRow>
<td>Cell 1</td>
<td>Cell 2</td>
<td>Cell 3</td>
<tr>
<tr class=HighlightableRow>
<td>Cell 1</td>
<td>Cell 2</td>
<td>Cell 3</td>
<tr>
</table>
Occasionally, I want to highlight a pair of rows when I hover my mouse over either of them. For example, when displaying a list of work orders in a table, I would have one row with the creator, date created, urgency, etc. and the second row would have an except of the work that was requested.
Is there any way, other than using the JavaScript onmouseover/onmouseout event handlers, to create this effect like the one shown above? Preferably using CSS.
Highlight Table Row using CSS. You can use CSS without any javascript to make the row of a table highlight on hover. All it requires is that the use the pseudo class :hover to add the effect to whatever html element you choose.
When you create Web pages in HTML, you can use JavaScript functions to alter the appearance of page elements on user interaction. To highlight certain rows in a table, you can set Cascading Style Sheet declarations for these rows in their normal state and in their highlighted state.
table-active: This class applies to the hover color of the table row or cell with grey color. table-default: This class is applied to change the color to white when the default row is selected.
Use the tbody element to group rows.
tbody:hover { background:#eee;}
HTML:
<table>
<tbody>
<tr>
<td>Hello</td>
<td>World!</td>
</tr>
<tr>
<td>Hello</td>
<td>World!</td>
</tr>
</tbody>
</table>
Check out the details about "tbody", "thead" and "tfoot" for the correct usage.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With