Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Style child element when hover on parent

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.

like image 431
Rajat Gupta Avatar asked Aug 27 '11 20:08

Rajat Gupta


People also ask

How do you target a child to a parent in CSS?

There is currently no way to select the parent of an element in CSS, at least not a way that works across all browsers. If there was a way to do it, it would be in either of the current CSS selectors specs: Selectors Level 3 Spec.

How do I style a parent in CSS?

The element>element selector is used to select elements with a specific parent. Note: Elements that are not directly a child of the specified parent, are not selected.

How do you make elements visible on hover?

To display div element using CSS on hover a tag: First, set the div element invisible i.e display:none;. By using the adjacent sibling selector and hover on a tag to display the div element.


2 Answers

Yes, you can definitely do this. Just use something like

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

According to this page it's supported by all major browsers.

like image 124
jtbandes Avatar answered Sep 19 '22 18:09

jtbandes


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>
like image 33
Iqbal Pasha Avatar answered Sep 18 '22 18:09

Iqbal Pasha