To add Twitter cards (now X cards) in Astro, add <meta name="twitter:card" content="summary_large_image"> to your shared head component, plus twitter:site for your account handle. X falls back to your Open Graph tags for the title, description and image, so if those are already set, two or three twitter: tags are all you need for a large image preview.
Key Takeaways
twitter:cardis the one tag X requires; usesummary_large_imagefor a wide image.- Twitter tags use
name=, Open Graph tags useproperty=.- Title, description and image fall back to
og:title,og:descriptionandog:image.- The image URL must be absolute; build it from
Astro.site.
The minimal set
If your pages already have Open Graph tags (see the Open Graph tip), add these next to them:
---
// src/components/TwitterCard.astro
interface Props {
creator?: string;
}
const { creator } = Astro.props;
---
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:site" content="@examplestudio" />
{creator && <meta name="twitter:creator" content={creator} />}
twitter:site is the account for the website; twitter:creator is the author’s account, useful on blog posts with several writers. Store author handles in your authors collection and pass them in from the post layout.
The full set, without Open Graph
If you want X to show different text from other platforms, or you do not use Open Graph at all, set every field explicitly:
---
// src/components/TwitterCardFull.astro
interface Props {
title: string;
description: string;
image: string;
imageAlt: string;
}
const { title, description, image, imageAlt } = Astro.props;
---
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:site" content="@examplestudio" />
<meta name="twitter:title" content={title} />
<meta name="twitter:description" content={description} />
<meta name="twitter:image" content={new URL(image, Astro.site)} />
<meta name="twitter:image:alt" content={imageAlt} />
new URL(image, Astro.site) turns /og/post.jpg into an absolute URL, which requires site in astro.config.mjs. The config docs show where to set it.
Card types
twitter:card value |
Preview |
|---|---|
summary_large_image |
Large image above the title; best for articles and landing pages |
summary |
Small square thumbnail next to the title |
player |
Inline video or audio player; needs approval from X |
app |
App store details |
Most sites use summary_large_image everywhere. summary suits pages where the image is a logo or avatar rather than a picture.
Image rules
Use a 1.91:1 or 2:1 image, such as 1200 x 630 px, in JPG, PNG or WebP. Keep it under 5 MB; X does not render SVG. One image can serve both Open Graph and X. Our OG image generation guide builds one per post at build time.
Testing
X retired its standalone Card Validator. To check a preview, paste the URL into the post composer on X and wait for the card to appear before posting. Previews are cached, so add a query string such as ?v=2 when testing changes. Locally, run npm run build and grep the output:
# Terminal
grep -o '<meta name="twitter:[^>]*>' dist/index.html
Our meta tags component guide combines these with title, description, canonical and Open Graph in one typed component. If you are editing a theme from our catalog, add the tags in the component that renders <head> in the base layout.
FAQ
Do I need Twitter card tags if I have Open Graph tags?
You need at least twitter:card. Without it, X shows a small or text-only preview. Title, description and image can come from Open Graph.
Should I use name or property for Twitter meta tags?
name. X’s documentation uses name="twitter:...", while Open Graph uses property="og:...". X also reads property in practice, but name is the documented form.
Why does X show an old image for my page?
X caches card data. Changes appear after the cache expires, or immediately for a new URL. Add a query parameter to test, and keep image URLs stable once published.
Is it still called a Twitter card?
The tags still use the twitter: prefix and the documentation still calls them cards, even though the platform is now X.