Languages
[Edit]
EN

CSS - hover one div to change another

0 points
Created by:
Paris-Bateman
504

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

Quick solution:

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

Note:

The :hover works only if the next div occurs directly after the hovered div.

Preview:

CSS - hover one div to change another
CSS - hover one div to change another

 

Practical example

In this section, we present how to change the background of the div element (div2) that is directly after the div that we hover (div1).

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 class="div2"></div>
  </body>
</html>

Note:

If there are any other elements between the div1 and div2 the solution won't work.

You can use the solution from the following article:

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
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