Learn Svelte Slots

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…

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>.

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

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>

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

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 content that lands wrong or doesn't show

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.

What you will learn