ThatGuy7 ThatGuy7 - 2 months ago 6
CSS Question

Trying to have one div slide down from behind another div on hover

I'm trying to have a div with some text that slides down another div with more text when hovered over. I'm currently having difficulty achieving this. If a good solution includes JQuery, could you guys ELI5 (I've never used JQuery before)? The current attempt laid out below has the basis of what I'm looking for. I just want there to be an animation on hover showing the additional text sliding down, instead of it suddenly appearing.

JSFiddle of my current attempt

HTML



<div class="container">
<div class="above">
<p>
Some text in the above div
</p>
</div>
<div class="below">
<p>
More text that slides down from under the above div
</p>
</div>
</div>


CSS





.container {
font-size: 2em;
box-shadow: 0px 0px 3px 1px black;
margin: 20px;
padding: 20px;
background-color: #E7E7EF;
display: block;
margin-left: auto;
margin-right: auto;
border-radius: 10px;
width: 80%;
margin-top: 50px;
-webkit-transition: 0.5s;
transition: 0.5s;
}

.below {
display: none;
-webkit-transition-duration: 0.5s;
transition-duration: 0.5s;
}

.container:hover .below {
display: block;
}

Answer

Use overflow instead of display https://jsfiddle.net/yb6vct8a/1/

.container {
    font-size: 2em;
    box-shadow: 0px 0px 3px 1px black;
    margin: 20px;
    padding: 20px;
    background-color: #E7E7EF;
    display: block;
    margin-left: auto;
    margin-right: auto;
    border-radius: 10px;
    width: 80%;
    margin-top: 50px;
    -webkit-transition: max-height 0.8s;
    -moz-transition: max-height 0.8s;
    transition: max-height 0.8s;


}

.below {
    max-height: 0;
    overflow: hidden;

    /* Set our transitions up. */
    -webkit-transition: max-height 0.8s;
    -moz-transition: max-height 0.8s;
    transition: max-height 0.8s;
}

.container:hover .below {

    max-height: 200px;


}