A menu card, a promo card, an announcement card: the frame is the same, the content wildly different. One holds a price, another a button, another a picture. Props suit values such as text and numbers, not whole pieces of the view. That's what a slot is for: a hole inside a component that its parent fills with markup, be it a paragraph, a button or another component. In Svelte 5 that content arrives as a snippet, and the component places it with {@render}. If you come from React, its role is close to children, and it even has the same name. From Vue, the name is slot.
Picture the serving trays at Mrs. Sri's stall. Every tray has the same shape: a spot for the dish's name card at the top, a spot for the plate in the middle, and a spot for the cutlery at the bottom. The tray doesn't care whether the plate holds Fried Rice or Iced Tea. The waiter using it fills it. If the plate spot is empty, "Nothing here yet" is printed on the tray's floor. The Card component is the tray, each {@render} is a spot, the parent is the waiter, and that printed text is the fallback.
The default slot: children
Card.svelte only provides the frame. The content comes from the parent:
<!-- Card.svelte -->
<script>
let { children } = $props()
</script>
<div class="card">
{@render children?.()}
</div>
<!-- App.svelte -->
<script>
import Card from "./Card.svelte"
</script>
<Card>
<h3>Fried Rice</h3>
<p>$15.00</p>
</Card>
The result is <div class="card"><h3>Fried Rice</h3> <p>$15.00</p></div>.
- Everything the parent writes between
<Card>and</Card>reaches Card as a prop namedchildren. It holds a snippet: a piece of markup ready to be placed. {@render children?.()}places that snippet right here. This is the default slot.?.covers a parent that sends no content.<Card />with no closing tag meanschildrenis empty, and{@render children()}without?.throws an error at run time.- The content still belongs to the parent: its variables are read from App's script, and its styles come from App's
<style>, not Card's.
Fallback: spare content
<script>
let { children } = $props()
</script>
<div class="card">
{#if children}{@render children()}{:else}Nothing here yet{/if}
</div>
| The parent writes | The card shows |
|---|---|
<Card><p>$15.00</p></Card> |
$15.00 |
<Card /> |
Nothing here yet |
<Card> </Card> |
Nothing here yet |
- What sits in the
{:else}branch is the fallback, spare content. It shows only when the parent sends nothing, because thenchildrenis empty. Whitespace alone doesn't count as content. - Inside
{#if children},?.isn't needed any more:childrenis sure to be there. - This is the shape of the first exercise:
let { children } = $props(), then<div class="card">{#if children}{@render children()}{:else}Default content{/if}</div>. The exercise renders Card with no parent, so it showsDefault content.
Named slots: more than one hole
<!-- Card.svelte -->
<script>
let { title, children, action } = $props()
</script>
<div class="card">
{#if title}{@render title()}{:else}<h3>New dish</h3>{/if}
{#if children}{@render children()}{:else}Nothing here yet{/if}
<footer>
{@render action?.()}
</footer>
</div>
<!-- App.svelte -->
<Card>
{#snippet title()}
<h3>Fried Rice</h3>
{/snippet}
<p>$15.00</p>
{#snippet action()}
<button>Order</button>
{/snippet}
</Card>
- A
{#snippet title()}...{/snippet}written directly between the<Card>tags becomes a prop namedtitle. In Card,{@render title()}places it. This is Svelte 5's named slot. - Whatever isn't wrapped in a snippet, here
<p>$15.00</p>, goes intochildren. - What shows follows the order of the
{@render}tags in Card, not the order they're written in App. - A named slot can have a fallback too, through
{:else}:<h3>New dish</h3>shows when App sends notitle. - Need two buttons in one hole? Write both inside
{#snippet action()}. A snippet needs no wrapper. - The second exercise uses this pattern:
let { title, children } = $props(), then<div class="alert"><h3>{#if title}{@render title()}{:else}Warning{/if}</h3>{#if children}{@render children()}{:else}No message.{/if}</div>.
Unfilled slots and {#if action}
<Card>{#snippet title()}<h3>Iced Tea</h3>{/snippet}</Card> produces:
<div class="card"><h3>Iced Tea</h3> Nothing here yet <footer></footer></div>
(Svelte also adds a few empty HTML comments as position markers. They're left out here, since they don't show on the page.)
<footer> is Card's own markup, not part of the snippet. An action that wasn't sent is just empty, while its wrapper stays. If that <footer> has a top border, the line is still drawn even though nothing is inside.
A snippet is an ordinary prop, so to know whether the parent sent one, a plain {#if} is enough. For the Iced Tea card above, title holds a snippet, while children and action are undefined. Use that to hide the wrapper:
{#if action}
<footer>
{@render action()}
</footer>
{/if}
If the parent sends {#snippet price()} while Card never renders price, its content shows nowhere, and there's no warning. So match the snippet's name in the parent with the name in the child's $props().
Slot props: the child hands data back
Sometimes the child owns the data, but the parent decides how it looks. MenuList.svelte loops over the menu array, and each round hands one item to the snippet the parent sent:
<!-- MenuList.svelte -->
<script>
let { row } = $props()
const menu = [
{ name: "Fried Rice", price: 15 },
{ name: "Chicken Noodles", price: 12 },
{ name: "Iced Tea", price: 5 },
]
</script>
<ul>
{#each menu as m}
<li>{#if row}{@render row(m)}{:else}{m.name}{/if}</li>
{/each}
</ul>
<!-- App.svelte -->
<MenuList>
{#snippet row(item)}
<b>{item.name}</b> ${item.price.toFixed(2)}
{/snippet}
</MenuList>
| The parent writes | The first row |
|---|---|
<MenuList /> |
Fried Rice (fallback) |
<MenuList>{#snippet row(item)}...{/snippet}</MenuList> |
Fried Rice $15.00 |
{@render row(m)}calls the snippetrowwithm. A snippet works like a function: it can take arguments.{#snippet row(item)}in the parent receives that value as the parameteritem. The name is up to you:{#snippet row(menu)}works too, as long as you writemenuinside it.- With no parameter,
{#snippet row()}, the snippet doesn't knowitem. The compiler says nothing, but at run time you getReferenceError: item is not defined.
Older code: <slot>
In Svelte 4 tutorials and repos, the same Card is written with <slot> tags, and the parent fills them through an attribute, such as <h3 slot="title">Fried Rice</h3>:
<!-- Card.svelte, Svelte 4 -->
<div class="card">
<slot name="title"><h3>New dish</h3></slot>
<slot>Nothing here yet</slot>
{#if $$slots.action}
<footer><slot name="action" /></footer>
{/if}
</div>
<slot />becomes{@render children?.()}, and a fallback between<slot>and</slot>becomes an{:else}branch.<slot name="title">in the child, plusslot="title"on the parent's element, becomes the snippettitle.$$slots.actionbecomes{#if action}.<slot item={m}>in the child, pluslet:itemin the parent, becomes{@render row(m)}and{#snippet row(item)}.- Svelte 5 still runs
<slot>in a component without runes. In a runes component, the compiler warnsslot_element_deprecated.
⚠️ Slot content that lands wrong or doesn't show
- A fallback outside
{:else}.{@render children?.()} Default contentputs the text beside the hole, so it always shows even when the parent sends content. The fallback goes in the{:else}branch; the first exercise checks for that shape. {#snippet}too deep.<Card><div>{#snippet title()}...{/snippet}</div></Card>doesn't sendtitleto Card. The snippet only lives inside that<div>, so Card shows theNew dishfallback plus an empty<div>. Write{#snippet}directly between the component's tags.- Expecting a snippet that wasn't sent to remove its wrapper.
<footer>stays. Wrap it in{#if action}. - Styling the parent's content from the child.
p { color: red; }in Card doesn't reach the<p>App sends, and the compiler warnsUnused CSS selector "p". Style that content in App.
TL;DR: A slot is a hole in a component that the parent fills with markup. In Svelte 5, the content between <Card> and </Card> arrives as the children prop, and Card places it with {@render children?.()}. A fallback goes in an {:else} branch, such as {#if children}{@render children()}{:else}Nothing here yet{/if}, and shows only when the parent sends nothing. A named slot becomes a snippet: {#snippet title()}...{/snippet} directly inside the component's tag, then {@render title()} in the child. A snippet that wasn't sent doesn't remove its wrapper, so check with {#if action}. The child hands data out through a snippet parameter: {@render row(m)} in the child, {#snippet row(item)} in the parent. Svelte 4 code writes all of this the old way (see the Older code section). The exercises: a Card with {#if children}{@render children()}{:else}Default content{/if}, and an Alert with a title snippet (fallback Warning inside <h3>) plus children (fallback No message.).
⚡ children: content from the parent
Write:
<!-- Card.svelte -->
<script>
let { children } = $props()
</script>
<div class="card">
{@render children?.()}
</div>
Meaning: Everything the parent writes between <Card> and </Card> reaches Card as the children prop. {@render children?.()} places it right there. This is the default slot.
⚡ A fallback in the {:else} branch
Write:
<div class="card">
{#if children}{@render children()}{:else}Nothing here yet{/if}
</div>
Guess first: the parent writes <Card> </Card>, with only a space inside. What does the card show?
Nothing here yet. Whitespace alone doesn't count as content, so children is empty and the {:else} branch shows.
Meaning: Fallback content goes in the {:else} branch of {#if children}, so it only shows when the parent sends nothing.
⚡ A named slot becomes a snippet
Write:
<Card>
{#snippet title()}
<h3>Fried Rice</h3>
{/snippet}
<p>$15.00</p>
</Card>
Meaning: A {#snippet title()} written directly between the <Card> tags becomes a prop named title, and Card places it with {@render title()}. Whatever isn't wrapped in a snippet goes into children.