What if you could turn plain text with HTML tags into real, styled content instantly?
Why HTML rendering with {@html} in Svelte? - Purpose & Use Cases
Imagine you want to show some HTML content that comes as a string from a server, like a blog post or user comment with formatting.
You try to display it by just putting the string inside your page, but it shows as plain text with all the tags visible.
Manually inserting HTML strings as text means the browser won't render the tags as real HTML.
You'd have to write complex code to parse and insert elements, which is slow, error-prone, and unsafe.
Svelte's {@html} tag lets you insert raw HTML strings directly into your component's output.
This means the HTML inside the string renders properly as real elements, saving you from manual parsing and making your app dynamic and flexible.
let content = '<p><strong>Hello</strong> world!</p>'
<p>{content}</p>let content = '<p><strong>Hello</strong> world!</p>'
{@html content}You can safely and easily render dynamic HTML content from strings, making your app more interactive and rich.
Displaying user-generated blog posts or comments that include formatting like bold, italics, or links without losing the HTML structure.
Manual HTML strings show as plain text, not formatted.
{@html} renders raw HTML strings as real elements.
This makes dynamic content display simple and clean.