Skip to the workspace
Pebellslearn by building
Templates  ›  Lesson

v-html — Rendering Raw HTML

v-html 11 minv-html replaces an element's content with a string parsed as HTML, which is only safe for content you fully trust.

vue / v-html — Rendering Raw HTML

Explore each scene at your own pace.

01 · The rule

The rule

v-html — Rendering Raw HTML

Shelf's team writes a short "Book of the week" blurb in the admin panel, stored as HTML with bold titles and links. Printed with double braces, the reader sees raw tags like <strong>Dune</strong>. To have the browser treat the string as markup, you need v-html, and you need to know exactly when it is safe.

The rule

v-html trusts the string completely, so feed it only trusted HTML.

<script setup>
const blurb = '<strong>Dune</strong> is this week\'s pick. <a href="/books/1">Read more</a>'
</script>

<template>
  <p>{{ blurb }}</p>          <!-- shows the tags as text -->
  <div v-html="blurb"></div>  <!-- shows bold text and a link -->
</template>

What v-html does, piece by piece:

  • Replaces the content. Whatever was inside the div is thrown away and the string's HTML takes its place. Do not put children inside an element that has v-html.
  • Plain HTML only. The string is not a Vue template. If it contains {{ title }} or <BookCard />, they are not processed; they just appear as text or an unknown tag.
  • Scoped styles do not reach it. Scoped CSS works by stamping elements Vue creates from the template. Elements that arrive through v-html are not stamped, so scoped rules skip them. Style them with global CSS or the :deep() selector.
  • No escaping. This is the dangerous part. If the string came from a user, it can carry markup such as an image with an onerror attribute that runs JavaScript in every reader's browser. That is a cross-site scripting (XSS) attack.
Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.