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 provided to x-show evaluates to true, the element is displayed (typically using its original display value, e.g., block, inline-block, etc.). When the condition is false, AlpineJS adds an inline style display: none; to the element, effectively hiding it from the layout.
x-show="isPropertyNameTrue": This is the core attribute. You assign it a JavaScript expression (usually a boolean property from your Alpine component's data).
When the expression evaluates to a "truthy" value, the element is shown.
When it evaluates to a "falsy" value (e.g., false, 0, null, undefined, empty string), the element is hidden with display: none;.
<div x-data="{ isOpen: true }">
<button @click="isOpen = !isOpen">Toggle</button>
<p x-show="isOpen">This paragraph is visible when isOpen is true.</p>
</div>
Works with x-transition for animations: x-show integrates seamlessly with Alpine's transition directives (x-transition:enter, x-transition:leave, etc.) to add smooth animations when elements appear or disappear. Without x-transition, the showing and hiding will be abrupt.
<div x-show="isOpen"
x-transition:enter="transition ease-out duration-300"
x-transition:enter-start="opacity-0 transform scale-90"
x-transition:enter-end="opacity-100 transform scale-100"
x-transition:leave="transition ease-in duration-200"
x-transition:leave-start="opacity-100 transform scale-100"
x-transition:leave-end="opacity-0 transform scale-90">
Animated content.
</div>
AlpineJS adds and removes CSS classes during these transitions, allowing you to define the animation using Tailwind CSS or custom CSS.
Elements still occupying space when hidden with `x-show` if not `display: none`.: This is a common misconception. x-show works by setting display: none;. This means the element is completely removed from the layout flow and does not occupy any space. If you're observing different behavior, it's likely due to:
!important) might be overriding Alpine's inline style="display: none;".visibility: hidden;: If you're expecting behavior similar to visibility: hidden; (where the element is invisible but still occupies its space), x-show is different. x-show is for completely removing the element from the document flow when hidden.Transitions not working without `x-transition` directives.: If you simply use x-show, elements will appear and disappear instantly. For smooth animations, you must add x-transition directives. Alpine doesn't apply any default transitions automatically. You need to define the transition states (e.g., x-transition:enter, x-transition:leave) and the corresponding CSS classes yourself.
Over-using `x-show` for elements that should be completely removed.:
While x-show is great for toggling visibility, it's important to understand its mechanism. The element remains in the DOM, just hidden with CSS.
For complex elements with many children, or for content that should not be in the accessibility tree when hidden (e.g., a modal that's not active), using x-if might be more appropriate.
x-if completely removes the element and its children from the DOM when its condition is false, and re-adds them when true. This can be better for performance with very large/complex DOM subtrees and ensures hidden content isn't accidentally picked up by assistive technologies.
Choose x-show for frequent toggles where the element's state might be preserved, and x-if when true conditional rendering (DOM addition/removal) is needed.
This example demonstrates toggling a dropdown menu's visibility using x-show. It also includes a simple transition effect.
Selected option:
Dropdown is closed. Click "Options" to open it and select an item.