Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

Vue Tutorial

Vue HOME Vue Intro Vue Directives Vue v-bind Vue v-if Vue v-show Vue v-for Vue Events Vue v-on Vue Methods Vue Event Modifiers Vue Forms Vue v-model Vue CSS Binding Vue Computed Properties Vue Watchers Vue Templates

Scaling Up

Vue Why, How and Setup Vue First SFC Page Vue Components Vue Props Vue v-for Components Vue $emit() Vue Fallthrough Attributes Vue Scoped Styling Vue Local Components Vue Slots Vue v-slot Vue Scoped Slots Vue Dynamic Components Vue Teleport Vue HTTP Request Vue Template Refs Vue Lifecycle Hooks Vue Provide/Inject Vue Routing Vue Form Inputs Vue Animations Vue Animations with v-for Vue Build Vue Composition API

Vue Reference

Vue Built-in Attributes Vue Built-in Components Vue Built-in Elements Vue Component Instance Vue Directives Vue Instance Options Vue Lifecycle Hooks

Vue Examples

Vue Examples Vue Exercises Vue Quiz Vue Syllabus Vue Study Plan Vue Server Vue Certificate

Vue v-show Directive


Example

Using the v-show directive to conditionally toggle the visibility of a <div> element, depending on the value of 'showDiv'.

<div v-show="showDiv">This div tag can be hidden</div>
Try it Yourself »

See more examples below.


Definition and Usage

The v-show directive is used to conditionally toggle the visibility of an element.

When the expression used by v-show evaluates to 'false', the CSS display property is set to 'none', and otherwise the CSS display property falls back to the default value.

An element with v-show is mounted once and remain in the DOM, only its visibility is toggled with v-show.

v-show triggers transition classes and events when used with the built-in <Transition> component.

Lifecycle hooks like mounted/unmounted, or activated/deactivated are not triggered when the visibility of an object is toggled with v-show.


v-show vs. v-if

The v-show and v-if directives are apparently quite similar, because they can both toggle an element so that it is displayed or not, but here are some differences:

v-show v-if
Creates and destroys an element in the DOM when it is toggled? no yes
Triggers lifecycle hooks mounted/unmounted when an element is toggled? no yes
Triggers transition events and classes for leaving and entering when used with the built-in <Transition> component? yes yes
Works with the built-in <template> element? no yes
Works with v-else-if and v-else? no yes

More Examples

Example

The v-show and v-if directives are used side-by-side to conditionally toggle the visibility of a <div> element.

Open the example, set the condition to 'false', then right-click and inspect the page to see that the element with v-show still exists in the DOM.

<div id="app">
  <div v-show="showDiv">Div tag with v-show</div>
  <div v-if="showDiv">Div tag with v-if</div>
</div>

<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<script>
  const app = Vue.createApp({
    data() {
      return {
        showDiv: true
      }
    }
  })
  app.mount('#app')
</script>
Try it Yourself »

Example

A <p> element becomes visible with v-show and triggers the after-enter event.

<template>
  <h1>JavaScript Transition Hooks</h1>
  <p>This code hooks into "after-enter" so that after the initial animation is done, a method runs that displays a red div.</p>
  <button @click="pVisible=true">Create p-tag!</button><br>
  <Transition @after-enter="onAfterEnter">
    <p v-show="pVisible" id="p1">Hello World!</p>
  </Transition>
  <br>
  <div v-show="divVisible">This appears after the "enter-active" phase of the transition.</div>
</template>

<script>
export default {
  data() {
    return {
      pVisible: false,
      divVisible: false
    }
  },
  methods: {
    onAfterEnter() {
      this.divVisible = true;
    }
  }
}
</script>

<style scoped>
  .v-enter-active {
    animation: swirlAdded 1s;
  }
  @keyframes swirlAdded {
    from {
      opacity: 0;
      rotate: 0;
      scale: 0.1;
    }
    to {
      opacity: 1;
      rotate: 360deg;
      scale: 1;
    }
  }
  #p1, div {
    display: inline-block;
    padding: 10px;
    border: dashed black 1px;
  }
  #p1 {
    background-color: lightgreen;
  }
  div {
    background-color: lightcoral;
  }
</style>
Run Example »

Related Pages

Vue Tutorial: Vue v-show Directive

Vue Tutorial: Vue v-if Directive

Vue Tutorial: Vue Animations

Vue Reference: Vue <Transition> Component

Vue Reference: Vue v-if Directive


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.