CSS - hover one div to change another

0 points
Created by:

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;


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


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>

      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;

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


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:

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.

❤️💻 🙂