Effect How to - Change another element on :hover








Question

We would like to know how to change another element on :hover.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
.sibling-hover, #parent {<!--from   w w w  .ja  v a2  s. co  m-->
  cursor: pointer;
}

.sibling-hover:hover ~ .sibling-highlight {
  background-color: red;
  color: white;
}

#parent:hover .parent-highlight {
  background-color: red;
  color: white;
}
</style>
</head>
<body>
  <b>sibling method:</b>
  <div>
    <div class="sibling-hover">hover over me</div>
    <div>I do nothing</div>
    <div>I do nothing</div>
    <div class="sibling-highlight">I get highlighted</div>
  </div>
  <b>child method:</b>
  <div id="parent">
    <div>hover over me</div>
    <div>hover over me</div>
    <div>hover over me</div>
    <div class="parent-highlight">I get highlighted</div>
  </div>
</body>
</html>

The code above is rendered as follows: