Anonymous
Anonymous

Reputation: 55

Child:Hover div box affect Parent div box CSS

The goal is to affect the Parent div box using :hover via child div box.

This is what I've Done and it doesn't work. HTML:

<body>
<div id=Parent-box>
<div id=Child-box></div>
</div>
</body>

CSS:

#Parent-box{
width: 750px;
height: 400px;
margin-left: auto;
margin-right: auto;
margin-top: 25px;
border: 1px solid #000;
position: relative;
}
 
#Child-box{
z-index: 1;
width: 700px;
height: 350px;
margin-left: 25px;
margin-top: 25px;
position: absolute;
background: #339;
border: 1px solid #000;
transition: all 200ms linear;
}
 
#Child-box:hover{
height: 600px;
transition: all 200ms linear;
}
 
#Child-box:hover + #Parent-box{
height: 650px;
transition: all 100ms linear;
}

does anyone know if this is possible?

thanks :)

Upvotes: 1

Views: 520

Answers (5)

Kheema Pandey
Kheema Pandey

Reputation: 10285

well, the main problem is you have defined the height:400px on parent element and this is the real problem. In your scenario you can achieve it with pure CSS; also there are much advanced options when using jquery. Check DEMO.

CSS Code

#Parent_box{height: 400px; /*Remove height.*/}

JS Code

$(document).ready(function()
{
$("#Child_box").hover(function (){
  $(this).parent("#Parent_box").css("height", "750px;");
});
});

Upvotes: 0

Anonymous
Anonymous

Reputation: 10216

PURE CSS - DEMO (Without JavaScript or jQuery)

But you must use position: relative; for your Child-box div.

HTML

<div id=Parent-box>
<div id=Child-box></div>
</div>

CSS

#Parent-box{
width: 750px;
height: auto;
margin-left: auto;
margin-right: auto;
margin-top: 25px;
border: 1px solid #000;
position: relative;
}

#Child-box{
z-index: 1;
width: 700px;
height: 350px;
margin-left: 25px;
margin-top: 25px;
margin-bottom: 25px;
position: relative;
background: #339;
border: 1px solid #000;
transition: all 200ms linear;
}

#Child-box:hover{
padding: 125px 0px;
transition: all 200ms linear;
}

Upvotes: 5

Steven Web
Steven Web

Reputation: 1961

It's only possible to trigger the hover event on the parent element in your case!

You can do this:

#Parent-box {
    width: 750px;
    height: 400px;
    margin-left: auto;
    margin-right: auto;
    margin-top: 25px;
    border: 1px solid #000;
    position: relative;
}
#Child-box {
    z-index: 1;
    width: 700px;
    height: 350px;
    margin-left: 25px;
    margin-top: 25px;
    position: absolute;
    background: #339;
    border: 1px solid #000;
    -moz-transition: all 200ms linear;
    -o-transition: all 200ms linear;
    -webkit-transition: all 200ms linear;
    transition: all 200ms linear;
}
#Parent-box:hover  {
    height: 650px;
    -moz-transition: all 100ms linear;
    -o-transition: all 100ms linear;
    -webkit-transition: all 100ms linear;
    transition: all 100ms linear;
}

#Parent-box:hover #Child-box {
    height: 600px;
    -moz-transition: all 200ms linear;
    -o-transition: all 200ms linear;
    -webkit-transition: all 200ms linear;
    transition: all 200ms linear;
}

If you want to have a syncron hover effect then change the transitions time. I also added the vendor specific properties.

Here's DEMO

Upvotes: 0

Viswanath Donthi
Viswanath Donthi

Reputation: 1821

Actually, there is no CSS selector for selecting a parent of a child. But, there are many ways to do the same in jquery. Here's the code:

$('#Child-box').mouseenter(function(){
   $(this).parent('#Parent-box').css('height','700px');
});


$('#Child-box').mouseleave(function(){
   $(this).parent('#Parent-box').css('height','600px');
});

Upvotes: 0

Ajey
Ajey

Reputation: 8212

you can acheive it by using jquery since, you cannot select a parent div using CSS Here is the Demo

$('#Child-box').hover(

function () {
    $('#Parent-box').css({
        'height': '800px'
    });
}, function () {
     $('#Parent-box').css({
        'height': '400px'
    })  ;
});

Not perfect, but you get the idea right ?

Upvotes: 0

Related Questions