To set page titles in Astro, give your base layout a title prop and build the <title> tag from a template such as `${title} | ${SITE_NAME}`, with a separate rule for the homepage. Every page passes only its own title, and the layout adds the site name, so titles stay consistent and you change the format in one place.
Key Takeaways
- Astro has no built-in title template; a layout prop does the job in a few lines.
- Put the page-specific words first and the site name last.
- Give the homepage its own descriptive title instead of the template.
- Keep most titles under about 60 characters so they are not cut off in results.
The layout
// src/site.config.ts
export const SITE_NAME = 'Example Studio';
export const HOME_TITLE = 'Example Studio: Websites for Small Teams';
---
// src/layouts/Base.astro
import { SITE_NAME, HOME_TITLE } from '../site.config';
interface Props {
title?: string;
/** Use the title exactly as given, with no site name suffix. */
rawTitle?: boolean;
}
const { title, rawTitle = false } = Astro.props;
const isHome = Astro.url.pathname === '/';
const fullTitle = isHome || !title
? HOME_TITLE
: rawTitle
? title
: `${title} | ${SITE_NAME}`;
---
<html lang="en">
<head>
<meta charset="utf-8" />
<title>{fullTitle}</title>
<meta property="og:title" content={title ?? HOME_TITLE} />
</head>
<body>
<slot />
</body>
</html>
Pages pass just their part:
---
// src/pages/pricing.astro
import Base from '../layouts/Base.astro';
---
<Base title="Pricing">
<h1>Pricing</h1>
</Base>
That renders <title>Pricing | Example Studio</title>. The rawTitle escape hatch is for pages whose title is already long and specific, such as a post where the suffix would push it past 60 characters. Open Graph gets the plain title, because platforms show og:site_name separately; see the Open Graph tip.
Titles from content collections
For posts, pass the frontmatter title through. An optional seoTitle field lets writers set a search-specific title without changing the visible heading:
---
// src/pages/blog/[id].astro (excerpt)
const { post } = Astro.props;
---
<Base title={post.data.seoTitle ?? post.data.title}>
<h1>{post.data.title}</h1>
</Base>
Add seoTitle: z.string().max(60).optional() to the collection schema so the build rejects titles that are too long. Our content collections guide covers schemas.
Writing titles that work
| Page | Example title |
|---|---|
| Homepage | Example Studio: Websites for Small Teams |
| Service page | Astro Website Development | Example Studio |
| Blog post | How to Price a Website Project | Example Studio |
| Paginated archive | Blog, Page 2 | Example Studio |
A few rules:
- Lead with the words people search for. The start of a title gets the most attention and is least likely to be cut.
- Make every title unique. Duplicate titles across pages, such as every page named βHomeβ, confuse search engines and visitors.
- Keep the separator simple. A pipe or a dash both work; pick one and use it everywhere.
- Do not stuff keywords. Search engines may rewrite titles that look spammy or do not match the page.
On paginated pages, add the page number so page 2 does not duplicate page 1βs title. You can read it from the page prop that paginate() provides.
Check your titles
After a build, list every title to spot duplicates and long ones:
# Terminal
npm run build && grep -rho '<title>[^<]*</title>' dist | sort | uniq -c | sort -rn | head
Any count above 1 is a duplicate. Our meta tags component guide moves the title, description and social tags into one component, and the pages docs show the layout structure of a theme from our catalog.
FAQ
How do I set the page title in Astro?
Render a <title> tag in the pageβs <head>, usually through a layout that receives a title prop. Astro has no separate title API; whatever is in <head> is what ships.
How long should an Astro page title be?
Aim for under about 60 characters. Search engines cut titles by pixel width rather than a fixed count, so shorter titles are safer on mobile.
Should the site name go at the start or end of the title?
At the end on most pages, so the page-specific words come first. The homepage is the exception, where the brand name usually leads.
Can Markdown pages set their own title in Astro?
Yes. Content collection entries pass their frontmatter title to the layout. Standalone Markdown pages in src/pages/ with a layout frontmatter field pass their frontmatter to the layout as frontmatter.