HTML rendering with {@html} in Svelte
📖 Scenario: You are building a simple Svelte component that displays user-generated HTML content safely inside your app.This is common when you want to show formatted text or HTML snippets stored as strings.
🎯 Goal: Create a Svelte component that renders a string containing HTML tags using the {@html} directive.This will show how to insert raw HTML inside your component safely and correctly.
📋 What You'll Learn
Create a variable called
htmlContent with a string containing HTML tagsCreate a variable called
maxLength to limit the displayed content lengthUse a computed variable called
shortHtml that slices htmlContent to maxLength charactersRender the
shortHtml variable inside a <div> using the {@html} directive💡 Why This Matters
🌍 Real World
Rendering user-generated or dynamic HTML content safely inside web apps is common in blogs, comments, or CMS systems.
💼 Career
Understanding how to use {@html} in Svelte helps you build interactive and dynamic web interfaces that handle rich text content.
Progress0 / 4 steps