- A page shell (
<html>,<head>and<body>tags) - A
<slot />to specify where individual page content should be injected
<html> element must be the parent of all other elements in the component.
Layout components are commonly placed in a src/layouts directory in your project for organization, but this is not a requirement; you can choose to place them anywhere in your project. You can even colocate layout components alongside your pages by prefixing the layout names with _.
Sample Layout
src/layouts/MySiteLayout.astro
src/pages/index.astro
Using TypeScript with Layouts
Any Astro layout can be modified to introduce type safety and autocompletion by providing the types for your props:src/components/MyLayout.astro
Markdown Layouts
Page layouts are especially useful for individual Markdown pages which otherwise would not have any page formatting. Astro provides a speciallayout frontmatter property intended for individual .md files located within src/pages/ using file-based routing to specify which .astro component to use as the page layout. This component allows you to provide <head> content like meta tags and styles for the Markdown page. By default, this specified component can automatically access data from the Markdown file.
This is not recognized as a special property when using content collections to query and render your content.
src/pages/page.md
- The
frontmatterprop to access the Markdown page’s frontmatter and other data - A default
<slot />to indicate where the page’s Markdown content should be rendered
src/layouts/BlogPostLayout.astro
Props type with the MarkdownLayoutProps helper:
src/layouts/BlogPostLayout.astro
Markdown Layout Props
A Markdown layout will have access to the following information viaAstro.props:
file- The absolute path of this file (e.g./home/user/projects/.../file.md)url- The URL of the page (e.g./en/guides/markdown-content)frontmatter- All frontmatter from the Markdown or MDX documentfrontmatter.file- The same as the top-levelfilepropertyfrontmatter.url- The same as the top-levelurlproperty
headings- A list of headings (h1 -> h6) in the Markdown or MDX document with associated metadata. This list follows the type:{ depth: number; slug: string; text: string }[]rawContent()- A function that returns the raw Markdown document as a stringcompiledContent()- An async function that returns the Markdown document compiled to an HTML string
Importing Layouts Manually (MDX)
You can also use the special Markdown layout property in the frontmatter of MDX files to passfrontmatter and headings props directly to a specified layout component in the same way.
To pass information to your MDX layout that does not (or cannot) exist in your frontmatter, you can instead import and use a <Layout /> component. This works like any other Astro component, and will not receive any props automatically. Pass it any necessary props directly:
src/pages/posts/first-post.mdx
Astro.props in your layout, and your MDX content will be injected into the page where your <slot /> component is written:
src/layouts/BaseLayout.astro
Nesting Layouts
Layout components do not need to contain an entire page worth of HTML. You can break your layouts into smaller components, and combine layout components to create even more flexible, page templates. This pattern is useful when you want to share some code across multiple layouts. For example, aBlogPostLayout.astro layout component could style a post’s title, date and author. Then, a site-wide BaseLayout.astro could handle the rest of your page template, like navigation, footers, SEO meta tags, global styles, and fonts. You can also pass props received from your post to another layout, just like any other nested component.
src/layouts/BlogPostLayout.astro