HTML Element Style How to - Change table cell background color when hover








Question

We would like to know how to change table cell background color when hover.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
tr {<!--from  ww  w .j  a v a2 s .  c o m-->
  color: #222;
  background-color: #f0f0f0;
}

tr:hover {
  color: #fff;
  background-color: #0080ff;
}
</style>
</head>
<body>
  <table>
    <tr>
      <td>Sample</td>
      <td>Text</td>
    </tr>
  </table>
</body>
</html>

The code above is rendered as follows: