Sub topic 2
Meta Tags in the Document Head
This chapter assumes you know basic HTML structure (the head and body elements), the difference between absolute and relative URLs, and how an HTTP request returns an HTML document. You will learn where Open Graph tags live in a page, how a meta element is built, and the errors that make a crawler ignore them.
Why the head matters
A link preview is built by a crawler, a program that requests your URL and reads the HTML it gets back. The crawler does not render the page the way a browser does. It looks for meta elements inside head, reads their attributes, and uses those values to fill the preview card.
Visitors never see the head content. It holds information about the page, not content on the page. That is why it is the right place for sharing data.
Anatomy of a meta element
A meta element is a void element. It has no closing tag and no inner text. All of its data sits in attributes.
Open Graph tags use two attributes:
propertynames the value, such asog:title.contentholds the value, such as the page title.
<meta property="og:title" content="How to Brew Cold Coffee">Other meta tags you may know use name instead of property. For example, <meta name="description" ...> is for search engines. The Open Graph protocol specifies property. Many crawlers also accept name, but do not rely on that. Use property for every og: tag.
A minimal head
This example shows the four core properties plus a description, placed inside head.
<head>
<meta charset="utf-8">
<title>How to Brew Cold Coffee</title>
<meta property="og:title" content="How to Brew Cold Coffee">
<meta property="og:type" content="website">
<meta property="og:url" content="https://example.com/cold-coffee">
<meta property="og:image" content="https://example.com/images/cold-coffee-1200x630.jpg">
<meta property="og:description" content="A simple method for smooth cold-brew coffee at home.">
</head>Notice three details:
meta charsetcomes first. Crawlers read only the start of a document in some cases, and the character encoding must be known before they decode the text that follows.- The
og:urlandog:imagevalues are absolute URLs. They include the scheme (https://) and the host. A relative path such as/images/cold.jpghas no meaning to a crawler that is not on your site. - The regular
titleelement stays. Open Graph tags add to the normal page metadata. They do not replace it.
Rules for placement
- Put every Open Graph tag inside
head. Ametaelement inbodyis invalid HTML, and some crawlers stop reading at the end ofhead. - Keep the tags near the top of
head. Some crawlers read only the first part of the response, so a large inline script or style block placed earlier can push your tags out of reach. - Use each property once, with one exception. Properties that allow several values, such as
og:image, can repeat. Each newog:imagestarts a new image, and the structured properties after it belong to that image. - Write the tags in the HTML the server sends. Tags that JavaScript adds after the page loads are usually invisible to crawlers.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Preview ignores your title | Used `name="og:title"` or misspelled `property` | Use `property="og:title"` |
| Image does not appear | `content` is a relative URL | Use the full `https://` URL |
| Card shows broken text | Unescaped quote in `content` | Escape `"` as `"` |
| Tags missing from crawler view | Added by client-side JavaScript | Render them on the server |
| Tags ignored | Placed in `body` | Move them into `head` |
Check what the crawler sees
You can see the raw HTML a crawler receives by sending a request with a crawler-style user agent. This does not run JavaScript, so it matches what many crawlers see.
curl -s -A "facebookexternalhit/1.1" https://example.com/cold-coffee | grep -i 'property="og:'If the command prints your tags, they are in the server response. If it prints nothing, the tags are missing, added by JavaScript, or written with a different attribute form than the one you searched for.
Exercise
A page sets <meta name="og:image" content="/img/cover.jpg"> inside head. Name two problems with this tag and write the corrected version for a site at https://example.com.
Show answer
The tag uses name instead of property, and the image URL is relative. Corrected: <meta property="og:image" content="https://example.com/img/cover.jpg">