Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (40.7k points)

I think this is a very basic question but I'm not sure how it can be done.

What I want to do is when a certain div hovers, it'd affect the properties of another div.

For example, in this simple example, when you hover over #cube it changes the background-color but what I want is that when I hover over #container, #cubeis affected.

div {

  outline: 1px solid red;

}

#container {

  width: 200px;

  height: 30px;

}

#cube {

  width: 30px;

  height: 100%;

  background-color: red;

}

#cube:hover {

  width: 30px;

  height: 100%;

  background-color: blue;

}

<div id="container">

  <div id="cube">

  </div>

</div>

1 Answer

0 votes
by (20.3k points)

If the cube is directly inside the container then use the code given below:

#container:hover > #cube { background-color: yellow; }

But, If cube is next to (after containers closing tag) the container then try this:

#container:hover + #cube { background-color: yellow; }

If the cube is somewhere inside the container then use this:

#container:hover #cube { background-color: yellow; }

If the cube is a sibling of the container then try using the code given below:

#container:hover ~ #cube { background-color: yellow; }

Browse Categories

...