Sub topic 2
Meta tags in the document head
This chapter assumes you know basic HTML structure, including the head and body elements. It also assumes you know the difference between absolute and relative URLs. You don't need to know the Open Graph properties yet. The next chapters cover them. Here you learn the container they live in: the meta element, and the rules for where it can sit in a page.
Why the head matters for previews
A link preview crawler is a program that fetches your page over HTTP and reads the HTML. It does not look at what a browser would draw. It looks for specific elements in the head and copies their values into a preview card.
If a tag is missing from the head, malformed, or pushed out of the head by a parsing quirk, the crawler never sees it. Most "my preview is wrong" problems come from this layer, not from the Open Graph properties themselves.
What the head contains
The head element holds metadata about the document. Metadata is data that describes the page but is not part of the visible content. A typical head has:
title: the page title shown in browser tabs and search results.meta: a general-purpose element for name-and-value metadata.link: a relationship to another resource, such as a stylesheet or a canonical URL.script,style, andbase: code, styles, and a base URL.
Open Graph data uses only the meta element. Social platforms read other head elements too. Many fall back to title and the standard description meta tag when Open Graph tags are absent. That fallback is not reliable, so you will still add explicit tags.
Anatomy of a meta element
A meta element is a void element. That means it has no content and no closing tag. Everything it carries sits in attributes. Which attributes you use depends on the kind of metadata.
| Form | Attributes | Example purpose |
|---|---|---|
| Document-level setting | `charset` | Declares the character encoding |
| Name and value | `name`, `content` | Page description, viewport, Twitter/X cards |
| Property and value | `property`, `content` | Open Graph properties |
| Pragma directive | `http-equiv`, `content` | Refresh or policy settings |
Here is a head that uses the first three forms:
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="A simple method for smooth cold-brew coffee at home.">
<title>How to Brew Cold Coffee</title>
<meta property="og:title" content="How to Brew Cold Coffee">
</head>Each element does one job:
charsettells the parser how to decode the bytes. Put it early, because browsers look for it in roughly the first 1024 bytes of the document.viewportaffects mobile layout and has nothing to do with previews.descriptionis the classic search-engine description.og:titleis the first Open Graph tag. It usesproperty, notname.
name versus property
The Open Graph protocol is based on RDFa, a way of embedding structured data in HTML attributes. That is why it specifies property instead of name. Twitter/X card tags use name. In practice many parsers accept either attribute for either family. That tolerance is not guaranteed, so follow each specification:
- Use
property="og:..."for Open Graph tags. - Use
name="twitter:..."for Twitter/X card tags. - Use
name="description"for the standard description.
A common error is writing <meta name="og:title" ...>. Some crawlers accept it. Others ignore it. You can avoid the problem by writing it correctly.
The content attribute
content holds the value. Three rules apply:
- Always put the value in quotes. An unquoted value stops at the first space.
- Escape characters that would break the attribute:
&as&,"as",<as<. - Use absolute URLs for anything that points to another resource. A crawler fetches your page out of context. It has no reliable way to resolve
/images/cover.jpgthe way a browser on your site would.
<meta property="og:title" content="Tom & Jerry: "The Cat" Returns">
<meta property="og:image" content="https://example.com/images/cover.jpg">A raw double quote inside the value ends the attribute early. The rest of the title would then be parsed as strange extra attributes, and the preview would show a truncated title.
Where meta tags may appear
Meta tags must be inside the head. This sounds obvious, but the HTML parser can close the head earlier than you intended.
The parser follows a fixed algorithm. While it is "in head" mode, it accepts only certain elements: title, meta, link, script, style, base, noscript, and template. As soon as it meets something else, such as a div, a p, or a text node containing non-whitespace characters, it closes the head and opens the body. Everything after that point belongs to the body.
Look at this page:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Broken head</title>
<meta property="og:title" content="Seen by the crawler">
<div id="banner">Site notice</div>
<meta property="og:description" content="Parsed into the body">
</head>
<body>
<p>Hello</p>
</body>
</html>The div forces an implicit end of the head. The og:description meta tag is then parsed as part of the body. A browser still shows the page, so you notice nothing. A crawler that reads strictly from the head can miss the description. Some crawlers read meta tags anywhere in the document, so the behavior varies by platform. This inconsistency makes the bug hard to track down.
Templating systems cause this often. A layout includes a partial that outputs a banner or a tracking pixel inside the head. The markup looks fine in the source, but the parsed structure is different.
Order, duplicates, and size
Order
The order of different meta tags does not matter, with two exceptions:
- Put
charsetfirst, or at least within the first 1024 bytes, so the encoding is known before any non-ASCII text appears. - Put the Open Graph tags before large inline scripts and styles. Some crawlers stop reading after a limited number of bytes. If your head holds 200 KB of inline CSS before the tags, a size-limited crawler may never reach them. The exact limits are not documented consistently across platforms, so keep the tags near the top and keep the head small.
Order matters in one more case: repeated properties. For properties that can appear more than once, such as og:image, the order defines the list. The first one is usually treated as the primary value. Structured properties like og:image:width must follow the og:image they describe.
Duplicates
If you write two og:title tags, crawlers differ. Some take the first, some the last. Duplicates often come from a CMS plugin and a hand-written template both adding tags. Open the rendered source and make sure each single-valued property appears once.
Case and spelling
Property names are lowercase and use a colon as a separator. og:Title and OG:title are not the same as og:title in strict parsers. Spell them exactly as the specification does.
Seeing what the crawler sees
The browser's "Elements" panel shows the DOM after JavaScript has run and after the parser has repaired errors. A crawler usually reads the raw HTTP response. To see that response, fetch it yourself:
curl -s -A "facebookexternalhit/1.1" https://example.com/cold-coffee | grep -i '<meta'The -s flag hides the progress bar. -A sets the user agent, which some servers use to decide what to return. grep -i '<meta' keeps every meta line. This only works if each tag sits on its own line. If your build tool minifies the HTML onto a single line, grep prints the whole document. For that case use -o to print only the matches:
curl -s -A "facebookexternalhit/1.1" https://example.com/cold-coffee | grep -io '<meta[^>]*>'Compare this output with what you see in the browser's inspector. Differences point to one of two causes:
- JavaScript adds the tags after load. The raw response does not contain them, so crawlers that don't run scripts never see them. The crawlers chapter covers this in detail.
- The parser moved tags out of the head, as in the broken example above.
A clean head checklist
Use this list when you review a page:
<!doctype html>at the top, so the parser uses standards mode.charsetwithin the first 1024 bytes.- Only metadata elements inside the
head. - Every Open Graph tag uses
propertyand everycontentvalue is quoted and escaped. - URLs in
contentare absolute and usehttps. - Each single-valued property appears once.
- The tags appear early in the head.
A head that follows it looks like this:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>How to Brew Cold Coffee</title>
<meta name="description" content="A simple method for smooth cold-brew coffee at home.">
<meta property="og:title" content="How to Brew Cold Coffee">
<meta property="og:url" content="https://example.com/cold-coffee">
<link rel="stylesheet" href="/styles.css">
</head>
<body>
<h1>How to Brew Cold Coffee</h1>
</body>
</html>Notice that the stylesheet link comes after the meta tags. A link to an external file adds almost nothing to the byte count, but the habit of keeping metadata first protects you when the head grows.
Common mistakes in this layer
| Symptom | Likely cause | Fix |
|---|---|---|
| Title cut off at a quote mark | Unescaped `"` in `content` | Escape values before printing them |
| Tags visible in the browser, absent in crawler output | Tags added by JavaScript | Render them on the server |
| Some tags ignored | A `div` or text closed the head early | Remove non-metadata elements from the head |
| Wrong value shown | Duplicate tags from plugin and template | Keep one source for each tag |
| Garbled accents | Missing or late `charset` | Put `charset` first |
| Image not found | Relative URL in `content` | Use an absolute URL |
Exercise
A page's source has this head. A teammate says the og:description tag is never picked up by one crawler, even though the browser displays the page normally. Explain why, and say how you would fix it.
<head>
<meta charset="utf-8">
<title>Pricing</title>
<meta property="og:title" content="Pricing">
<img src="/tracker.gif" alt="">
<div class="promo">Sale ends today</div>
<meta property="og:description" content="Plans for every team.">
</head>
Show answer
The div is not allowed in the head, so the parser closes the head at that point and starts the body. The og:description tag after it is parsed as part of the body, and a crawler that reads only the head does not see it. (The img also does not belong in the head and may itself trigger the early close.) Fix it by moving the og:description tag above the img and div, and moving the tracking image and promo banner into the body. Then verify with curl -s https://example.com/pricing | grep -io '<meta[^>]*>'.
Exercise
You write <meta name="og:image" content="/images/cover.jpg">. Name the two problems and give the corrected tag, assuming the site is https://example.com.
Show answer
The first problem is that Open Graph tags use the property attribute, not name. The second is that the URL is relative, and a crawler needs an absolute URL. The corrected tag is <meta property="og:image" content="https://example.com/images/cover.jpg">.