References

Beginner-friendly references for web development, with live, editable examples.

The HTML content attribute

Attribute All modern browsers Updated
In short

The HTML content attribute holds the value of a <meta> tag. That value might be a page description or viewport settings. It is required when the tag has name, http-equiv or itemprop, and it must be left out when the tag only has charset.

Overview

The content attribute supplies the value half of a <meta> tag. Another attribute on the tag says what is being set, and content says what it is set to, so a description tag carries the actual summary text in content.

The pairing rules come from the HTML Standard. Every meta tag has exactly one of name, http-equiv, charset or itemprop, and content must be present with name, http-equiv and itemprop and absent with charset.

What goes inside depends on that partner. A description is free-form text suited to a search engine's listing, keywords is a comma-separated list, and theme-color must be a valid CSS color. A viewport takes comma-separated key and value pairs like width=device-width, initial-scale=1, and a refresh takes a number of seconds with an optional URL.

The value is plain text, so tags inside content are not rendered as markup, and a double quote inside a double-quoted value has to be written as a character reference such as &quot;. In JavaScript the content property reflects the attribute, which lets a script read or change the value after the page loads.

Syntax

<meta name="description" content="Step-by-step recipes for weeknight dinners.">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="refresh" content="5; url=https://example.com/new-page">

Values

Value
A string whose meaning depends on the tag's name, http-equiv or itemprop attribute.

Example

Live example
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta name="description" content="Step-by-step recipes for weeknight dinners.">
  <meta name="theme-color" content="#1e72e0">
  <title>Weeknight recipes</title>
</head>
<body>
  <ul id="out"></ul>
  <script>
    for (const meta of document.querySelectorAll('meta[name]')) {
      const item = document.createElement('li');
      item.textContent = meta.name + ' = ' + meta.content;
      document.getElementById('out').append(item);
    }
  </script>
</body>
</html>

More Examples

Best practices

  • Write one description per page, since the HTML Standard allows only a single meta description in a document and expects it to describe that page.
  • Remove meta tags you have no value for instead of shipping empty content attributes.
  • Leave content off a meta charset tag, because the HTML Standard requires it to be omitted there.
  • Keep viewport values to settings like width=device-width, initial-scale=1, and never add user-scalable=no, which MDN warns stops people with low vision from zooming.
  • Use single quotes around a content value that needs double quotes inside it, or escape them as &quot;.

Accessibility

The viewport tag's content can block zooming. MDN warns that setting user-scalable to no prevents people with low vision from reading and understanding the page, so keep zoom enabled and let the layout respond to the device width instead.

Frequently asked questions

What is the content attribute in a meta tag?
The content attribute holds the value of a meta tag's name, http-equiv or itemprop setting. A description tag, for example, stores its summary text in content.
Is the content attribute required in meta tags?
The content attribute is required whenever a meta tag has name, http-equiv or itemprop. It must be omitted on a meta tag that only declares charset.
Can I put HTML inside the content attribute?
No, the content attribute holds plain text, so tags inside it are not rendered as markup. Use single quotes around a value that contains double quotes, or escape them with a character reference.
How do I change a meta tag's content with JavaScript?
Select the tag and assign its content property, as in document.querySelector('meta[name=theme-color]').content = '#000000'. The property reflects the content attribute, so the markup changes too.
Is the HTML content attribute related to the CSS content property?
No, the HTML content attribute sets the value of a meta tag. The CSS content property replaces content with a generated value, such as text in ::before and ::after pseudo-elements.
What should the content of a meta description say?
The content of a meta description should summarize that page in plain text suitable for a search listing. The HTML Standard allows only one description meta tag per document.