Animating height in the browser is one of those things that sounds simple but quickly turns messy. CSS doesn’t transition well between height: 0 and height: auto, which is exactly what you need for expand/collapse UI patterns like accordions, dropdowns, or dynamic content blocks.
In this article, we’ll break down a reusable Vue 3 Transition Component that solves this problem cleanly and globally—so you can plug it in anywhere and get smooth expand/collapse animations.
Originally published on Medium.
The Problem: Why height: auto Won’t Animate
CSS transitions require explicit numeric values. But auto isn’t a number—it’s computed dynamically by the browser. So this won’t work:
height: 0 → height: auto ❌
To animate height properly, we need to:
- Measure the element’s actual height
- Animate from
0px→<measured height> - Reset back to
autoafter animation completes
That’s exactly what this component does.
Core Idea of the Component
We leverage Vue’s <transition> hooks:
enter→ when element is insertedafterEnter→ after expand animation completesleave→ when element is removed
These hooks give us full control over DOM styles during animation.
The Component Code (Explained)
Here’s the template from our component:
<template>
<transition
name="expand"
@enter="enter($el)"
@after-enter="afterEnter($el)"
@leave="leave($el)"
>
<slot />
</transition>
</template>
Let’s break this down.
Vue provides a built-in <transition> component that allows you to animate elements when they enter the DOM and leave the DOM. Instead of manually adding/removing classes, Vue gives us lifecycle hooks like enter, after-enter, leave, etc. We are using these hooks to control the animation.
And we are using Vue slots for making this fully reusable. For anyone who is not familiar with slots, <slot /> is one of the most powerful concepts in Vue. It acts like a placeholder for whatever content you pass into this component. Example usage:
<TransitionExpand>
<div v-if="isOpen">
Hello, I will expand!
</div>
</TransitionExpand>
Now writing the animation logic in script setup. I am using TypeScript, but if you are using JavaScript that’s totally fine—just ignore the types.
We need to define three functions: enter, afterEnter, and leave. As their names suggest, these functions define what to do when the element enters the DOM, what changes to perform after it enters, and finally the actions we need to perform once it leaves the DOM.
Function 1: enter() — When Element Appears
Like I said before, this function runs when the element is inserted into the DOM (i.e., when v-if becomes true).
function enter(element: HTMLElement) {
const width = getComputedStyle(element).width
element.style.width = width
element.style.position = 'absolute'
element.style.visibility = 'hidden'
element.style.height = 'auto'
const { height, padding } = getComputedStyle(element)
element.style.removeProperty('position')
element.style.removeProperty('visibility')
element.style.removeProperty('width')
element.style.height = '0'
element.style.margin = '0'
element.style.padding = '0'
// Force repaint to make sure the
// animation is triggered correctly.
// oxlint-disable-next-line no-unused-expressions
getComputedStyle(element).height
// Trigger the animation.
// We use `requestAnimationFrame` because we need
// to make sure the browser has finished
// painting after setting the `height`
// to `0` in the line above.
requestAnimationFrame(() => {
element.style.height = height
element.style.padding = padding
})
}
Capture the width
const width = getComputedStyle(element).width
We store the width because when we temporarily change styles, layout might break—and fixing width ensures stable measurement.
Prepare for measurement
element.style.width = width
element.style.position = 'absolute'
element.style.visibility = 'hidden'
element.style.height = 'auto'
We make the element absolute so it gets removed from normal layout, set visibility to hidden so the user doesn’t see the element, and set height to auto so the browser calculates the height for us. In the next line, const { height, padding } = getComputedStyle(element) we capture the height.
Reset styles to start animation
element.style.removeProperty('position')
element.style.removeProperty('visibility')
element.style.removeProperty('width')
element.style.height = '0'
element.style.margin = '0'
element.style.padding = '0'
This brings the element back to normal flow, collapses it to height: 0, and removes the spacing. This becomes our starting point of the animation.
Force reflow
getComputedStyle(element).height
This line looks useless—but it’s critical.
It forces the browser to apply all style changes immediately. Without this, the browser might skip the animation and everything could jump directly to the final state.
Animate to full height
requestAnimationFrame(() => {
element.style.height = height
element.style.padding = padding
})
We wait for the next frame, then set the height to the actual value and restore padding.
Function 2: afterEnter() — Cleanup After Animation
function afterEnter(element: HTMLElement) {
element.style.height = 'auto'
}
Once animation finishes, we reset height to auto. The reason we do this is that future content changes should adjust normally—a fixed height would break responsiveness.
Function 3: leave() — When Element Disappears
This runs when v-if becomes false.
function leave(element: HTMLElement) {
const { height, padding } = getComputedStyle(element)
element.style.height = height
element.style.padding = padding
// Force repaint to make sure the
// animation is triggered correctly.
// oxlint-disable-next-line no-unused-expressions
getComputedStyle(element).height
requestAnimationFrame(() => {
element.style.height = '0'
element.style.margin = '0'
element.style.padding = '0'
})
}
This is very similar to the enter function but just reversed: we capture the current state, explicitly set the height and padding so animation starts from the exact visible state, force a reflow again with getComputedStyle(element).height (same reason as before), and finally animate the collapse in the next frame by setting everything to 0. This creates the smooth collapse effect.
CSS That Makes It Smooth
.expand-enter-active,
.expand-leave-active {
transition:
height 0.3s ease-in-out,
opacity 0.3s ease-in-out,
margin 0.3s ease-in-out;
overflow: hidden;
}
This defines what properties to animate, the duration, and easing.
.expand-enter,
.expand-leave-to {
height: 0;
opacity: 0;
margin: 0;
}
This defines the initial and final collapse state.
Performance Optimisations
<style lang="scss" scoped>
* {
will-change: height;
backface-visibility: hidden;
}
</style>
will-change→ tells the browser to prepare for animationbackface-visibility→ reduces rendering glitches
We have marked it scoped and used * to apply this to everything in the component.
Final Code & Usage Example
<script lang="ts" setup>
function enter(element: HTMLElement) {
const width = getComputedStyle(element).width
element.style.width = width
element.style.position = 'absolute'
element.style.visibility = 'hidden'
element.style.height = 'auto'
const { height, padding } = getComputedStyle(element)
element.style.removeProperty('position')
element.style.removeProperty('visibility')
element.style.removeProperty('width')
element.style.height = '0'
element.style.margin = '0'
element.style.padding = '0'
// Force repaint to make sure the
// animation is triggered correctly.
// oxlint-disable-next-line no-unused-expressions
getComputedStyle(element).height
// Trigger the animation.
// We use `requestAnimationFrame` because we need
// to make sure the browser has finished
// painting after setting the `height`
// to `0` in the line above.
requestAnimationFrame(() => {
element.style.height = height
element.style.padding = padding
})
}
function afterEnter(element: HTMLElement) {
element.style.height = 'auto'
}
function leave(element: HTMLElement) {
const { height, padding } = getComputedStyle(element)
element.style.height = height
element.style.padding = padding
// Force repaint to make sure the
// animation is triggered correctly.
// oxlint-disable-next-line no-unused-expressions
getComputedStyle(element).height
requestAnimationFrame(() => {
element.style.height = '0'
element.style.margin = '0'
element.style.padding = '0'
})
}
</script>
<template>
<transition
name="expand"
@enter="enter($el)"
@after-enter="afterEnter($el)"
@leave="leave($el)"
>
<slot />
</transition>
</template>
<style lang="scss">
.expand-enter-active,
.expand-leave-active {
transition:
height 0.3s ease-in-out,
opacity 0.3s ease-in-out,
margin 0.3s ease-in-out;
overflow: hidden;
}
.expand-enter,
.expand-leave-to {
height: 0;
opacity: 0;
margin: 0;
}
</style>
<style lang="scss" scoped>
* {
will-change: height;
backface-visibility: hidden;
}
</style>
Usage:
<template>
<button @click="isOpen = !isOpen">
Toggle
</button>
<TransitionExpand>
<div v-if="isOpen">This content expands smoothly</div>
</TransitionExpand>
</template>
Final Understanding
Here’s the full mental model:
- Measure actual height (hidden)
- Collapse element to
0 - Force browser to apply styles
- Animate to full height
- Reset to
autoafter animation
Why This Pattern is Powerful
- Works with any dynamic content
- Fully reusable via
<slot /> - Gives smooth, professional UI
- No external libraries needed
Once you understand this, you’ve basically unlocked a core frontend animation technique used in real-world production apps.
Thanks for reading—hope you learned something new. If you liked this article, please consider following me; I’ll be writing more such articles on Vue, front-end, and back-end. If you have any thoughts, share them in the comments, and see you next time.



