# Social Previews & Structured Data

> Open Graph tags control how your pages appear when shared on social media. JSON-LD structured data gives search engines machine-readable facts that can unlock rich results in SERPs.

Subject: HTML | Lesson 38 of 42 | 7 min read
Interactive lesson: https://codewithanimation.in/html/social-and-structured-data
Previous: [Title, Meta & Indexing](https://codewithanimation.in/html/title-meta-and-indexing.md) | Next: [Loading CSS, JS & Resource Hints](https://codewithanimation.in/html/loading-css-js-and-hints.md)

## What Open Graph and structured data are

When someone pastes a URL into a social platform, the platform's scraper fetches the page and reads `<meta property="og:*">` tags to build the preview card. Without them, the scraper picks random text and images, often producing a card that looks broken or misleading. The Open Graph protocol -- originally created by Facebook -- defines four required tags: `og:title`, `og:description`, `og:image`, and `og:url`. Twitter (X) has its own `twitter:card` system but falls back to Open Graph tags if the Twitter-specific ones are absent, so setting OG tags benefits both platforms.

Structured data is a way of embedding machine-readable facts directly in the page so that search engines can understand them without guessing from prose. The recommended format is JSON-LD inside a `<script type="application/ld+json">` element in `<head>`. The vocabulary comes from Schema.org: `Article`, `Product`, `FAQPage`, `BreadcrumbList`, `Event`, `Recipe`, and dozens of others. When Google can parse structured data, it may display rich results in the SERP: star ratings, event dates, FAQ dropdowns, recipe cards, and more.

## Why it matters for reach and click-through

A share without Open Graph tags generates a plain-text link. A share with a well-crafted `og:image` (at least 1200x630 pixels) generates a large image card that dominates the feed. Studies consistently show that social posts with image cards get more engagement than text-only links. The investment is four meta tags.

Structured data can earn rich results that take up more vertical space in the SERP than a standard blue link and display extra information (ratings, availability, price, event date) before the user even clicks. Rich results are not guaranteed -- Google decides when to show them -- but correctly implemented structured data is the prerequisite. Use Google's Rich Results Test and Search Console to verify and monitor them.

## How to implement Open Graph and JSON-LD

For Open Graph, add the four required tags to `<head>`: `<meta property="og:title" content="...">`, `<meta property="og:description" content="...">`, `<meta property="og:image" content="https://example.com/image.jpg">`, and `<meta property="og:url" content="https://example.com/page">`. The image should be at least 1200x630 pixels and hosted on an absolute HTTPS URL. Set `<meta property="og:type" content="website">` (or `"article"` for blog posts).

For Twitter Cards, add `<meta name="twitter:card" content="summary_large_image">` to enable the large image format. If your OG tags are already set, the title, description, and image will be read from them automatically. Add `<meta name="twitter:creator" content="@handle">` to attribute the content to an account.

For JSON-LD, add a `<script>` in `<head>` with `type="application/ld+json"`. Write a JSON object using a `@context` of `"https://schema.org"` and a `@type` matching the content (e.g. `"Article"`). Include the properties that Schema.org defines for that type. Validate with the Rich Results Test before publishing.

## Common mistake: Using a relative URL for og:image

The og:image value must be an absolute URL including the scheme and domain (`https://example.com/images/card.jpg`). Social scrapers fetch the image directly -- they do not resolve relative paths against your domain. A relative path like `/images/card.jpg` will produce a broken card image. Similarly, the og:url should be the canonical absolute URL of the page, not a relative path.

## Tip: Test social cards before publishing

Facebook provides the Sharing Debugger at developers.facebook.com/tools/debug. Twitter (X) has the Card Validator at cards-dev.twitter.com/validator. LinkedIn has its Post Inspector. All three let you paste a URL and see exactly what card will be generated, including any errors. Run these tools before every launch of a new page type. Both Facebook and Twitter cache the scraped data, so use the "Scrape Again" option after making changes.

## Try it: Try it

Edit the Open Graph fields and watch the simulated social card assemble in real time.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/html/social-and-structured-data

## Check yourself

### Question 1

Which four Open Graph properties are required for a complete social card?

- A. og:title, og:description, og:image, og:url
- B. og:title, og:image, og:type, og:site_name
- C. twitter:card, twitter:title, twitter:image, twitter:description

Correct answer: A

The Open Graph protocol defines four required properties: `og:title`, `og:description`, `og:image`, and `og:url`. Without all four, some platforms may not generate a card or may fall back to guessing.

- Why not B: og:type and og:site_name are recommended but not required. The four required tags are og:title, og:description, og:image, and og:url.
- Why not C: Those are Twitter Card tags, not the four required Open Graph tags. Twitter falls back to OG tags anyway, so the OG set is usually sufficient.

### Question 2

Where does JSON-LD structured data go and what is its purpose?

- A. In a <script type="application/ld+json"> in <head>; it gives search engines machine-readable facts
- B. In a <meta name="structured-data"> tag; it replaces the need for og:* tags
- C. In an external .jsonld file linked with <link rel="structured-data">

Correct answer: A

JSON-LD embedded in `<script type="application/ld+json">` in `<head>` is the format search engines recommend for structured data. It uses Schema.org vocabulary and can unlock rich results.

- Why not B: There is no such meta tag. JSON-LD goes in a <script> element and serves a different purpose from Open Graph -- it enables rich results in SERPs, not social cards.
- Why not C: JSON-LD is embedded inline in a <script type="application/ld+json"> element, not in an external file. Google's documentation recommends placing it in <head>.

### Question 3

What is wrong with og:image content='/images/card.jpg'?

- A. It is a relative URL; social scrapers need an absolute HTTPS URL
- B. The image path uses slashes instead of backslashes
- C. og:image only supports external CDN URLs, not local files

Correct answer: A

Social scrapers fetch `og:image` directly without context. A relative path like `/images/card.jpg` has no domain, so the scraper cannot find the image. Always use an absolute URL: `https://example.com/images/card.jpg`.

- Why not B: URL paths use forward slashes. The problem is that the URL is relative -- it starts with / not https://.
- Why not C: og:image can point to images on your own server. The requirement is that the URL is absolute (includes scheme and domain), not that it is on a CDN.

## Remember this

- Open Graph requires four tags: `og:title`, `og:description`, `og:image`, `og:url`. All must be absolute URLs.
- `og:image` must be at least 1200x630px and use an absolute HTTPS URL.
- Twitter falls back to OG tags. Adding `twitter:card` enables the large image format.
- JSON-LD in `<script type="application/ld+json">` enables Schema.org rich results in SERPs.
