3

I have the next html structure:

<div class="parent">
    <div class="child"></div>
</div>

And css:

.parent {
    width: 100px;
    height: 100px;
}

.parent:hover {
    background-color: red;
}

.child {
    width: 50px;
    height: 50px;
}

How can I change background color of child div when its parent div is hovered? Can I do it with css or I shoud use JQuery?

user190794
  • 445
  • 1
  • 4
  • 18
  • I see that many others have answered already. But if you would like to know more consider reading [This Link](https://stackoverflow.com/questions/6910049/on-a-css-hover-event-can-i-change-another-divs-styling) – Chris Aby Antony Jun 19 '18 at 09:15

5 Answers5

4

Here you go with a solution

.parent {
  background-color: green;
  width: 100px;
  height: 100px;
}

.parent:hover > .child{
  background-color: red;
}

.child {
  width: 50px;
  height: 50px;
}
<div class="parent">
    <div class="child"></div>
</div>

Hope this will help you.

Shiladitya
  • 12,003
  • 15
  • 25
  • 38
2

You can try this

.parent:hover .child {
   background-color: red;
}
charan kumar
  • 2,119
  • 2
  • 20
  • 26
1

Try This:

.parent:hover .child {
  background-color: blue;
}

.parent {
  width: 100px;
  height: 100px;
}

.parent:hover {
   background-color: red;
}

.parent:hover .child{
  background-color: blue;
}

.child {
  width: 50px;
  height: 50px;
}
<div class="parent"> PARENT
  <div class="child">CHILD</div>
</div>
Ehsan
  • 12,655
  • 3
  • 25
  • 44
1

If I understand you correctly, you just need to change

.parent:hover {
    background-color: red;
}

to

.parent:hover .child {
    background-color: red;
}
Prawns
  • 61
  • 6
1

Add a new rule for .parent:hover .child

Check the example below:

.parent {
    width: 100px;
    height: 100px;
    background-color:blue;
}

.parent:hover,
.parent:hover .child  {
    background-color: red;
}

.child {
    width: 50px;
    height: 50px;
}

.child {
background-color:yellow;
}
<div class="parent">
    <div class="child"></div>
</div>
souzan
  • 289
  • 1
  • 2
  • 14