Layout components help you organize the look and structure of your website. They let you reuse common parts like headers and footers easily.
Layout components in Astro
--- // Import your layout component import Layout from '../layouts/BaseLayout.astro'; --- <Layout> <h1>Your page content here</h1> </Layout>
Use import to bring in your layout component.
Wrap your page content inside the layout tags to apply the layout.
MainLayout to wrap homepage content.--- import MainLayout from '../layouts/MainLayout.astro'; --- <MainLayout> <p>Welcome to the homepage!</p> </MainLayout>
BlogLayout for consistent blog styling.--- import BlogLayout from '../layouts/BlogLayout.astro'; --- <BlogLayout> <article> <h2>Blog Post Title</h2> <p>Blog post content goes here.</p> </article> </BlogLayout>
This example shows a simple layout component BaseLayout with header, footer, and a slot for page content. The homepage uses this layout to keep consistent structure.
--- // src/layouts/BaseLayout.astro --- <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>My Site</title> </head> <body> <header> <h1>My Website Header</h1> <nav> <a href="/">Home</a> | <a href="/about">About</a> </nav> </header> <main> <slot /> </main> <footer> <p>Ā© 2024 My Website</p> </footer> </body> </html> --- // src/pages/index.astro --- import BaseLayout from '../layouts/BaseLayout.astro'; --- <BaseLayout> <h2>Welcome to the homepage!</h2> <p>This is the main content of the homepage.</p> </BaseLayout>
Use the <slot /> tag inside your layout to show the page content.
Layouts help keep your site consistent and easier to update.
Remember to import your layout component at the top of your page file.
Layout components wrap page content to keep design consistent.
Use import and wrap your content inside layout tags.
The <slot /> tag shows where page content appears inside the layout.
Practice
Solution
Step 1: Understand what layout components do
Layout components are used to wrap page content to maintain a consistent look and feel across multiple pages.Step 2: Compare with other options
Fetching data, handling user input, and defining routes are not the main roles of layout components.Final Answer:
To wrap page content and keep design consistent across pages -> Option DQuick Check:
Layout = consistent design wrapper [OK]
- Confusing layout with data fetching
- Thinking layouts handle user events
- Mixing layout with routing logic
Solution
Step 1: Identify correct import and usage
In Astro, you import the layout component and wrap your page content inside its tags.Step 2: Check other options for syntax errors
Calling layout as a function or using tags without import is incorrect. Adding attributes to<body>is not how layouts work.Final Answer:
Import layout and wrap content with <Layout>...</Layout> -> Option AQuick Check:
Import + wrap content = correct layout use [OK]
- Trying to call layout as a function
- Using layout tags without importing
- Misusing HTML attributes for layouts
---
// Layout.astro
---
Site Header
<slot />
And this page code:
--- import Layout from '../layouts/Layout.astro'; --- <Layout> <main>Welcome to my site</main> </Layout>
What will the rendered HTML output look like?
Solution
Step 1: Understand how <slot /> works in layout
The <slot /> tag is replaced by the page content wrapped inside the layout.Step 2: Replace <slot /> with page content
The page content is <main>Welcome to my site</main>, so it appears between header and footer.Final Answer:
<html><body><header>Site Header</header><main>Welcome to my site</main><footer>Site Footer</footer></body></html> -> Option AQuick Check:
Slot replaced by page content =Site Header Welcome to my site [OK]
- Leaving tag as is in output
- Placing page content outside footer
- Omitting header or footer
--- import Layout from '../layouts/Layout' --- <Layout> <h1>Hello</h1> </Layout>
What is the most likely cause of the error?
Solution
Step 1: Check import statement syntax
The import is missing the file extension '.astro', which is required in Astro imports.Step 2: Understand Astro import rules
Astro requires full file paths including extensions for components, so '../layouts/Layout.astro' is correct, but '../layouts/Layout' is not.Final Answer:
Layout component file path is incorrect or missing file extension -> Option CQuick Check:
Missing file extension causes import error [OK]
- Assuming semicolon is required in Astro imports
- Forgetting to import layout before use
- Thinking page content must be inside <main>
Solution
Step 1: Understand layout usage for conditional design
Layouts wrap pages, so to conditionally add sidebar, create separate layouts for each design.Step 2: Evaluate other options
Adding if inside layout is complex and not recommended; passing sidebar via slot is not typical; adding sidebar in pages breaks consistency.Final Answer:
Create two layouts: one with sidebar and one without; import and use the right one per page -> Option BQuick Check:
Use different layouts for different page designs [OK]
- Trying to conditionally render sidebar inside one layout
- Misusing slots to pass sidebar content
- Adding sidebar directly in pages breaking consistency
Ready to prove it?
Pass the test and this pattern moves unknown
