Vue Transitions
Vue Transitions: CSS Animations with Transition and TransitionGroup
Vue transitions let you animate elements as they enter or leave the DOM, using the built-in <Transition> and <TransitionGroup> components. Vue adds and removes CSS classes at each stage of the animation wheneverv-if, v-show, or a dynamic component toggles, so you write plain CSS instead of juggling timers in JavaScript.
flowchart LR
EnterFrom["fade-enter-from"] --> EnterActive["fade-enter-active"] --> EnterTo["fade-enter-to"]
LeaveFrom["fade-leave-from"] --> LeaveActive["fade-leave-active"] --> LeaveTo["fade-leave-to"]
How Vue Transitions Work
You describe the start and end states in CSS. Vue reads the animation duration from your CSS and keeps the element in the DOM until the leave animation finishes, then removes it. Without this, a v-if would delete the element instantly, and there would be nothing left to animate.
Transition Class Lifecycle
When using <Transition name="fade">, Vue toggles six predictable CSS classes:
fade-enter-fromandfade-enter-to: Starting and ending styles for elements entering the DOM.fade-enter-active: Active class during the entire enter phase, where you settransitionproperties.fade-leave-fromandfade-leave-to: Starting and ending styles for elements leaving the DOM.fade-leave-active: Active class during the exit animation.
Transition vs TransitionGroup
<Transition> handles a single element or component. <TransitionGroup> handles a list of them, and it differs in a few ways. It renders a wrapper element only if you set the tag (for example, tag="ul"), and every child needs a unique key. The mode prop is not available on it.
Common Mistakes to Avoid
Missing
: keyinside<TransitionGroup>. Each direct child needs a unique key, or Vue can't track which items were added, moved, or removed. Avoid using the array index as the key if items can be reordered or deleted from the middle.Rendering two elements at once inside
<Transition>. It animates one element at a time. When switching between two branches, make sure only one renders at any moment, and usemode="out-in"so they don't overlap.Forgetting the
tagon<TransitionGroup>. Without it, no wrapper is rendered. Settingtag="ul"keeps your list markup semantic.Using
v-ifon multiple root nodes. A transition's content should have a single root element, or the animation may not run.
Frequently Asked Questions
What does mode="out-in" do on <Transition>?
With mode="out-in", Vue waits for the outgoing element to finish its leave animation before starting the incoming element's enter animation. This prevents two elements from jumping past each other.
Can <Transition> animate on initial page render?
Yes. Add the appear attribute (<Transition name="fade" appear>) to run the enter animation when the component first mounts.
How do I trigger JavaScript animations with libraries like GSAP?
<Transition> emits JavaScript hooks such as @enter, @before-enter, and @leave. Pass :css="false" when driving animations purely with JavaScript.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With