It seems to require a bit of a hack and there are several possibilities.
Have you considered using React? I need something similar for a project I’m about to start (swiping out divs) and I’m planning to try using a transition library, probably this one - https://github.com/reactjs/react-transition-group
overflow: hidden
height: <reduces from original height to 0px>
padding-top: 0px
padding-bottom: 0px
margin-top: <reduces from original value to 0px>
margin-bottom: <reduces from original value to 0px>
and at the end of the animation, it sets the element to