Sub topic 2
The meta element: name, content, and http-equiv
This chapter assumes you know basic HTML syntax (elements, attributes, nesting) and the document structure of doctype, html, head, and body. It covers how the meta element is built. Later chapters cover individual tags such as the charset declaration, the viewport, and the description.
What meta is for
The meta element holds metadata that no other head element covers. It is a void element, so it has no closing tag and no content between tags. All of its information sits in attributes. Browsers, search engines, and link-sharing tools read those attributes before they look at the visible page.
A meta element works in one of a few forms. Each form uses a different attribute to say what kind of metadata it carries.
| Form | Attribute pair | Purpose |
|---|---|---|
| Named metadata | `name` + `content` | Information about the page, such as a description or robots rule |
| Pragma directive | `http-equiv` + `content` | Acts like an HTTP response header |
| Encoding | `charset` alone | Declares the character encoding |
| Property metadata | `property` + `content` | Used by Open Graph and similar vocabularies |
name and content
With name, you give the metadata a label, and with content you give its value. The pair works like a key and a value.
<meta name="description" content="Compare lightweight trail running shoes.">
<meta name="robots" content="noindex">Some rules are worth knowing:
- Every
metaelement that usesnameorhttp-equivmust also have acontentattribute. - Names are matched without regard to case, but lowercase is the convention.
- A tool that does not recognize a name ignores it. A misspelled name causes no error, so the tag silently does nothing.
- The
contentvalue is plain text. Quote it, and escape a literal"inside it as".
The HTML specification and a public registry list the standard names. Some names, such as description, robots, viewport, and color-scheme, have their own chapters.
http-equiv
The name means "HTTP equivalent". An http-equiv meta element asks the browser to behave as if the server had sent a particular HTTP response header. The HTML specification allows only a short list of values.
<meta http-equiv="content-security-policy" content="default-src 'self'">
<meta http-equiv="refresh" content="30">The first line applies a Content Security Policy. The second reloads the page every 30 seconds.
Keep these limits in mind:
- A real HTTP header is more reliable. Servers can send headers for every response, including images and scripts. A meta element affects only the HTML document that contains it.
- A policy in a meta element applies only to content that comes after it. Put it early in the head.
- Some policy directives, such as
frame-ancestors, do not work in a meta element at all. They need a real header. http-equiv="refresh"with a URL redirects the page. Avoid this. It breaks the Back button and disorients screen reader users. Use a server redirect instead.
For encoding, http-equiv="content-type" is allowed but only in the form text/html; charset=utf-8. The short <meta charset="utf-8"> does the same job and is the form to use.
Where meta can appear
Place meta elements in the head. The one common exception is a meta with an itemprop attribute, which belongs to microdata and may appear in the body. Ordinary name or http-equiv tags in the body are invalid and may be ignored.
A common mistake
<meta name="description" value="Compare trail shoes.">This tag is invalid, because it uses value instead of content. Validators report the missing content, and no search engine will read a description from it. The fix is to rename value to content.
Exercise
A page includes <meta name="robots"> with no other attribute. Is it valid, and what would you add to fix it? Separately, why is a meta Content Security Policy weaker than one sent as an HTTP header?
Show answer
It is invalid, because a meta element that uses name must have a content attribute, for example content="noindex". A meta policy affects only the document that contains it and only content after the tag, and some directives such as frame-ancestors are not supported there. A header covers every response and supports all directives.