Vue Teleport
Some UI belongs logically inside a component but visually somewhere else on the page. A modal dialog opened by a button deep inside a card, a toast notification, a dropdown that must escape an overflow: hidden container — they should be rendered near the end of <body>, on top of everything.
The built-in <Teleport> component (written <teleport> in in-DOM templates) moves its content to another place in the DOM, while it stays a normal part of your component in Vue's eyes: it still uses the component's data, props and events.
<teleport to="body">
<div class="modal">…</div>
</teleport>to takes a CSS selector ("body", "#modals", ".toasts") or an actual DOM element.
Why it's needed
The example below has a card with overflow: hidden and transform — two CSS properties that trap position: fixed children. The "trapped" popup is clipped by the card; the teleported one is rendered at the end of <body> and covers the whole frame correctly.
<style>
.card { width: 240px; height: 110px; padding: 10px; border: 2px solid #42b883; overflow: hidden; transform: translateZ(0); font-family: sans-serif; }
.overlay { position: fixed; inset: 0; background: #0007; display: grid; place-items: center; }
.popup { background: white; padding: 16px; border-radius: 8px; }
</style>
<div id="app">
<div class="card">
<b>Product card</b><br>
<button @click="trapped = true">Open (no teleport)</button>
<button @click="teleported = true">Open (teleport)</button>
<div v-if="trapped" class="overlay" @click="trapped = false">
<div class="popup">I'm stuck inside the card 😕</div>
</div>
<teleport to="body">
<div v-if="teleported" class="overlay" @click="teleported = false">
<div class="popup">I'm rendered at the end of <body> 🎉<br><small>click to close</small></div>
</div>
</teleport>
</div>
</div>
<script type="module">
import { createApp } from 'vue';
createApp({ data: () => ({ trapped: false, teleported: false }) }).mount('#app');
</script>Even though the teleported overlay lives outside #app in the DOM, @click="teleported = false" still changes the component's data.
A reusable modal component
A typical pattern: a modal component that always teleports itself, so it can be used anywhere.
<style>
.backdrop { position: fixed; inset: 0; background: #0006; display: grid; place-items: center; }
.dialog { background: white; border-radius: 10px; padding: 16px 20px; width: 240px; font-family: sans-serif; }
</style>
<div id="app">
<p>Cart: {{ items }} item(s)</p>
<button @click="ask = true">Empty the cart</button>
<confirm-modal v-if="ask" title="Empty cart?" @yes="items = 0; ask = false" @no="ask = false">
All {{ items }} items will be removed.
</confirm-modal>
</div>
<script type="module">
import { createApp } from 'vue';
const app = createApp({ data: () => ({ items: 3, ask: false }) });
app.component('confirm-modal', {
props: ['title'],
emits: ['yes', 'no'],
mounted() {
console.log('backdrop is inside #app?', !!document.querySelector('#app .backdrop'));
console.log('backdrop is a direct child of body?', !!document.querySelector('body > .backdrop'));
},
template: `
<teleport to="body">
<div class="backdrop" @click.self="$emit('no')">
<div class="dialog">
<h3>{{ title }}</h3>
<p><slot></slot></p>
<button @click="$emit('no')">Cancel</button>
<button @click="$emit('yes')">Yes</button>
</div>
</div>
</teleport>`
});
app.mount('#app');
</script>Teleporting into a specific container
Many apps add a dedicated container next to the app root, such as <div id="toasts">, and teleport notifications into it. Several teleports to the same target are appended in order.
<style>
#toasts { position: fixed; right: 10px; bottom: 10px; display: flex; flex-direction: column; gap: 6px; }
.toast { background: #1f2937; color: white; padding: 8px 12px; border-radius: 6px; font: 14px sans-serif; }
</style>
<div id="app">
<button @click="notify('Saved ✔')">Save</button>
<button @click="notify('Copied 📋')">Copy</button>
<toast-message v-for="t in toasts" :key="t.id" :text="t.text"></toast-message>
</div>
<div id="toasts"></div>
<script type="module">
import { createApp } from 'vue';
const app = createApp({
data: () => ({ toasts: [], nextId: 1 }),
methods: {
notify(text) {
const id = this.nextId++;
this.toasts.push({ id, text });
setTimeout(() => { this.toasts = this.toasts.filter(t => t.id !== id); }, 2500);
}
}
});
app.component('toast-message', {
props: ['text'],
template: `<teleport to="#toasts"><div class="toast">{{ text }}</div></teleport>`
});
app.mount('#app');
</script>Note: The target element must already exist when the teleport is mounted. Here
#toastscomes before the script runs, so it's ready. Vue 3.5 also adds adeferprop (<teleport defer to="#late">) for targets rendered later by the same app.
Teleport options
| Prop | Meaning |
|---|---|
to |
Target: CSS selector string or element (required) |
:disabled="cond" |
When true, content stays in place instead of moving — e.g. render inline on mobile |
defer |
Resolve the target after the rest of the app has mounted (Vue 3.5+) |
Tip: Teleport changes only the DOM position. The component tree is unchanged, so props, events, provide/inject and Vue DevTools behave as if the content were still inside its parent.
Test yourself
What does <Teleport to="body"> do?
- Renders its content at the end of
<body>in the DOM - Copies the component to another app
- Scrolls the page to the body
- Loads the content lazily
Can teleported content still use the component's data and emit events?
- No, it becomes a separate app
- Yes, it is still part of the same component
- Only if you use provide/inject
- Only with
v-once
Which Teleport prop keeps the content in its original place conditionally?
stayinlinedisabledv-show