270

How to change the style of child element when there is hover on parent element. I would prefer a CSS solution for this if possible. Is there any solution possible through :hover CSS selectors. Actually I need to change color of options bar inside a panel when there is an hover on the panel.

Looking to support all major browsers.

0

4 Answers 4

484

Yes, you can definitely do this:

.parent:hover .child { /* ... */ } 

This uses the :hover pseudoclass to restrict to elements matching .parent which are also hovered, and the descendant combinator (space) to select any descendant that matches .child.

Here's a live example:

.parent { border: 1px dashed gray; padding: 0.5em; display: inline-block; } .child { border: 1px solid brown; margin: 1em; padding: 0.5em; transition: all 0.5s; } .parent:hover .child { background: #cef; transform: scale(1.5) rotate(3deg); border: 5px inset brown; }
<div class="parent"> parent <div class="child"> child </div> </div>

Sign up to request clarification or add additional context in comments.

1 Comment

Finally learned enough CSS to know what to ask for, thanks for the help! Note that this applies to all descendants, if you just want children I think you need .parent:hover > .child?
22

Yes, you can do this use this below code it may help you.

.parentDiv { margin: 25px; } .parentDiv span { display: block; padding: 10px; text-align: center; border: 5px solid #000; margin: 5px; } .parentDiv div { padding: 30px; border: 10px solid green; display: inline-block; align: cente; } .parentDiv:hover { cursor: pointer; } .parentDiv:hover .childDiv1 { border: 10px solid red; } .parentDiv:hover .childDiv2 { border: 10px solid yellow; } .parentDiv:hover .childDiv3 { border: 10px solid orange; }
<div class="parentDiv"> <span>Hover me to change Child Div colors</span> <div class="childDiv1"> First Div Child </div> <div class="childDiv2"> Second Div Child </div> <div class="childDiv3"> Third Div Child </div> <div class="childDiv4"> Fourth Div Child </div> </div>

2 Comments

This answer would benefit from some explanation. It seems to me that there is more code here than there needs to be and its not clear at which part of the code we should be focusing.
You have an invalid property in your code, align : cente. Did you mean align: center? Also, this doesn't appear to add anything to the existing answer. It just has a bunch of extra styles. New answers should be reserved for novel solutions that haven't already been given.
3

you can use this too

.parent:hover * { /* ... */ } 

3 Comments

can you explain a little more your answer ?
@MassimoPetrus put the name of the child where the * is not sure if it works for svg files but for infline svg this works fine. You can also go as many children deep by placing a space and then the next childs name
While not technically wrong, this is dangerous/overkill, because it applies to everything, not just a specific child element. It's like wanting to paint your house blue, but using a firehose to spray paint from the street; when you're done, the house's siding (what you wanted blue) will be blue, but so will your windows, roof, lawn, car, etc...
2

While the accepted solution is correct, the hover styles are being applied to both the parent and child element. This is because the parent contains the child inside its "box".

If anyone, for whatever reason, is curious about how to apply styles to a child element only when the parent is hovered, try this:

.parent { height: 100px; width: 100px; background-color: blue; } .parent:hover .child { background-color: red; } .child { height: 100px; width: 100px; margin-left: 100px; background-color: green; pointer-events: none; /* this line does the trick */ }
<div class="parent"> <div class="child"></div> </div>

1 Comment

"the hover styles are being applied to both the parent and child element." Well, yes, that is what the question asked for. It's quite rare someone would want to style a child only when the child parent is hovered and not the child, because child elements are almost always within the flow of their parent.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.