x-show Directive
Toggle visibility without removing the element from the DOM. Super fast for frequent toggles.
What is x-show?
x-show toggles the visibility of an element by setting display: none; inline. It does not remove the element from the DOM, meaning the element's state (scroll position, input values, etc.) is preserved.
When to Use
Use x-show when you need to show/hide an element frequently and want to keep its internal state. It’s faster than x-if because no DOM elements are created/destroyed. Ideal for dropdowns, tooltips, or tabs where the content is toggled often.
Basic Example
<div x-data="{ visible: true }">
<button @click="visible = !visible">Toggle</button>
<p x-show="visible">Now you see me!</p>
</div>Transition Effects
Combine x-show with x-transition to animate the show/hide behavior. This is extremely powerful and allows you to use Tailwind CSS utilities for animations.
<div x-data="{ open: false }">
<button @click="open = !open">Animate</button>
<div x-show="open" x-transition.opacity.scale>
Animated content
</div>
</div>Difference from x-if
x-showtoggles the CSSdisplayproperty. The element remains in the DOM.x-ifremoves or inserts the element entirely from the DOM.- Use
x-showfor toggles that happen often (dropdowns, tooltips). - Use
x-iffor content that should be completely removed to save resources (e.g., rarely opened modals).
Does x-show remove event listeners?
No, because the element stays in the DOM. All event listeners remain active, so you don't need to rebind them.
Can I use x-show with transitions other than opacity and scale?
Yes! You can use the full x-transition:enter and x-transition:leave modifiers to define any CSS classes, allowing for custom animations like sliding, rotating, etc.
Live Toggle
Hello!