Slot fallback content shows default content when no content is provided by the parent. It helps keep components useful and clear.
You want a reusable component that can show default text or elements if no custom content is given.
You build a card component that should display a message if no title or body is passed.
You create a modal that shows a default button if the user does not provide one.
You want to avoid empty spaces in your UI when slot content is missing.
Syntax
Svelte
<slot>Fallback content here</slot>
The fallback content is placed between the tags inside the component.
If the parent component provides content for the slot, the fallback is ignored.
Examples
This slot shows 'Default text' if no content is passed from the parent.
Svelte
<slot>Default text</slot>
The slot will show a button by default unless the parent provides its own content.
Svelte
<slot><button>Click me</button></slot>
Useful for showing a loading message if no content is given yet.
Svelte
<slot>Loading...</slot>
Sample Program
This example shows a Child component with a slot that has fallback content. In Example 1, no content is passed, so the fallback text appears. In Example 2, the parent passes custom content, so the fallback is replaced.
Svelte
<!-- Child.svelte -->
<script>
// No script needed here
</script>
<div class="box">
<slot>Default fallback content</slot>
</div>
<style>
.box {
padding: 1rem;
border: 2px solid #007acc;
border-radius: 0.5rem;
font-family: sans-serif;
color: #007acc;
}
</style>
<!-- App.svelte -->
<script>
import Child from'./Child.svelte';
</script>
<h2>Example 1: No slot content</h2>
<Child />
<h2>Example 2: With slot content</h2>
<Child>
<p>This is custom content passed from the parent.</p>
</Child>
OutputSuccess
Important Notes
Fallback content only appears if the parent does not provide anything for that slot.
You can style fallback content just like any other content inside the component.
Summary
Slots let you insert content into components from outside.
Fallback content shows a default if no content is given.
This keeps your UI clear and avoids empty spaces.
Practice
(1/5)
1. What does fallback content in a Svelte slot do?
easy
A. It disables the slot from receiving content.
B. It hides the slot completely when empty.
C. It duplicates the slot content automatically.
D. It shows default content if no slot content is provided.
Solution
Step 1: Understand slot usage in Svelte
Slots allow external content to be inserted into a component.
Step 2: Role of fallback content
If no content is passed to the slot, fallback content is displayed instead.
Final Answer:
It shows default content if no slot content is provided. -> Option D
Quick Check:
Fallback content = default display when slot empty [OK]
Hint: Fallback content appears only if slot is empty [OK]
Common Mistakes:
Thinking fallback hides the slot
Assuming fallback duplicates content
Believing fallback disables the slot
2. Which of the following is the correct syntax to provide fallback content inside a Svelte slot?
easy
A. Fallback text here
B.
C. {Fallback text here}
D. Fallback text here
Solution
Step 1: Recall Svelte slot syntax
Fallback content is placed between the opening and closing <slot> tags.
Step 2: Identify correct syntax
Only <slot>Fallback text here</slot> correctly defines fallback content.
Final Answer:
<slot>Fallback text here</slot> -> Option A
Quick Check:
Fallback inside slot tags = Fallback text here [OK]
The <slot> tag is missing the closing slash on the closing tag (</slot>).
Step 2: Validate HTML correctness
Proper closing tags are required; missing slash causes syntax error.
Final Answer:
Missing closing tag slash on <slot> element -> Option B
Quick Check:
Proper closing tags = Missing closing tag slash on <slot> element [OK]
Hint: Always close slot tags properly with [OK]
Common Mistakes:
Ignoring missing slash on closing tag
Thinking fallback must be wrapped in div
Believing extra content after slot is invalid
5. You want a Svelte component to show a default message if no slot content is passed, but also include a button that only appears when slot content is provided. How can you achieve this?
hard
A.
Click me
B. Default message
{#if !$$slots.default}
Click me
{/if}
C. Default message
{#if $$slots.default}
Click me
{/if}
D. Default message
Click me
Solution
Step 1: Use fallback content inside <slot>
Placing 'Default message' inside <slot> ensures it shows if no content is passed.
Step 2: Conditionally show button with $$slots.default
Using {#if $$slots.default} checks if slot content exists, showing button only then.
Final Answer:
<slot>Default message</slot>
{#if $$slots.default}
<button>Click me</button>
{/if} -> Option C