Languages
[Edit]
EN

CSS - hover one div to change another div element (multiple divs)

0 points
Created by:
DrJoystick
315

In this article, we would like to show you how to change multiple div elements after you hover another div using CSS.

Quick solution:

.div1:hover ~ .div2 {
  background: yellow;
}

Note:

With this solution, the style will be applied to all div elements with class="div2".

Preview:

CSS - hover one div to change another div element (multiple divs)
CSS - hover one div to change another div element (multiple divs)

 

Practical example

In this section, we present how to change the background of the div elements (div2) that are separated by other elements using ~ operator.

Runnable example:

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<head>
  <style>

    div {
      width: 100px;
      border: 2px solid black;
      border-radius: 5px;
      margin-right: 10px;
      text-align: center;
      line-height: 100px;
    }

    /*  required  */
    .div1:hover ~ .div2 {
      background: yellow;
    }

  </style>
</head>
<body style="display: flex;">
  <div class="div1">Hover me!</div>
  <div>other element</div>
  <div class="div2">div2</div>
  <div class="div2">div2</div>
</body>
</html>

Related posts

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join