Sometimes you want to show HTML code stored in a variable as real HTML on the page, not just plain text.
HTML rendering with {@html} in Svelte
Introduction
Syntax
Svelte
{@html variableContainingHtml}This tells Svelte to treat the variable's content as HTML, not plain text.
Be careful: only use with trusted content to avoid security risks.
Examples
Svelte
<script> let htmlContent = '<strong>Hello!</strong>'; </script> <p>{@html htmlContent}</p>
Svelte
<script> let listHtml = '<ul><li>Apple</li><li>Banana</li></ul>'; </script> <div>{@html listHtml}</div>
Svelte
<script> let unsafeHtml = '<img src=x onerror="alert(1)" />'; </script> <p>{@html unsafeHtml}</p>
Sample Program
This component shows the difference between showing HTML as plain text and rendering it as real HTML.
Svelte
<script> let message = '<em>Welcome to Svelte!</em>'; </script> <main> <h1>HTML Rendering Example</h1> <p>Normal text: {message}</p> <p>Rendered HTML: {@html message}</p> </main>
Important Notes
Never use {@html} with untrusted content to avoid security problems like cross-site scripting (XSS).
If you want to show HTML code as text, just use curly braces without {@html}.
Summary
{@html} lets you render HTML stored in variables as real HTML inside your Svelte components.
Use it only with safe, trusted HTML to keep your app secure.
It helps when you want to display formatted content dynamically.
Practice
1. What does the
{@html} tag do in a Svelte component?easy
Solution
Step 1: Understand {@html} purpose
The {@html} tag tells Svelte to treat the string as HTML, not plain text.Step 2: Compare with normal rendering
Normally, Svelte escapes HTML tags to show them as text, but {@html} renders them as real HTML.Final Answer:
It renders a string as actual HTML inside the component. -> Option BQuick Check:
{@html} = renders HTML [OK]
Hint: Remember {@html} outputs HTML, not plain text [OK]
Common Mistakes:
- Thinking {@html} escapes HTML instead of rendering it
- Confusing {@html} with creating new elements
- Assuming {@html} comments out code
2. Which of the following is the correct syntax to render HTML stored in a variable
htmlContent inside a Svelte component?easy
Solution
Step 1: Recall correct {@html} syntax
The correct syntax is {@html variableName} without parentheses or block tags.Step 2: Check each option
<div>{htmlContent}</div>renders as plain text, B uses invalid parentheses, D uses wrong block syntax.Final Answer:
<div>{@html htmlContent}</div> -> Option AQuick Check:
Correct {@html} syntax = {@html variable} [OK]
Hint: Use {@html variable} without parentheses or blocks [OK]
Common Mistakes:
- Adding parentheses around variable in {@html}
- Using block syntax like {#html}{/html}
- Not using {@html} and rendering raw string
3. Given this Svelte code:
What will be the rendered output in the browser?
<script>
let htmlString = '<strong>Hello</strong> <em>World</em>';
</script>
<div>{@html htmlString}</div>What will be the rendered output in the browser?
medium
Solution
Step 1: Understand {@html} rendering
{@html renders the string as HTML, so tags become real formatting.Step 2: Analyze the string content
The string contains <strong> and <em> tags, so output will be bold and italic text.Final Answer:
<strong>Hello</strong> <em>World</em> (formatted text) -> Option CQuick Check:
HTML string renders as formatted text [OK]
Hint: {@html} renders tags as formatting, not raw text [OK]
Common Mistakes:
- Expecting tags to show as text instead of formatting
- Assuming plain text output without tags
- Thinking unsafe HTML causes runtime error
4. What is the main problem with this Svelte code?
<script>
let unsafeHtml = '';
</script>
<div>{@html unsafeHtml}</div>medium
Solution
Step 1: Identify unsafe HTML usage
The string contains an image with an onerror event that runs JavaScript.Step 2: Understand {@html} risks
{@html renders raw HTML, so malicious scripts can run causing XSS security issues.Final Answer:
It can run unsafe scripts and cause security issues (XSS). -> Option AQuick Check:
{@html with unsafe HTML = security risk [OK]
Hint: Never use {@html} with untrusted content [OK]
Common Mistakes:
- Thinking Svelte sanitizes HTML automatically
- Assuming syntax error due to quotes inside string
- Believing image won't display due to src
5. You want to display user-generated content that may contain HTML tags safely in your Svelte app. Which approach is best?
hard
Solution
Step 1: Understand risks of raw {@html}
Using {@html} directly on user content risks XSS attacks if content is unsafe.Step 2: Best practice for safe HTML rendering
Sanitize user content to remove dangerous tags/scripts, then render with {@html} for formatting.Final Answer:
Sanitize the user content to remove unsafe tags, then use {@html}. -> Option DQuick Check:
Sanitize first, then {@html} = safe rendering [OK]
Hint: Always sanitize user HTML before {@html} rendering [OK]
Common Mistakes:
- Using {@html} directly on raw user input
- Escaping content but losing formatting
- Using <pre> which shows raw text, not formatted HTML
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
