🔥 All-Access Pass — $199 USD (Regular $499) · Unlock all 2,836 Astro themes ➔
komal.nakrani
Get All-Access Pass ($199)
ThemesCategoriesDocsBlogTips & TricksPricingGet All-Access Pass ($199)
Components & Islands••By Komal Nakrani

Astro Slots and Named Slots: A Practical Guide

Astro slots explained with code: default and named slots, fallback content, Fragment slots, Astro.slots.has(), passing slots through layouts, and slots in React.

#Astro#Astro Slots#Components#Layouts

Astro slots let a component render content passed in by its parent. Put <slot /> in the component’s template where children should go; add <slot name="header" /> for a named slot and fill it from the parent with slot="header" on any element. Astro also gives you fallback content, Astro.slots.has() to check whether a slot was filled, and Astro.slots.render() to work with slot content as a string.

This guide covers each pattern with Astro 7 code, then the layout and framework-component cases where slots behave in ways people don’t expect.

Key Takeaways

  • <slot /> renders the parent’s children; anything without a slot attribute goes to the default slot.
  • Named slots use <slot name="x" /> in the component and slot="x" in the parent.
  • Content inside <slot>...</slot> is a fallback shown only when the slot is empty.
  • Wrap several elements in <Fragment slot="x"> to fill a named slot without an extra wrapper.
  • Astro.slots.has('x') lets you skip wrapper markup when a slot is empty.

The default slot

A component with <slot /> renders whatever the parent places between its tags:

---
// src/components/Panel.astro
---
<section class="panel">
  <slot />
</section>

<style>
  .panel { padding: 1.5rem; border: 1px solid #e5e5e5; border-radius: 12px; }
</style>
---
// src/pages/index.astro
import Panel from '../components/Panel.astro';
---
<Panel>
  <h2>Shipping</h2>
  <p>Orders leave within two business days.</p>
</Panel>

The <h2> and <p> render inside the <section>. Children can be HTML, other components, or expressions like {items.map(...)}. Slot content is rendered by the parent, so it has access to the parent’s variables, not the child’s.

Named slots

When a component has more than one place for content, give each slot a name:

---
// src/components/Card.astro
---
<article class="card">
  <header class="card-header">
    <slot name="header" />
  </header>
  <div class="card-body">
    <slot />
  </div>
  <footer class="card-footer">
    <slot name="footer" />
  </footer>
</article>

Fill them with the slot attribute:

---
// src/pages/pricing.astro
import Card from '../components/Card.astro';
---
<Card>
  <h2 slot="header">Pro plan</h2>
  <p>Everything in Starter, plus custom domains.</p>
  <p>Priority email support.</p>
  <a slot="footer" href="/checkout/">Choose Pro</a>
</Card>

The two <p> elements have no slot attribute, so they go to the default slot. Order in the parent doesn’t matter; each element lands in the slot it names. If you pass content to a slot name the component doesn’t define, it isn’t rendered.

Fallback content

Anything placed inside a <slot> element is a fallback, rendered only when the parent passes nothing for that slot:

---
// src/components/Card.astro
---
<article class="card">
  <slot name="header">
    <h2>Untitled</h2>
  </slot>
  <slot />
  <slot name="footer">
    <a href="/contact/">Get in touch</a>
  </slot>
</article>

<Card><p>Body only</p></Card> now renders with an “Untitled” heading and the default footer link. Fallbacks work for the default slot too.

Filling a named slot with several elements

The slot attribute goes on a single element. To put several elements into one named slot without adding a wrapper <div>, use <Fragment>:

---
// src/pages/pricing.astro
import Card from '../components/Card.astro';
---
<Card>
  <Fragment slot="footer">
    <a href="/checkout/">Choose Pro</a>
    <a href="/compare/">Compare plans</a>
  </Fragment>
  <p>Everything in Starter, plus custom domains.</p>
</Card>

Fragment is available in every .astro file without an import. Its children render in the slot with nothing around them, which matters for flex and grid layouts where an extra wrapper would break the styling.

Checking whether a slot was filled

Wrapper markup around an empty slot leaves stray padding and borders. Astro.slots.has() returns true if the parent passed content for that slot:

---
// src/components/Card.astro
const hasFooter = Astro.slots.has('footer');
---
<article class="card">
  <slot />
  {hasFooter && (
    <footer class="card-footer">
      <slot name="footer" />
    </footer>
  )}
</article>

The default slot is called 'default': Astro.slots.has('default').

Rendering slot content as a string

Astro.slots.render() returns the slot’s HTML as a string. That’s useful when you need to inspect or transform it, for example to build an excerpt or a title attribute:

---
// src/components/Quote.astro
const html = Astro.slots.has('default') ? await Astro.slots.render('default') : '';
const plain = html.replace(/<[^>]+>/g, '').trim();
---
<blockquote title={plain.slice(0, 120)}>
  <Fragment set:html={html} />
</blockquote>

If a parent passes a function as children, render() accepts an array of arguments to call it with. This lets a component hand data back to the parent’s markup:

---
// src/components/List.astro
interface Props {
  items: string[];
}
const { items } = Astro.props;
const rendered = await Promise.all(
  items.map((item, i) => Astro.slots.render('default', [item, i]))
);
---
<ul>
  {rendered.map((html) => <li set:html={html} />)}
</ul>
---
// src/pages/index.astro
import List from '../components/List.astro';
---
<List items={['Astro', 'Tailwind CSS', 'MDX']}>
  {(item: string, i: number) => <strong>{i + 1}. {item}</strong>}
</List>

Use this sparingly. For most components, plain props are easier to read than render callbacks.

Slots in layouts

Layouts are where slots do most of their work. A base layout typically has a default slot for the page body and named slots for optional head tags or sidebars:

---
// src/layouts/BaseLayout.astro
interface Props {
  title: string;
}
const { title } = Astro.props;
---
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>{title}</title>
    <slot name="head" />
  </head>
  <body>
    <main><slot /></main>
    {Astro.slots.has('sidebar') && <aside><slot name="sidebar" /></aside>}
  </body>
</html>
---
// src/pages/about.astro
import BaseLayout from '../layouts/BaseLayout.astro';
---
<BaseLayout title="About">
  <meta slot="head" name="robots" content="noindex" />
  <h1>About</h1>
  <p>We build fast websites.</p>
</BaseLayout>

Passing slots through nested layouts

When a BlogLayout wraps BaseLayout, a named slot from the page has to be forwarded explicitly. Put a <slot> with both name and slot attributes inside the inner component call:

---
// src/layouts/BlogLayout.astro
import BaseLayout from './BaseLayout.astro';
const { title } = Astro.props;
---
<BaseLayout title={title}>
  <slot name="head" slot="head" />
  <article>
    <slot />
  </article>
</BaseLayout>

name="head" receives the page’s head content, and slot="head" passes it on to BaseLayout’s head slot. Without this line, the page’s slot="head" content is silently dropped. The Astro components guide covers layout composition further.

Slots and framework components

You can pass children to React, Preact, Solid, Vue and Svelte components from an .astro file:

  • React, Preact, Solid: the default slot arrives as children. Named slots arrive as top-level props with the same name; kebab-case names such as slot="social-links" become camelCase props like socialLinks.
  • Vue and Svelte: default and named slots map to the framework’s own slot (or snippet) system.
---
// src/pages/index.astro
import Modal from '../components/Modal.jsx';
---
<Modal client:visible>
  <h2 slot="title">Subscribe</h2>
  <p>One email a month. No tracking.</p>
</Modal>
// src/components/Modal.jsx
import { useState } from 'react';

export default function Modal({ title, children }) {
  const [open, setOpen] = useState(false);
  return (
    <>
      <button onClick={() => setOpen(true)}>Open</button>
      {open && (
        <div role="dialog" aria-modal="true">
          {title}
          {children}
          <button onClick={() => setOpen(false)}>Close</button>
        </div>
      )}
    </>
  );
}

One limit: slot content passed to an island is rendered to static HTML on the server. An Astro component in the slot renders fine. A framework component in the slot is only interactive if it has its own client:* directive. You also can’t pass slot content from a framework component down into an Astro component, because Astro components can’t render inside a framework component’s tree in the browser. The React in Astro guide covers this boundary.

Slots vs props

Use props for data: strings, numbers, URLs, objects. Use slots for markup the parent controls: rich text, icons, other components. A title that’s always plain text is a prop. A title that sometimes contains a <code> tag or a link is a slot.

Komal theme components use this split throughout, with props for configuration and slots for page content, so they’re straightforward to reuse; you can browse Astro themes or see the folder structure docs for where layouts live. The official reference is the Astro slots documentation.

FAQ

What is a slot in Astro?

A slot is a placeholder in a component’s template, written as <slot />, where content passed by the parent component is rendered. It’s how layouts and wrapper components accept children.

How do named slots work in Astro?

Add <slot name="footer" /> in the component and put slot="footer" on the element you pass from the parent. Elements without a slot attribute go to the default slot.

How do I check if a slot is empty in Astro?

Use Astro.slots.has('name') in the component’s frontmatter or template. It returns false when the parent passed nothing for that slot, so you can skip wrapper elements.

How do I pass multiple elements to a named slot?

Wrap them in <Fragment slot="name">...</Fragment>. The fragment adds no element of its own, so the children render directly inside the slot.

Why is my named slot not rendering in a nested layout?

The intermediate layout has to forward it. Add <slot name="x" slot="x" /> inside its call to the outer layout; otherwise the named content from the page is dropped.