Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Highlight two table rows on hover with CSS

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.

like image 618
Sparafusile Avatar asked Jan 06 '12 19:01

Sparafusile


People also ask

How do you highlight a table row in 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.

How do I highlight a row in a table in HTML?

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.

Which class sets the hover color for a certain table row or cell?

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.


1 Answers

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.

like image 197
Chris Avatar answered Sep 18 '22 13:09

Chris