TExcelTAD
Home › Vue › Vue v-show

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.

html
<div v-show="menuOpen">…menu…</div>

A first example

html · try it yourself
<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.

html · try it yourself
<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-show for things that toggle often (tabs, dropdowns, tooltips) and v-if for 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.

html · try it yourself
<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-show cannot be used on <template>, because a <template> produces no element to style.
  • A CSS rule with display: … !important on the element will override v-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-show sets an inline style="display: none;". When the condition becomes true again, Vue restores the element's original display value.

Test yourself

How does v-show hide an element?

  1. It removes the element from the DOM
  2. It sets CSS display: none
  3. It sets visibility: hidden
  4. It moves the element off-screen

Which directive is the better choice for a dropdown that opens and closes very often?

  1. v-show
  2. v-if
  3. v-for
  4. v-once

Which of these works with v-show?

  1. <template v-show="ok">
  2. v-show followed by v-else
  3. A normal <div v-show="ok">
  4. None of them