Description: Efficiently show or hide elements by toggling their `display` CSS property (usually between `display: none;` and its original display value) based on a boolean condition.
x-show="isPropertyNameTrue"
The x-show directive is your primary tool for conditional visibility in AlpineJS. It evaluates a JavaScript expression (typically a boolean property from your Alpine component's data). If the expression evaluates to true, the element is displayed. If it's false, AlpineJS adds an inline style style="display: none;" to the element, effectively hiding it and removing it from the document layout flow.
<div x-data="{ isVisible: true }">
<button @click="isVisible = !isVisible">Toggle Content</button>
<p x-show="isVisible">This content can be toggled.</p>
</div>
Works with x-transition for animations.
By itself, x-show toggles visibility abruptly. To create smooth entrance and exit animations (like fades, slides, or scales), you need to use x-show in conjunction with Alpine's transition directives (x-transition:enter, x-transition:enter-start, x-transition:enter-end, x-transition:leave, x-transition:leave-start, x-transition:leave-end). These directives allow you to apply CSS classes (often utility classes from Tailwind CSS) during different stages of the element's appearance and disappearance.
<div x-show="isVisible"
x-transition:enter="transition ease-out duration-300"
x-transition:enter-start="opacity-0 scale-90"
x-transition:enter-end="opacity-100 scale-100"
x-transition:leave="transition ease-in duration-200"
x-transition:leave-start="opacity-100 scale-100"
x-transition:leave-end="opacity-0 scale-90">
This content will animate smoothly.
</div>
Elements still occupying space when hidden with `x-show` if not `display: none`.
This is a common misconception. x-show works by adding an inline style="display: none;". This CSS rule removes the element from the layout flow entirely, so it does not occupy space. If you observe an element seemingly occupying space while hidden with x-show, it's likely due to other CSS rules (e.g., a parent element's fixed height, or a CSS rule with !important overriding Alpine's inline style, though this is rare for display: none). It's important to distinguish this from visibility: hidden;, which *does* hide the element but keeps its space in the layout.
Transitions not working without `x-transition` directives.
A frequent point of confusion is expecting animations to "just work" with x-show. AlpineJS requires explicit transition directives (x-transition:*) to manage animations. If these are omitted, the element will simply appear or disappear instantly. Always remember to add the necessary x-transition attributes if you want smooth visual effects.
Over-using `x-show` for elements that should be completely removed.
x-show is efficient for toggling visibility frequently because it only changes CSS display properties. The element and its children (including any Alpine components or event listeners within them) remain in the DOM. For elements that are complex, computationally intensive to render, rarely shown, or need to be completely absent from the accessibility tree when not visible, x-if is often a better choice. x-if physically removes the element from the DOM when its condition is false and re-adds/re-initializes it when true. This distinction is crucial for performance and accessibility in certain scenarios.
This is the content of the accordion panel. It is shown or hidden using x-show.
When hidden, this element has style="display: none;" applied, removing it from the layout flow. The transition is handled by x-transition directives.
Current state:
Notice how the `max-h` property is used in transitions for smooth height animation alongside opacity and translate.