Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Transition - Show div smoothly

Here's the idea: A div on top which is hidden by default and will slide down, pushing other content, by the click of a button. Another click will slide the div to be hidden again, raising the other content.

HTML

<div id="topDiv" class="hidden">
    Some content<br>
    More very complex content
</div>
<button id="thebutton" onclick="toggle('topDiv');">Toggle the div</button>
<div id="bottomDiv">
    Some content<br>
    More content!<br>
    Even more content!<br>
</div>

CSS

div.hidden {
    height: 0px;
}
div {
    height: 400px;
    transition: height 0.5s;
}

Javascript

function toggle(someId) {
    var someElem = document.getElementById(someId);
    someElem.className = someElem.className == 'hidden' ? '' : 'hidden';
}

The issue here is that the content of the div that I want to be hidden is shown. If I alter div.hidden to have display: none; or visibility: hidden; then I lose the "sliding" effect.

I would like to have a solution that does not use jQuery.

like image 818
William Avatar asked Dec 02 '25 06:12

William


1 Answers

Add overflow:hidden on the div.

like image 179
Niels Keurentjes Avatar answered Dec 03 '25 19:12

Niels Keurentjes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!