Description: The `x-show` directive in AlpineJS allows you to efficiently show or hide HTML elements by toggling their CSS `display` property. When the condition provided to `x-show` is true, the element is displayed; when false, its `display` style is set to `none`, effectively removing it from the layout flow.
x-show="isPropertyNameTrue"
This is the core directive. You assign it a JavaScript expression (typically a boolean property from your Alpine component's data). If the expression evaluates to a truthy value, the element is shown. If it evaluates to a falsy value, the element is hidden (style="display: none;" is applied).
For example:
<div x-data="{ isOpen: false }">
<button @click="isOpen = !isOpen">Toggle Content</button>
<div x-show="isOpen">This content appears and disappears.</div>
</div>
Works with x-transition for animations
By itself, `x-show` toggles visibility instantly. To add smooth animations (like fades or slides), you need to pair `x-show` with `x-transition` directives. AlpineJS provides several transition helper directives (e.g., x-transition:enter, x-transition:leave, x-transition:enter-start, x-transition:leave-end) that apply CSS classes at different stages of the transition.
For example, a simple fade transition:
<div x-show="isOpen"
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">
Animated content.
</div>
Alpine comes with some default transitions that can be applied more simply using just `x-transition` (e.g., `
Elements and Layout Flow: When an element is hidden with `x-show`, AlpineJS adds an inline style `style="display: none;"`. This is crucial because `display: none;` removes the element entirely from the document's layout flow. This means it doesn't take up any space, and surrounding elements will reflow to fill the gap. This is different from `visibility: hidden;`, which makes an element invisible but still reserves its space in the layout. If your CSS has a `display` property with `!important` that overrides Alpine's inline style, `x-show` might not hide the element as expected.
Transitions Require x-transition Directives:
If you expect smooth animations when elements appear or disappear, simply using `x-show` is not enough. It will result in an abrupt change. You MUST explicitly add `x-transition` directives to define the animation behavior. Without them, the toggle is instantaneous.
Over-using x-show vs. x-if:
Python developers might wonder when to use `x-show` versus `x-if`.
x-if: Conditionally removes/adds elements from/to the DOM. This is more suitable if:
Current modal state: