Under The Hood
frontend vue Vue 3.x

<Teleport>: DOM Relocation Outside Component Root & Dynamic Targets

Last updated
Prerequisites:
Vue Template Syntax
CSS Stacking Contexts & z-index
  • vue
  • teleport
  • dom
  • modals
  • top-layer
  • portal

Read at your depth

The practical view

`<Teleport to="body">` moves its template markup to a different DOM node (like `document.body` or `#modals-container`) while maintaining its reactive component connection. It is the standard solution for modals, tooltips, and notifications to prevent them from being cut off by parent CSS `overflow: hidden` or `z-index` stacking contexts. The `disabled` prop lets you toggle teleportation dynamically.

The same idea in other frameworks

react equivalent
`createPortal(<Modal />, document.body)` from `react-dom`.

Legacy vs modern

CSS-Trapped Modal vs Vue <Teleport>

Nested modal markup gets clipped by parent CSS; `<Teleport to="body">` mounts directly into body.

before → after
Clipped Nested Modal
<div class="modal">...</div> // ❌ Trapped inside overflow: hidden container
Teleport to Body
<Teleport to="body"><div v-if="showModal" class="modal">...</div></Teleport> // ✅ NEW

Interview gotchas

Context

Interviewers test your knowledge of mounting order and querySelector timing.

The mechanical answer

If the target element does not exist in the DOM when the component mounts, Vue will throw a runtime warning (`[Vue warn]: Failed to locate Teleport target with selector...`) and rendering of the teleported content will fail. The target DOM element must already exist in the HTML *before* the component containing `<Teleport>` is mounted.

Trap

Do not attempt to teleport to a sibling element that renders later in the same template; target elements must already be present in the DOM (e.g. in `index.html` or a previously mounted layout).

Under The Hood — a multi-depth technical interview hub.

Third-party licenses Press ⌘ K to search.