Open Graph and social metadata help websites show nice previews when shared on social media. They tell platforms like Facebook or Twitter what title, image, and description to show.
Open Graph and social metadata in Astro
<head> <meta property="og:title" content="Your Page Title" /> <meta property="og:description" content="A short description of your page." /> <meta property="og:image" content="https://example.com/image.jpg" /> <meta property="og:url" content="https://example.com/page" /> <meta name="twitter:card" content="summary_large_image" /> <meta name="twitter:title" content="Your Page Title" /> <meta name="twitter:description" content="A short description of your page." /> <meta name="twitter:image" content="https://example.com/image.jpg" /> </head>
Use property attribute for Open Graph tags and name for Twitter tags.
Place these tags inside the <head> section of your HTML or Astro component.
<head> <meta property="og:title" content="Welcome to My Site" /> <meta property="og:description" content="Learn about my projects and blog posts." /> <meta property="og:image" content="https://mysite.com/og-image.png" /> <meta property="og:url" content="https://mysite.com" /> </head>
<head> <meta property="og:title" content="Article Title" /> <meta property="og:description" content="Summary of the article." /> <meta property="og:image" content="https://mysite.com/article-image.jpg" /> <meta property="og:url" content="https://mysite.com/article" /> <meta name="twitter:card" content="summary" /> <meta name="twitter:title" content="Article Title" /> <meta name="twitter:description" content="Summary of the article." /> <meta name="twitter:image" content="https://mysite.com/article-image.jpg" /> </head>
<head> <!-- Minimal Open Graph --> <meta property="og:title" content="Simple Page" /> <meta property="og:url" content="https://simplepage.com" /> </head>
This Astro component sets Open Graph and Twitter metadata dynamically using variables. When shared, social sites show the title, description, and image you set here.
--- const pageTitle = "My Astro Blog"; const pageDescription = "Welcome to my blog where I share coding tips."; const pageImage = "https://astroblog.com/og-image.jpg"; const pageUrl = "https://astroblog.com"; --- <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>{pageTitle}</title> <meta property="og:title" content={pageTitle} /> <meta property="og:description" content={pageDescription} /> <meta property="og:image" content={pageImage} /> <meta property="og:url" content={pageUrl} /> <meta name="twitter:card" content="summary_large_image" /> <meta name="twitter:title" content={pageTitle} /> <meta name="twitter:description" content={pageDescription} /> <meta name="twitter:image" content={pageImage} /> </head> <body> <h1>{pageTitle}</h1> <p>{pageDescription}</p> </body> </html>
Open Graph tags improve how your site looks when shared but do not affect the visible page content.
Always use absolute URLs (full links starting with https://) for images and URLs in metadata.
Common mistake: forgetting to add og:url or using relative URLs, which can cause preview errors.
Open Graph and social metadata control how your site appears on social media.
Use og: tags for Facebook and general social platforms, and twitter: tags for Twitter.
Place these tags inside the <head> section of your Astro pages for best results.
Practice
og: tags in an Astro page's <head> section?Solution
Step 1: Understand the role of
og:tagsog:tags are Open Graph tags used to define how content is displayed on social media.Step 2: Identify their placement and effect
These tags go inside the<head>and control preview title, image, and description on social platforms.Final Answer:
To control how the page appears when shared on social media platforms -> Option AQuick Check:
Open Graph tags = social media preview control [OK]
og: tags shape social media previews [OK]- Confusing
og:tags with CSS or fonts - Thinking they improve page speed
- Placing tags outside the
<head>section
<head> section?Solution
Step 1: Recall Twitter card meta tag syntax
Twitter card tags usename="twitter:..."attribute, notproperty.Step 2: Check attribute order and correctness
The correct syntax is<meta name="twitter:title" content="..." />. Attribute order does not affect validity but standard is name then content.Final Answer:
<meta name="twitter:title" content="My Page Title" /> -> Option DQuick Check:
Twitter card title uses name="twitter:title" [OK]
- Using property instead of name for Twitter tags
- Mixing Open Graph and Twitter tag syntax
- Incorrect attribute order causing confusion
<head> snippet:<meta property="og:title" content="Welcome" /> <meta property="og:description" content="Best site" /> <meta name="twitter:card" content="summary_large_image" /> <meta name="twitter:title" content="Welcome" />
What will be the title shown when this page is shared on Twitter?
Solution
Step 1: Identify Twitter title meta tag
The tag<meta name="twitter:title" content="Welcome" />sets the Twitter card title.Step 2: Understand Twitter card behavior
Twitter uses its owntwitter:titletag over Open Graphog:titleif present.Final Answer:
Welcome -> Option AQuick Check:
Twitter title = twitter:title content [OK]
- Assuming Twitter uses og:title instead of twitter:title
- Confusing description or card type as title
- Ignoring the presence of twitter:title tag
<head>:<meta property="og:image" content="/images/preview.png">
But the image does not show on social shares. What is the likely problem?
Solution
Step 1: Check the image URL format
Open Graph requires absolute URLs for images to display correctly on social platforms.Step 2: Verify tag correctness
The tag uses correctpropertyattribute and closing is optional in HTML5; the main issue is the relative URL.Final Answer:
ThecontentURL is relative; it should be an absolute URL -> Option CQuick Check:
OG image URL must be absolute [OK]
- Using relative paths for og:image content
- Confusing property and name attributes
- Worrying about self-closing slash in HTML5
- Facebook uses Open Graph tags
- Twitter uses Twitter card tags
- Both show the same title and description
Which is the best way to write these tags inside
<head>?Solution
Step 1: Use correct attributes for Open Graph and Twitter tags
Open Graph tags useproperty="og:...", Twitter tags usename="twitter:...".Step 2: Ensure matching titles and descriptions for both
Set the same content values for both sets of tags to keep consistency.Final Answer:
Usepropertyfor og tags andnamefor twitter tags with matching content -> Option BQuick Check:
OG = property, Twitter = name, same content [OK]
- Mixing property and name attributes between OG and Twitter
- Using different content values causing inconsistent previews
- Omitting either OG or Twitter tags
Ready to prove it?
Pass the test and this pattern moves unknown
