Description: `x-transition` allows you to add smooth enter and leave animations for elements whose visibility is controlled by `x-show` or `x-if`. It works by applying CSS classes at different stages of the transition, which your CSS then uses to animate properties like opacity, transform, or size.
x-show or x-if (to control visibility):
These directives are prerequisites for using `x-transition`. Transitions are triggered when the element's visibility state changes.
x-show="condition": Toggles the element's `display` style between `none` and its original value. The element always remains in the DOM.x-if="condition": Conditionally adds or removes the element from the DOM. Transitions apply as the element is inserted or just before it's removed.Alpine provides directives to specify CSS classes for different phases of an element's appearance (enter) and disappearance (leave).
x-transition:enter="classes-for-duration-of-enter": These classes are applied for the entire duration of the "enter" transition. This is where you typically define the CSS `transition` properties (e.g., Tailwind's `transition ease-out duration-300`).x-transition:enter-start="classes-at-start-of-enter": CSS classes defining the element's initial state before it starts entering (e.g., `opacity-0 transform scale-95`).x-transition:enter-end="classes-at-end-of-enter": CSS classes defining the element's final state after it has fully entered (e.g., `opacity-100 transform scale-100`).x-transition:leave="classes-for-duration-of-leave": Similar to `enter`, but for the "leave" transition (e.g., `transition ease-in duration-200`).x-transition:leave-start="classes-at-start-of-leave": CSS classes defining the element's state as it begins to leave (e.g., `opacity-100 transform scale-100`).x-transition:leave-end="classes-at-end-of-leave": CSS classes defining the element's final state after it has fully left (e.g., `opacity-0 transform scale-95`).Example:
<div x-show="isOpen"
x-transition:enter="transition ease-out duration-300"
x-transition:enter-start="opacity-0 transform -translate-y-2"
x-transition:enter-end="opacity-100 transform translate-y-0"
x-transition:leave="transition ease-in duration-150"
x-transition:leave-start="opacity-100 transform translate-y-0"
x-transition:leave-end="opacity-0 transform -translate-y-2">
Dropdown Content
</div>
Alpine offers shorthands for common transitions, which apply a default set of fade and scale animations. You can modify these with specific properties.
x-transition: Applies a default fade and scale.x-transition.opacity: Transitions only opacity.x-transition.scale: Transitions only scale. You can combine (e.g., x-transition.opacity.scale).x-transition.duration.500ms: Sets the enter duration to 500ms (leave duration will be shorter by default).x-transition:enter.duration.500ms: Customizes only the enter duration.x-transition.leave.duration.200ms: Customizes only the leave duration.Example using shorthand:
<div x-show="isOpen" x-transition.opacity.duration.300ms>
Fades in and out.
</div>
AlpineJS only adds/removes CSS classes. Your actual CSS (e.g., Tailwind utility classes or custom CSS) must define the transition property itself (e.g., transition: opacity 0.3s ease; or Tailwind's transition duration-300 ease-out) and the styles for the start/end states (e.g., opacity: 0; and opacity: 1;). Without these CSS rules, the class changes won't produce any visual animation. Python developers accustomed to libraries that handle all animation aspects internally might overlook this CSS dependency.
x-transition with x-if requires careful placement:
When an element is controlled by x-if, it's completely added to or removed from the DOM. For transitions to work correctly, the x-transition directives must be on the element itself, or on its direct child if x-if is on a <template> tag. If transitions are on a deeply nested element within the x-if, they might not fire as expected because the parent is added/removed before Alpine can effectively apply classes to the inner child for the animation frame.
<!-- Correct: x-if on element with x-transition -->
<div x-if="isVisible" x-transition>Content</div>
<!-- Correct: x-if on template, x-transition on direct child -->
<template x-if="isVisible">
<div x-transition>Content</div>
</template>
Keep your transition classes focused. If you use many utility classes (like from Tailwind CSS) that also affect properties like transform or opacity, ensure they don't conflict with your intended x-transition behavior. Using dedicated, clearly named transition classes can make your setup easier to understand and debug. Always use your browser's developer tools to inspect the element during transition phases to see exactly which classes are active and what CSS properties are being applied or overridden.
x-transition shorthand.
x-if (Element Removal/Addition)x-if and slides in/out.