Sub topic 2
The meta element: name, content, and http-equiv
This chapter assumes you can read basic HTML syntax (elements, attributes, nesting) and know the parts of a document: doctype, html, head, and body. It also assumes you know a browser fetches a page and a search engine crawls it. You will learn how the meta element is built, which attribute combinations are valid, and what each combination does.
What the meta element is
The meta element carries metadata that no other head element covers. It is a void element, so it has no closing tag and no children. All of its information sits in attributes. A single meta element does one job, and the attributes you choose decide which job.
<meta charset="utf-8">
<meta name="description" content="Compare lightweight trail running shoes.">
<meta http-equiv="refresh" content="300">
<meta property="og:title" content="Trail Running Shoes">These four lines use four different forms. Each form is a different way of saying "here is a piece of information, and here is who should read it."
The attribute forms
A meta element uses exactly one of these "key" attributes. The key says what kind of metadata it is.
| Key attribute | Paired with | Purpose |
|---|---|---|
| `charset` | nothing | Declares the document's character encoding |
| `name` | `content` | Document-level metadata for browsers, crawlers, and tools |
| `http-equiv` | `content` | Stands in for an HTTP response header or a browser directive |
| `property` | `content` | Used by Open Graph and similar vocabularies (not part of the HTML standard, but widely read) |
| `itemprop` | `content` | Microdata, which is allowed outside the head |
Two rules follow from this table:
name,http-equiv,property, anditempropeach require acontentattribute. Ametaelement withnamebut nocontentcarries no information.- Do not combine key attributes on one element.
<meta charset="utf-8" name="x">is invalid. Use one element per piece of metadata.
name and content
The name attribute is the label, and content is the value. Together they form a key-value pair about the whole document.
<meta name="description" content="Compare lightweight trail running shoes by grip, cushioning, and weight.">
<meta name="robots" content="noindex">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="color-scheme" content="light dark">
<meta name="theme-color" content="#0b5fff">Some points about how these pairs behave:
- The
namevalue is not checked against a closed list. The HTML standard defines a few names itself, such asapplication-name,author,description,generator,keywords,referrer,theme-color,color-scheme, andviewport. Others, such asrobots, are defined by the software that reads them. Extra names are registered on a public WHATWG wiki. - A consumer reads only the names it knows. A browser ignores
robots, and a crawler ignoresviewport. An unknown name is harmless but does nothing. - Names are matched case-insensitively.
Descriptionanddescriptionare the same name. Lowercase is the convention. - The
contentvalue is a plain string. Its syntax depends on the name.viewportuses comma-separatedkey=valuepairs,robotsuses comma-separated keywords, andtheme-coloruses a CSS color. - Do not repeat a name to add more values. For example,
robotstakes its values in a single comma-separated list:content="noindex, nofollow". How a reader handles duplicate names is not consistent across tools.
Later chapters cover the individual names in depth. For now, learn the shape: one name, one string value.
The keywords meta tag
You may see <meta name="keywords" content="shoes, trail, running"> in older pages. The standard still lists it, but major search engines have said for years that they do not use it for ranking. Adding it costs nothing, but it gives you no benefit and shows competitors your keyword list.
http-equiv
The name http-equiv means "HTTP equivalent." Historically, authors could not always control the server's response headers, so the standard let a document carry a few of those headers inside the HTML. Today, only a small set of values is useful or valid.
| Value | What it does | Status |
|---|---|---|
| `content-type` | Declares encoding as `text/html; charset=utf-8` | Valid, but `<meta charset>` is shorter and preferred |
| `refresh` | Reloads the page or redirects after a delay | Valid, but discouraged |
| `content-security-policy` | Applies a Content Security Policy to the document | Valid, with limits |
| `default-style` | Selects a preferred alternate stylesheet set | Valid, rarely used |
| `x-ua-compatible` | Selected an old Internet Explorer rendering mode | Obsolete |
content-type
<meta http-equiv="content-type" content="text/html; charset=utf-8">This is equivalent to <meta charset="utf-8">. The content value must be exactly text/html; charset=..., or the element is invalid. Use the short form unless a tool forces the long one. Either way, the declaration must appear in the first 1024 bytes of the document. A byte order mark or the HTTP Content-Type header takes priority over it.
refresh
<meta http-equiv="refresh" content="5; url=https://example.com/new-page">The number is the delay in seconds. With a url, the browser navigates there after the delay. Without one, as in content="300", it reloads the same page.
Avoid it for two reasons. A timed redirect or reload takes control away from users, and screen reader users may lose their place. WCAG, the web accessibility guideline set, lists this as a failure. A server-side redirect (HTTP 301 or 302) is also faster and clearer to search engines. A refresh with a delay of 0 is treated like a redirect by some crawlers, but a real server redirect is better.
content-security-policy
<meta http-equiv="content-security-policy" content="default-src 'self'; img-src 'self' https://images.example.com">A Content Security Policy (CSP) tells the browser which sources of scripts, images, and other resources the page may load. It is normally sent as an HTTP header. A meta version is useful when you host static files and cannot set headers, but it has limits:
- It only applies to content that comes after it in the document. Put it first in the head, before any
scriptorlink, or earlier resources escape the policy. - It does not support the
frame-ancestors,report-uri, orsandboxdirectives. Browsers ignore those directives when they appear in ametatag. - It cannot be added later by a script and still cover what came earlier.
Where meta can appear
meta belongs in the head. The one exception is itemprop metadata for microdata, which may appear in the body. A meta element with name or http-equiv placed in the body is invalid, and browsers may handle it unpredictably. If you see one there, a template is probably including head markup in the wrong place.
A complete example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="content-security-policy" content="default-src 'self'; img-src 'self' https://images.example.com">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Trail Running Shoes - Example Store</title>
<meta name="description" content="Compare lightweight trail running shoes by grip, cushioning, and weight.">
<meta name="robots" content="index, follow">
<meta property="og:title" content="Trail Running Shoes">
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Trail Running Shoes</h1>
</body>
</html>The encoding comes first so the parser knows how to read every byte after it. The policy comes before the stylesheet link so it covers that request. The remaining meta elements have no strict order between them.
Common errors
- Missing
content.<meta name="description">is valid syntax but carries no value. - Mixing keys. Putting
nameandhttp-equivon one element makes it invalid. Use two elements. - Using
namefor Open Graph. The Open Graph tags useproperty. Many scrapers also acceptname, but the documented form isproperty. - Placing
charsetlate. If the declaration falls beyond the first 1024 bytes, the browser may guess the encoding and display garbled text. - Relying on
metaas security. A robotsmetais a request, and a CSP in ametatag has the limits listed above. - Smart quotes in attribute values. Curly quotes copied from a word processor are not delimiters. Use straight quotes.
Inspecting meta elements
You can list every meta element of a loaded page in the browser console. This shows what the parser actually produced, which helps when a template or plugin inserts tags you did not write.
document.querySelectorAll("meta").forEach((m) => {
const key = m.getAttribute("name") || m.getAttribute("http-equiv") ||
m.getAttribute("property") || (m.hasAttribute("charset") ? "charset" : "(none)");
console.log(key, "=>", m.getAttribute("content") ?? m.getAttribute("charset"));
});Each line prints the key and its value. A line that shows undefined or null for the value points to a missing content.
Exercise
A teammate writes <meta name="refresh" content="5; url=/new"> to redirect visitors, and <meta http-equiv="description" content="Our shoe catalog"> to describe the page. Neither works. What is wrong with each, and what should replace them?
Show answer
The first uses name, but refresh is an http-equiv value, so it should be <meta http-equiv="refresh" content="5; url=/new">. Better still, use a server-side 301 redirect, because timed refreshes harm accessibility. The second uses http-equiv, but description is a name value, so it should be <meta name="description" content="Our shoe catalog">.
Exercise
You host a static site and cannot set HTTP headers. You add a meta tag with http-equiv="content-security-policy" after your analytics script and include frame-ancestors 'none' in it. Name two ways this fails to protect the page.
Show answer
The policy only applies to content after it, so the analytics script placed before it is not covered. Also, browsers ignore the frame-ancestors directive in a meta tag, so the page can still be framed. Put the meta first in the head, and set frame-ancestors through a real HTTP header.