Sveltejs Academy · Lesson

Named Slots with slot="name"

Define multiple insertion points in a component using named slots.

Lesson 2 of 413 steps

Named Slots with slot="name" is a free Sveltejs Academy lesson on CoddyKit. This is lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Sveltejs Academy learning path, and your progress syncs across the web and the CoddyKit app. The Sveltejs Academy course includes 4 lessons in total.

Why Named Slots

Components often need multiple insertion points: a header, a body, and a footer for example.

Defining Named Slots

Add a name attribute to the slot element in the child.

<!-- Layout.svelte -->
<header><slot name="header" /></header>
<main><slot /></main>
<footer><slot name="footer" /></footer>

Filling Named Slots

From the parent, mark elements with a slot="name" attribute.

<Layout>
  <h1 slot="header">Title</h1>
  <p>Body</p>
  <p slot="footer">Copyright</p>
</Layout>

Mixing Default and Named

Content without a slot attribute fills the default slot. Named content goes to its named slot.

Order Independence

The order in the parent does not matter. Each slot is matched by name.

Multiple Elements per Slot

Wrap multiple elements in a single host with the slot attribute to send them all to the same slot.

Conditional Headers

Use {#if} in the parent to optionally fill named slots.

Common Layouts

This pattern is great for layouts with header/sidebar/main/footer.

Form Components

Form components might define slots for prefix, suffix, helper text, etc.

Self-Documentation

Slot names describe the component's API. Pick descriptive ones like title or actions.

TypeScript Helpers

You can type slots with a special $$Slots generic in TypeScript Svelte files.

Quick Check

How do you target a named slot?

Recap

Define multiple <slot name="..."> in the child and target them with slot="..." on parent elements.

Free to start

Learn HTML with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
41
Lessons
163

Frequently Asked Questions

Is the “Named Slots with slot="name"” lesson free?

Yes — the full text of “Named Slots with slot="name"” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Sveltejs Academy course, upgrade to CoddyKit PRO. The Sveltejs Academy course includes 4 lessons in total.

What will I learn in “Named Slots with slot="name"”?

Define multiple insertion points in a component using named slots. You practise Sveltejs Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Sveltejs Academy?

No prior experience is required. Sveltejs Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

How long does the “Named Slots with slot="name"” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Sveltejs Academy lesson?

Yes. Every Sveltejs Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Default Slots for Content Projection
  2. Named Slots with slot="name"
  3. Slot Fallback Content
  4. Checking Slot Content with $$slots
← Back to Sveltejs Academy