Description: The x-show directive in AlpineJS provides an efficient way to show or hide HTML elements. It works by toggling the element's display CSS property. When the condition for x-show is true, the element is displayed using its original display value (e.g., block, inline-block, etc.). When the condition is false, AlpineJS adds an inline style style="display: none;" to the element, effectively hiding it and removing it from the document layout flow.
x-show="isPropertyNameTrue": This is the core directive. You provide a JavaScript expression (typically a boolean property from your Alpine component's data) to it. If the expression evaluates to a truthy value, the element is shown. If it evaluates to a falsy value, the element is hidden.
For example:
<div x-data="{ isOpen: true }">
<button @click="isOpen = !isOpen">Toggle Content</button>
<div x-show="isOpen">This content can be toggled.</div>
</div>
Works with x-transition for animations: x-show integrates seamlessly with Alpine's transition directives (like x-transition:enter, x-transition:leave, etc.) to add smooth animations when elements appear or disappear. Without x-transition, the visibility change is abrupt.
<div x-show="isOpen" x-transition:enter="transition ease-out duration-300" ...>
Animated content
</div>
This allows for CSS-based transitions on properties like opacity and transform, making UI changes more polished.
Elements still occupying space when hidden with x-show if not display: none: This is generally a misconception. x-show works by setting style="display: none;". This CSS rule removes the element entirely from the layout flow, meaning it does not occupy space. However, if you have conflicting CSS rules (e.g., a CSS rule with !important that sets a different display value for that element, or if styles target it via high-specificity selectors), it might override Alpine's inline style. If you're accustomed to visibility: hidden; (which hides an element but keeps its space in the layout), x-show behaves differently and more like what's usually desired for conditional UI sections.
Transitions not working without x-transition directives: If you expect an element to fade in or slide out when its visibility is toggled by x-show, you must add the appropriate x-transition directives. x-show by itself only changes the display property, leading to an instant show/hide. Alpine provides a powerful and easy-to-use transition system; leverage it!
<!-- Abrupt change -->
<div x-show="isVisible">Content</div>
<!-- Smooth fade (example) -->
<div x-show="isVisible"
x-transition:enter="transition ease-out duration-300"
x-transition:enter-start="opacity-0"
x-transition:enter-end="opacity-100"
x-transition:leave="transition ease-in duration-200"
x-transition:leave-start="opacity-100"
x-transition:leave-end="opacity-0">
Fading Content
</div>
Over-using x-show for elements that should be completely removed: While x-show is great for toggling visibility, it keeps the element in the DOM (just hidden with CSS). If an element and its children are complex, computationally intensive to maintain, or should not be in the accessibility tree when hidden (e.g., a modal that's not active), x-if might be a more appropriate choice. x-if completely removes the element from the DOM when its condition is false and re-adds it when true. For simple toggles or elements that are frequently shown/hidden, x-show is generally more performant as it avoids DOM manipulation costs.
When to use x-show:
When to consider x-if:
Retrieved at:
Error:
This is a simple panel. When hidden, it has display: none; and does not take up space.
This panel uses x-transition for a smooth fade and slide effect when appearing and disappearing.