0Pricing
Sveltejs Academy · Lesson

Named Slots with slot="name"

Define multiple insertion points in a component using named slots.

Named Slots with slot="name" is a free Sveltejs Academy lesson on CoddyKit — 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, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.

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, and the Sveltejs Academy course includes 4 lessons in total. 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.

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; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

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