---
title: "How do I add Open Graph tags so my links show a preview?"
description: "Open Graph meta tags tell chat apps and social sites which title, description and image to show when someone shares your link. Here are the tags you need and how to test them."
url: https://www.danny.engineering/answer/html/how-do-i-add-open-graph-tags-so-my-links-show-a-preview
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T20:30:00.000Z
tags: ["HTML"]
---

# How do I add Open Graph tags so my links show a preview?

## The short answer


Add a few `meta` tags with a `property` attribute to the head of each page. Use an absolute URL for the image, ideally 1200 by 630 pixels. Add `twitter:card` so X shows a large image.


```html
<head>
  <meta property="og:type" content="article">
  <meta property="og:title" content="How we cut our build time in half">
  <meta property="og:description" content="Three changes that took our CI from 12 minutes to 6.">
  <meta property="og:url" content="https://example.com/blog/faster-builds">
  <meta property="og:image" content="https://example.com/og/faster-builds.png">
  <meta property="og:image:alt" content="A chart showing build time dropping from 12 to 6 minutes">
  <meta property="og:site_name" content="Example Engineering">
  <meta name="twitter:card" content="summary_large_image">
</head>
```


## Why it happens


When you paste a link into Slack, WhatsApp, LinkedIn, Discord or similar, a bot from that service fetches the URL and reads the HTML head. It looks for Open Graph tags, a format originally created by Facebook that most services now support. If it finds none, it falls back to the `title` element and perhaps the first image it can find, or shows a bare link.


These bots usually do not run JavaScript. If your tags are added by client-side code after the page loads, the bot never sees them. The tags must be in the HTML that the server sends.


## How to fix it


### Add the core tags to every page


Four properties form the minimum the Open Graph protocol asks for: `og:title`, `og:type`, `og:image` and `og:url`. Add `og:description` as well, because almost every service shows it.

- `og:title`: the headline for the card. It can be shorter than your page `title`, without the site name.
- `og:type`: use `website` for most pages and `article` for blog posts.
- `og:url`: the canonical URL of the page, absolute.
- `og:image`: an absolute HTTPS URL to the preview image.
- `og:description`: one or two sentences. You can reuse your meta description.

Note that Open Graph uses `property`, not `name`. Many parsers accept either, but `property` is what the spec defines.


### Get the image right


The image is what makes a preview stand out, and it is the part that most often breaks.

- Use 1200 by 630 pixels, a ratio of about 1.91 to 1. It crops reasonably on most services.
- Keep important text away from the edges, because some apps crop to a square.
- Use PNG or JPEG. Support for WebP and AVIF varies between services, so a JPEG or PNG is the safe choice.
- Keep the file reasonably small, a few hundred kilobytes at most. Some bots give up on large files.
- Make sure the URL is public, not behind a login, and not blocked by `robots.txt`.

You can give the dimensions so services can lay out the card before the image downloads:


```html
<meta property="og:image:width" content="1200">
<meta property="og:image:height" content="630">
```


### Add the X card type


X reads Open Graph tags for the title, description and image, but uses its own `twitter:card` tag to decide the layout. Without it you get a small summary card or none. `summary_large_image` gives the wide image layout.


### Set them from your framework


If you use a framework, set these tags through its metadata system so they are rendered on the server. In Next.js, for example, export a `metadata` object or a `generateMetadata` function with an `openGraph` field, and Next.js writes the tags for you.


```typescript
export const metadata = {
  title: 'How we cut our build time in half',
  openGraph: {
    title: 'How we cut our build time in half',
    description: 'Three changes that took our CI from 12 minutes to 6.',
    url: 'https://example.com/blog/faster-builds',
    images: [{ url: 'https://example.com/og/faster-builds.png', width: 1200, height: 630 }],
    type: 'article',
  },
  twitter: { card: 'summary_large_image' },
};
```


### Test, then refresh the cache


Services cache previews, sometimes for days. After you change the tags, use the service's own tool to re-fetch the page. Facebook has the Sharing Debugger and LinkedIn has the Post Inspector. For other apps, adding a harmless query string such as `?v=2` to the URL forces a fresh fetch.


You can check what bots see with curl:


```bash
curl -s https://example.com/blog/faster-builds | grep -i 'og:'
```


## Common pitfalls

- **Relative image URLs.** `/og.png` does not work. Bots need the full `https://` URL.
- **Tags added by JavaScript.** Most preview bots do not run scripts, so client-only tags are invisible to them.
- **The same image and title on every page.** It works, but every shared link looks identical. Generate a per-page image if you can.
- **Old cached previews.** If your fix does not show, it is probably cached. Re-scrape with the service's tool.
- **Images behind authentication or a CDN that blocks bots.** Check your firewall or bot protection rules allow known preview crawlers.
- **Missing alt text.** Add `og:image:alt` so the image is described for people using screen readers in apps that support it.

## Related reading

- [The Open Graph protocol](https://ogp.me/)
- [Next.js: generateMetadata](https://nextjs.org/docs/app/api-reference/functions/generate-metadata)
