Introduction
The svelte:fragment tag lets you group multiple elements together without adding extra HTML tags to the page.
The svelte:fragment tag lets you group multiple elements together without adding extra HTML tags to the page.
<svelte:fragment> <!-- multiple elements here --> </svelte:fragment>
svelte:fragment does not create any extra HTML element in the output.<svelte:fragment> <p>First paragraph</p> <p>Second paragraph</p> </svelte:fragment>
<MyComponent>
<svelte:fragment>
<h1>Title</h1>
<p>Description here.</p>
</svelte:fragment>
</MyComponent>This example shows how svelte:fragment groups a heading and paragraph inside an if block without adding extra HTML elements.
<script> let show = true; </script> {#if show} <svelte:fragment> <h2>Welcome!</h2> <p>This is grouped without extra divs.</p> </svelte:fragment> {/if}
Using svelte:fragment helps keep your HTML clean and semantic.
It is especially useful when you want to avoid unnecessary wrappers that might affect styling or layout.
svelte:fragment groups multiple elements without adding extra HTML tags.
Use it to keep your markup clean and pass multiple elements as one block.
It works well inside conditionals and slots.
svelte:fragment in Svelte?svelte:fragmentsvelte:fragment allows grouping multiple elements together without wrapping them in an extra HTML tag.svelte:fragment.svelte:fragment groups without extra tags [OK]svelte:fragment to group two paragraphs?<svelte:fragment> with a colon, not <fragment> or other variants.<svelte:fragment><p>One</p><p>Two</p></svelte:fragment> correctly wraps two paragraphs inside <svelte:fragment> tags.svelte:fragment with colon exactly [OK]svelte:fragmentfragment or svelte-fragment tagsdiv instead of svelte:fragment{#if show}
<svelte:fragment>
<h1>Hello</h1>
<p>Welcome!</p>
</svelte:fragment>
{:else}
<p>Goodbye</p>
{/if}
Assuming show is true.show is true, so the first block inside {#if} runs.svelte:fragment outputsvelte:fragment groups the <h1> and <p> without adding extra tags, so both render side by side.svelte:fragment?
{#if show}
<svelte:fragment>
<h2>Title</h2>
</svelte:fragment>
<p>Paragraph</p>
{/if}
Assuming show is true.{#if} block contains two sibling elements: a svelte:fragment wrapping <h2> and a separate <p>.svelte:fragment usagesvelte:fragment groups elements but does not require all elements inside it. Siblings outside still render normally.svelte:fragment to achieve this?svelte:fragment with the slot attribute groups them without extra tags.<Child>
<svelte:fragment slot="header">
<h1>Title</h1>
<p>Subtitle</p>
</svelte:fragment>
</Child> uses <svelte:fragment slot="header"> correctly. <Child>
<div slot="header">
<h1>Title</h1>
<p>Subtitle</p>
</div>
</Child> adds an extra div, <Child>
<slot name="header">
<h1>Title</h1>
<p>Subtitle</p>
</slot>
</Child> misuses slot inside child, and <Child>
<svelte:fragment>
<h1>Title</h1>
<p>Subtitle</p>
</svelte:fragment>
</Child> misses the slot attribute.svelte:fragment with slot attribute = B [OK]svelte:fragment slot="name" to group slot content [OK]slot tags incorrectly inside childsvelte:fragmentPass the test and this pattern moves unknown