What are Layouts?
A layout is just an Astro component that wraps page content using the<slot /> element. There’s nothing special about a layout file—it’s a regular .astro component.
src/layouts/BaseLayout.astro
Using a Layout
Import and use layouts like any other component:src/pages/index.astro
The content between the opening and closing
<BaseLayout> tags is passed to the <slot /> in the layout.Props to Layouts
Pass data to layouts via props:src/layouts/BlogLayout.astro
src/pages/blog/my-post.astro
Nesting Layouts
Layouts can import and use other layouts, creating a hierarchy:src/layouts/BaseLayout.astro
src/layouts/BlogLayout.astro
src/pages/blog/post.astro
- Rendered Structure
Layout with Multiple Slots
Use named slots for more complex layouts:src/layouts/DocumentLayout.astro
src/pages/docs.astro
Markdown Layouts
Markdown and MDX files can specify a layout in their frontmatter:src/pages/blog/post.md
src/layouts/BlogLayout.astro
The
<slot /> contains the rendered markdown content.Real-World Example
Here’s a production-ready layout from the Astro source code examples:src/layouts/BaseLayout.astro
Layout Patterns
- Base Layout
- App Layout
- Content Layout
Dynamic Layouts
Conditionally render different layouts:src/layouts/ConditionalLayout.astro
Best Practices
1
Start with a base layout
Create a minimal base layout with essential HTML structure.
2
Create specialized layouts
Build specialized layouts that extend the base for different page types.
3
Use TypeScript
Type your layout props for better developer experience.
4
Keep layouts focused
Each layout should serve a specific purpose (blog, docs, landing page).
5
Use slots wisely
Named slots provide flexibility, but don’t overuse them.
Prefer composition
Nest layouts instead of duplicating code.
Default props
Provide sensible defaults for optional props.
SEO metadata
Include SEO tags in your base layout.
Consistent structure
Keep similar pages using the same layout.
Learn More
Components
Learn about Astro component basics
Content Collections
Type-safe content with automatic layouts