How to Use <head> in Astro for SEO and Metadata
In Astro, you use the
<head> tag inside your component or page to add metadata like titles, descriptions, and links. Place your <head> content at the top level of your Astro file to control the document's head section easily.Syntax
The <head> tag in Astro works like the HTML head element. You put metadata tags inside it, such as <title>, <meta>, and <link>. Astro collects these tags and inserts them into the final HTML document's head.
Use it at the top level of your Astro component or page file.
astro
<head> <title>Your Page Title</title> <meta name="description" content="Page description here" /> <link rel="icon" href="/favicon.ico" /> </head>
Example
This example shows a simple Astro page that sets the page title, description, and favicon using the <head> tag. When you open this page in a browser, the tab will show the title, and search engines will see the description.
astro
--- // No frontmatter needed for this example --- <head> <title>Welcome to My Astro Site</title> <meta name="description" content="This is a simple Astro page with head metadata." /> <link rel="icon" href="/favicon.ico" /> </head> <main> <h1>Hello, Astro!</h1> <p>This page uses the <head> tag to set metadata.</p> </main>
Output
<!DOCTYPE html>
<html lang="en">
<head>
<title>Welcome to My Astro Site</title>
<meta name="description" content="This is a simple Astro page with head metadata." />
<link rel="icon" href="/favicon.ico" />
</head>
<body>
<main>
<h1>Hello, Astro!</h1>
<p>This page uses the <head> tag to set metadata.</p>
</main>
</body>
</html>
Common Pitfalls
- Placing <head> inside nested components: The
<head>tag should be at the top level of your Astro page or layout, not inside child components, or it may not work as expected. - Multiple conflicting <title> tags: Avoid adding more than one
<title>tag in the same page to prevent confusion in browsers and SEO. - Forgetting to close tags: Always close your
<meta>and<link>tags properly to keep valid HTML.
astro
<!-- Wrong: head inside a component --> --- // ChildComponent.astro --- <head> <title>Wrong Place</title> </head> <!-- Right: head at page level --> --- // Page.astro --- <head> <title>Correct Place</title> </head> <ChildComponent />
Quick Reference
Use this quick guide to remember how to add common metadata in Astro's <head>:
| Tag | Purpose | Example |
|---|---|---|
| Sets the page title shown in browser tabs | ||
| Adds metadata like description or charset | ||
| Links to resources like favicon or stylesheets | ||
| Defines character encoding |
Key Takeaways
Use the tag at the top level of your Astro page or layout to add metadata.
Include , , and tags inside for SEO and browser info.
Avoid placing inside nested components to ensure proper rendering.
Do not add multiple tags on the same page to prevent conflicts.
Always close self-closing tags like and for valid HTML.