📖 Scenario: You are building a simple website using Astro. You want to create a reusable layout component that wraps page content with a header and footer.
🎯 Goal: Build a layout component in Astro that includes a header, a footer, and a slot for page content.
📋 What You'll Learn
Create a layout component file named Layout.astro
Add a header section with the text My Website Header
Add a footer section with the text My Website Footer
Use the <slot /> element to insert page content inside the layout
Use the layout component in a page file named index.astro to wrap some sample content
💡 Why This Matters
🌍 Real World
Layout components help keep website structure consistent and make it easy to update common parts like headers and footers across many pages.
💼 Career
Understanding layout components is essential for building maintainable websites and working with modern web frameworks like Astro, React, Vue, and others.
Progress0 / 4 steps
1
Create the Layout component file
Create a new file named Layout.astro and add the basic Astro component structure with an empty div container.
Astro
Hint
Start by creating a file named Layout.astro and add a <div> element inside it.
2
Add header and footer sections
Inside the div in Layout.astro, add a <header> element with the text My Website Header and a <footer> element with the text My Website Footer.
Astro
Hint
Use semantic HTML tags <header> and <footer> to add the header and footer text.
3
Add the slot for page content
Between the <header> and <footer> in Layout.astro, add the <slot /> element to allow page content to be inserted.
Astro
Hint
Use the <slot /> element to insert page content inside the layout.
4
Use the Layout component in a page
Create a file named index.astro. Import the Layout component and use it to wrap a <p> element with the text Welcome to my website!.
Astro
Hint
Import the Layout component and wrap your page content inside it.
Practice
(1/5)
1. What is the main purpose of a layout component in Astro?
easy
A. To fetch data from an API
B. To define routes for the website
C. To handle user input events
D. To wrap page content and keep design consistent across pages
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 D
Quick Check:
Layout = consistent design wrapper [OK]
Hint: Layouts wrap content to keep pages looking the same [OK]
Common Mistakes:
Confusing layout with data fetching
Thinking layouts handle user events
Mixing layout with routing logic
2. Which of the following is the correct way to use a layout component in an Astro page?
easy
A. import Layout from '../layouts/Layout.astro'; then wrap content with <Layout>...</Layout>
B. import Layout from '../layouts/Layout.astro'; then call Layout() as a function
C. Use <layout>...</layout> without import
D. Add layout='Layout' attribute to the <body> tag
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 A
Quick Check:
Import + wrap content = correct layout use [OK]
Hint: Always import layout and wrap content inside its tags [OK]
Common Mistakes:
Trying to call layout as a function
Using layout tags without importing
Misusing HTML attributes for layouts
3. Given this layout component code:
---
// 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?
medium
A. Site HeaderWelcome to my site
B. Site HeaderWelcome to my site
C. Welcome to my site
D. Site Header
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 A
Quick Check:
Slot replaced by page content = Site HeaderWelcome to my site [OK]
Hint: Slot tag shows page content inside layout [OK]
Common Mistakes:
Leaving tag as is in output
Placing page content outside footer
Omitting header or footer
4. This Astro page code tries to use a layout but shows an error:
---
import Layout from '../layouts/Layout'
---
<Layout>
<h1>Hello</h1>
</Layout>
What is the most likely cause of the error?
medium
A. Using <Layout> without importing it
B. Missing semicolon after import statement
C. Layout component file path is incorrect or missing file extension
D. Page content must be inside a <main> tag
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 C
Quick Check:
Missing file extension causes import error [OK]
Hint: Always include .astro extension in import paths [OK]
Common Mistakes:
Assuming semicolon is required in Astro imports
Forgetting to import layout before use
Thinking page content must be inside <main>
5. You want to create a layout that adds a sidebar only on some pages. Which approach correctly applies this conditional layout in Astro?
hard
A. Add sidebar HTML directly in each page without using layouts
B. Create two layouts: one with sidebar and one without; import and use the right one per page
C. Wrap the sidebar in a tag and pass it from the page
D. Use a single layout and add an if statement inside the layout to show sidebar based on page content
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 B
Quick Check:
Use different layouts for different page designs [OK]
Hint: Use separate layouts for different page designs [OK]
Common Mistakes:
Trying to conditionally render sidebar inside one layout
Misusing slots to pass sidebar content
Adding sidebar directly in pages breaking consistency
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.