Vue v-show
v-show also shows or hides an element based on a condition, but it works differently from v-if: the element is always rendered and stays in the DOM. Vue simply switches its CSS display property between its normal value and none.
<div v-show="menuOpen">…menu…</div>A first example
<style>
.panel { margin-top: 8px; padding: 12px; background: #ecfdf5; border: 1px solid #42b883; border-radius: 6px; }
</style>
<div id="app">
<button @click="open = !open">{{ open ? 'Hide' : 'Show' }} details</button>
<div class="panel" v-show="open">
<strong>Opening hours</strong><br>
Mon–Fri 8:00–18:00, Sat 9:00–13:00
</div>
<p><small>Open the browser inspector: the panel is still in the DOM, just with <code>display: none</code>.</small></p>
</div>
<script type="module">
import { createApp } from 'vue';
createApp({
data: () => ({ open: false })
}).mount('#app');
</script>v-show versus v-if
The next playground renders the same input twice — once controlled by v-if, once by v-show. Type something in both, hide them, and show them again.
<div id="app">
<label><input type="checkbox" v-model="visible"> Visible</label>
<p v-if="visible">v-if: <input placeholder="type here"></p>
<p v-show="visible">v-show: <input placeholder="type here too"></p>
<button @click="inspect">Count paragraphs in the DOM</button>
</div>
<script type="module">
import { createApp } from 'vue';
createApp({
data: () => ({ visible: true }),
methods: {
inspect() {
const count = document.querySelectorAll('#app p').length;
console.log('Paragraphs in the DOM:', count);
}
}
}).mount('#app');
</script>When hidden, the v-if paragraph is gone (the count drops to 1) and its text is lost. The v-show paragraph is still there and keeps what you typed.
v-if |
v-show |
|
|---|---|---|
| How it hides | Removes the element from the DOM | Sets display: none |
| Initial cost | Cheap when false — nothing is rendered | Always renders the element |
| Toggle cost | Higher — destroys and recreates elements and components | Very low — only one CSS change |
| Keeps state (input text, component data) | ❌ no | ✅ yes |
Works with v-else |
✅ yes | ❌ no |
Works on <template> |
✅ yes | ❌ no |
Tip: Rule of thumb — use
v-showfor things that toggle often (tabs, dropdowns, tooltips) andv-iffor things that rarely change or may never be needed (admin panels, error messages).
Building tabs with v-show
Tabs are a classic v-show use case: switching is instant, and each tab keeps its scroll position and form input.
<style>
.tabs button { border: none; padding: 6px 12px; cursor: pointer; background: #eee; }
.tabs button.active { background: #42b883; color: white; }
.tab { border: 1px solid #ddd; padding: 12px; }
</style>
<div id="app">
<div class="tabs">
<button v-for="name in tabs" :class="{ active: current === name }" @click="current = name">
{{ name }}
</button>
</div>
<div class="tab" v-show="current === 'Profile'">
<label>Nickname <input value="vue-fan"></label>
</div>
<div class="tab" v-show="current === 'Settings'">
<label><input type="checkbox" checked> Email notifications</label>
</div>
<div class="tab" v-show="current === 'About'">
ExcelTAD Vue course — version 1.0
</div>
</div>
<script type="module">
import { createApp } from 'vue';
createApp({
data: () => ({ tabs: ['Profile', 'Settings', 'About'], current: 'Profile' })
}).mount('#app');
</script>Change the nickname, switch to another tab and back: your change is still there.
Things to watch out for
v-showcannot be used on<template>, because a<template>produces no element to style.- A CSS rule with
display: … !importanton the element will overridev-show. - Hidden content is still in the page, so screen readers and search engines may still find it, and any images inside are still downloaded.
Note:
v-showsets an inlinestyle="display: none;". When the condition becomes true again, Vue restores the element's originaldisplayvalue.
Test yourself
How does v-show hide an element?
- It removes the element from the DOM
- It sets CSS
display: none - It sets
visibility: hidden - It moves the element off-screen
Which directive is the better choice for a dropdown that opens and closes very often?
v-showv-ifv-forv-once
Which of these works with v-show?
<template v-show="ok">v-showfollowed byv-else- A normal
<div v-show="ok"> - None of them