Skip to content
Tutorials Center
My learning

Topics

Lesson 6 of 37 ยท Pages, Layouts and Routing

Layouts and Slots in Astro

Build reusable Astro layouts with the slot element, named slots, props for titles and SEO tags, nested layouts and Markdown frontmatter layouts.

Almost every page on a site shares the same skeleton: the <html> and <head> tags, a header, a footer and some global styles. Copying that markup into each page is error-prone. In Astro you put it in a layout, which is just a component that wraps other content.

A layout is not a special file type. It is an ordinary .astro component, usually stored in src/layouts/, that uses the <slot /> element to say where the page's own content goes.

A basic layout

ASTRO
---
// src/layouts/BaseLayout.astro
const { title } = Astro.props;
---
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />
    <title>{title}</title>
  </head>
  <body>
    <header><a href="/">My Site</a></header>
    <main>
      <slot />
    </main>
    <footer>ยฉ 2026 My Site</footer>
  </body>
</html>

A page uses it by importing it and wrapping its content:

ASTRO
---
// src/pages/about.astro
import BaseLayout from "../layouts/BaseLayout.astro";
---
<BaseLayout title="About">
  <h1>About us</h1>
  <p>We make small, fast websites.</p>
</BaseLayout>

Everything between <BaseLayout> and </BaseLayout> replaces the <slot />. The title attribute arrives as a prop in Astro.props (covered fully in Props and slots).

Layout plus page

Layout head, header, footer
Slot A hole for content
1 / 2

Named slots

A single default slot is not always enough. Give extra slots a name and send content to them with a slot attribute:

ASTRO
---
// src/layouts/ArticleLayout.astro
---
<article>
  <header><slot name="header" /></header>
  <div class="body"><slot /></div>
  <aside><slot name="sidebar">Default sidebar text</slot></aside>
</article>
ASTRO
---
import ArticleLayout from "../layouts/ArticleLayout.astro";
---
<ArticleLayout>
  <h1 slot="header">Why layouts matter</h1>
  <p>This goes into the default slot.</p>
  <nav slot="sidebar">Related links</nav>
</ArticleLayout>

Anything between the <slot> tags in the layout is fallback content, shown only when the page supplies nothing for that slot. Content without a slot attribute goes into the unnamed default slot.

Passing SEO data as props

A layout is the right place to produce the <head> tags that every page needs. Accept props with sensible defaults:

ASTRO
---
interface Props {
  title: string;
  description?: string;
  image?: string;
}
const { title, description = "A fast website built with Astro.", image = "/og-default.png" } = Astro.props;
const canonical = new URL(Astro.url.pathname, Astro.site);
---
<head>
  <title>{title}</title>
  <meta name="description" content={description} />
  <link rel="canonical" href={canonical} />
  <meta property="og:title" content={title} />
  <meta property="og:image" content={new URL(image, Astro.site)} />
</head>

Astro.site comes from the site option you set in the config, which is why that setting matters. Each page then only needs <BaseLayout title="Pricing" description="Plans and prices">.

Nested layouts

A layout can wrap another layout. A blog post layout adds a title and date, then hands everything to the base layout:

ASTRO
---
// src/layouts/PostLayout.astro
import BaseLayout from "./BaseLayout.astro";
const { title, date } = Astro.props;
---
<BaseLayout title={title}>
  <article>
    <h1>{title}</h1>
    <time datetime={date}>{date}</time>
    <slot />
  </article>
</BaseLayout>

Keep nesting shallow. Two levels (base, then a section layout) cover almost every site.

Layouts for Markdown

A Markdown page can name a layout in its frontmatter. Astro then passes the page's frontmatter to the layout in Astro.props.frontmatter and puts the rendered body in the default slot. This is described in Markdown pages.

Stand-in: slot substitution

Slots are a templating idea you can model in a few lines: a layout is a function that takes named pieces of content. This stand-in implements default and named slots with fallbacks.

Node.js Stand-in: layouts and named slots

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Stand-in: nesting layouts

Because a layout is just a function from content to HTML, nesting is composition. Here a post layout feeds the base layout, exactly like PostLayout above.

Node.js Stand-in: nested layouts

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Practical tips

  • Put global CSS imports in the base layout so every page gets them: import "../styles/global.css";.
  • Keep the <html>, <head> and <body> tags in exactly one layout.
  • Prefer props for data (title, description) and slots for markup.
  • A component that renders <slot /> more than once shows the content more than once.
  • Give every layout a lang attribute on <html>; screen readers and search engines use it.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Where does the content you put between <BaseLayout> tags appear?

    The default <slot /> in the layout is replaced with the children of the layout tag.
  2. 2. How do you send content to a slot named sidebar?

    An element with the slot attribute is placed into the matching named slot.
  3. 3. When is fallback content inside a <slot> shown?

    Fallback content is a default that is replaced whenever the page supplies content.
  4. 4. Where do the title and description props reach a layout?

    Attributes you pass on a component tag are available in Astro.props inside it.

Summary

  • A layout is a normal .astro component with a <slot /> where page content goes.
  • Named slots (slot="name") allow several insertion points; slot contents act as fallbacks.
  • Layouts take props such as title and description and build the <head> and SEO tags.
  • Layouts can be nested, but keep it shallow.
  • Markdown pages choose a layout through their frontmatter.

Next: Dynamic Routes and getStaticPaths