So what I want is to have a smooth transition from height: 0;
to height: auto;
revealing the content inside a <div></div>
. The best method I've seen so far is using the max-height
property, and with that you get this.
#menu #list {
max-height: 0;
transition: max-height 0.15s ease-out;
overflow: hidden;
background: #d5d5d5;
}
#menu:hover #list {
max-height: 500px;
transition: max-height 0.25s ease-in;
}
<div id="menu">
<a>hover over me</a>
<ul id="list">
<li>item</li>
<li>item</li>
<li>item</li>
<li>item</li>
<li>item</li>
</ul>
</div>
And this works perfectly, except the data I have inside the list is dynamic and is taken from a database. So it can have a large number of items or quite few. I can't set an absurdly high number as the max-height
because while that would work fine for a large number of items, it would be instantaneous for few items (transitioning the max-height
from 0px
to 99999px
would not show animation if there are only 3 items). Apart from the number of items, they would also be displayed differently on different devices, so setting the max-height
by the number of items using JavaScript would also be quite difficult. While this solution is good, it isn't quite perfect.
Is there a solution that would work if there are a large number of items as well as if there were a few? A pure CSS solution would be simply perfect! But if that isn't possible, then I'm alright with a JavaScript one.