Astro nested layouts are layouts that use other layouts. Keep one BaseLayout.astro that owns <html>, <head> and the site header and footer, then build section layouts such as BlogPostLayout.astro or DocsLayout.astro that import BaseLayout, pass its props through, and put their own structure inside its <slot />. Pages pick the most specific layout, and every page still gets the same head tags.
Key Takeaways
- Layouts are ordinary Astro components; nesting is just one component rendering another.
- Only the base layout should output
<html>,<head>and<body>.- Pass
titleanddescriptiondown so every page sets its own SEO tags.- Named slots let pages fill areas like a sidebar or extra
<head>tags.
The base layout
---
// src/layouts/BaseLayout.astro
import Header from '../components/Header.astro';
import Footer from '../components/Footer.astro';
import '../styles/global.css';
interface Props { title: string; description: string }
const { title, description } = Astro.props;
---
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>{title}</title>
<meta name="description" content={description} />
<link rel="canonical" href={new URL(Astro.url.pathname, Astro.site)} />
<slot name="head" />
</head>
<body>
<Header />
<main id="main"><slot /></main>
<Footer />
</body>
</html>
The named head slot lets inner layouts and pages add tags like JSON-LD or a preload without editing the base.
A section layout on top
---
// src/layouts/BlogPostLayout.astro
import BaseLayout from './BaseLayout.astro';
interface Props { title: string; description: string; pubDate: Date }
const { title, description, pubDate } = Astro.props;
---
<BaseLayout title={title} description={description}>
<meta slot="head" property="og:type" content="article" />
<article class="post">
<header>
<h1>{title}</h1>
<time datetime={pubDate.toISOString()}>{pubDate.toLocaleDateString('en-US', { dateStyle: 'long' })}</time>
</header>
<div class="prose"><slot /></div>
</article>
</BaseLayout>
The section layout doesn’t repeat <html> or <head>. It wraps its own markup in BaseLayout and forwards the slot.
A layout with a sidebar slot
---
// src/layouts/DocsLayout.astro
import BaseLayout from './BaseLayout.astro';
import DocsNav from '../components/DocsNav.astro';
interface Props { title: string; description: string }
const { title, description } = Astro.props;
const hasAside = Astro.slots.has('aside');
---
<BaseLayout title={`${title} | Docs`} description={description}>
<div class:list={['docs', { 'docs-with-aside': hasAside }]}>
<DocsNav />
<div class="docs-content"><slot /></div>
{hasAside && <aside><slot name="aside" /></aside>}
</div>
</BaseLayout>
Pages fill the sidebar only when they need it:
---
// src/pages/docs/install.astro
import DocsLayout from '../../layouts/DocsLayout.astro';
---
<DocsLayout title="Install" description="Install the CLI and create a project.">
<h1>Install</h1>
<p>Run the installer, then start the dev server.</p>
<nav slot="aside">On this page: Requirements, Install, Next steps</nav>
</DocsLayout>
Astro.slots.has() skips the empty column when a page has no aside; see checking for slot content.
Forwarding a slot through two layouts
If a page fills a named slot that the base layout defines, the middle layout must pass it along. Re-slot it explicitly:
<!-- inside BlogPostLayout.astro -->
<BaseLayout title={title} description={description}>
<slot name="head" slot="head" />
<article><slot /></article>
</BaseLayout>
<slot name="head" slot="head" /> takes the page’s head content and places it in the base layout’s head slot.
Markdown and MDX pages
Markdown files in src/pages/ can choose a layout in frontmatter, and the layout receives the frontmatter as Astro.props.frontmatter:
---
# src/pages/about.md
layout: ../layouts/PageLayout.astro
title: About
description: Who we are.
---
For content collections, the page route imports the layout and renders <Content /> inside it, which is the usual setup for blogs. The content collections guide shows that pattern.
Keep the nesting shallow
Two levels (base plus section) cover most sites; three is the practical limit before props become hard to trace. If a layout only adds one wrapper div, it’s probably a component, not a layout.
The pages docs and folder structure docs show how layouts are organised in a Komal theme, and our Astro components guide covers slots in general. Every one of our Astro themes ships a base layout with section layouts on top.
FAQ
Can an Astro layout use another layout?
Yes. Layouts are components, so a section layout imports the base layout, wraps its own markup in it and forwards the default slot.
Where should the head tags go in nested layouts?
In the base layout only. Inner layouts pass title and description as props and add extra tags through a named head slot.
How do I pass a named slot through two layouts in Astro?
In the middle layout, render <slot name="x" slot="x" /> inside the outer layout. It forwards the page’s content for slot x into the outer layout’s slot of the same name.