How do I make an animation where a div goes from height: 0px; to [its regular height]?

one of my divs has a height that depends on the content inside it, and i want to make an animation for it to start out with a height: 0px; and then expand to have its regular height. How could this be done using @keyframes?
14 Replies
Kivory
KivoryOP2w ago
Obviously this is a start:
@keyframes expand{
0%{
height: 0px;
}
to{
height: [something here];
}
}
@keyframes expand{
0%{
height: 0px;
}
to{
height: [something here];
}
}
ἔρως
ἔρως2w ago
i think you want auto
Kivory
KivoryOP2w ago
i tried doing that, and that does achieve the desired visual effect, but it still has it so the div takes up the auto height compared to the rest of the content around it don't know if i explained that very well
ἔρως
ἔρως2w ago
you can limit the height, you don't have to use auto
Kivory
KivoryOP2w ago
well i'd like to not have it limited as the height can depend on the content inside unless i misunderstood
ἔρως
ἔρως2w ago
i think i get you you want something like max-content?
Kivory
KivoryOP2w ago
tried, same problem as auto visually it does the job, but the problem is that it doesn't push the stuff around it, which is part of the intended effect like i want it to not have any size at all, then it appears in the middle and pushes all the things above it upwards as it increases the height
ἔρως
ἔρως2w ago
you might want to use the <details> tag instead
~MARSMAN~
~MARSMAN~2w ago
you want something like this behavior?
Chris Bolson
Chris Bolson2w ago
You could also use a grid row animation to go from 0fr to 1fr
Kivory
KivoryOP2w ago
might've figured it out. used max-height instead of height, and made it go from 0 to a size much larger than my div is ever gonna get, as recommended by someone on stack overflow unsure if this is the most optimal approach yes pretty much
~MARSMAN~
~MARSMAN~2w ago
The codepen Chris shared is what I use as well Give it a shot, it's a really good solution.

Did you find this page helpful?