HTML Element Style How to - Change other list items' CSS when hovering an item








Question

We would like to know how to change other list items' CSS when hovering an item.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
ul:hover li {<!--from   w  w  w .j a v a 2  s . c  o m-->
  opacity: 0.5;
}

ul li:hover {
  opacity: 1;
}
</style>
</head>
<body>
  <ul>
    <li>Item 1</li>
    <li>Item 2</li>
    <li>Item 3</li>
    <li>Item 4</li>
    <li>Item 5</li>
  </ul>
</body>
</html>

The code above is rendered as follows: