<meta>

Page metadata: character encoding, mobile viewport settings, a description for search engines, and a link preview on social networks.

Display
none
Closing tag
None, void element
Permitted content
None, an empty element
On this page

Example

#
HTML
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>JavaScript Course for Beginners | CodeWorlds</title>
  <meta name="description" content="Learn JavaScript from scratch: variables, functions, loops, and the DOM in short, interactive lessons.">
  <meta property="og:title" content="JavaScript Course for Beginners">
  <meta property="og:image" content="https://www.codeworlds.com/og/javascript-course.png">
</head>

Definition and usage

#

The <meta> element passes on information about the document that no other head element can express. It is empty (it has no closing tag) and goes into <head>. What it means depends on the attribute used: charset declares the character encoding, the name and content pair describes named metadata (e.g. the page description), and http-equiv works like a header sent by the server.

Two tags should appear on every page. The first is <meta charset="utf-8">, which makes accented and non-Latin letters display correctly. The second is <meta name="viewport">: without it, a phone will show the page as if its screen were about 980 pixels wide and shrink it to an unreadable size.

The description (name="description") doesn’t directly affect the ranking in Google, but it often lands under the title in search results and encourages clicks. The Open Graph tags (property="og:…"), in turn, decide how a link looks once it’s shared on Facebook, LinkedIn, or in messengers. The property attribute doesn’t come from the HTML standard but from the Open Graph protocol, yet most large platforms understand it.

Syntax

#
Syntax
<meta charset="utf-8">
<meta name="name" content="value">

Attributes

#
  • charset

    utf-8

    Declares the document’s character encoding. In HTML the only allowed value is utf-8. The tag must fit within the first 1024 bytes of the file, so make it the first element in <head>. Without it, the browser may guess the encoding wrong and show odd characters instead of “é” or “ü”.
  • name="viewport"

    width=device-width, initial-scale=1

    Tells the browser on a phone how wide the page should be and at what zoom to show it. width=device-width fits the layout width to the device screen, and initial-scale=1 turns off the initial zoom-out. Without it, CSS @media queries won’t work on phones the way you expect.
  • name="description"

    text

    A short summary of the page. Search engines often show it under the title in results, usually up to about 150-160 characters. Be specific and inviting, and give every subpage a different description.
  • name="robots"

    index | noindex | follow | nofollow | …

    Instructions for search engine robots. noindex asks that the page not appear in results, and nofollow asks the robot not to follow its links. By default robots index the page and visit its links, so you don’t need to write index, follow.
  • name="theme-color"

    CSS color

    A color the browser can use to tint its interface, for example the address bar on a phone. With the media attribute you can give separate colors for light and dark mode. Not every browser supports it, so treat it as a nice extra.
  • content

    text

    The value of the metadata. It always appears paired with name, http-equiv, or property, and its format depends on what you’re describing: for a page description it’s plain text, for viewport a comma-separated list of settings.
  • http-equiv

    content-security-policy | refresh | …

    Works like an HTTP header sent by the server. refresh reloads the page or redirects to another address after the given number of seconds (avoid it, because it surprises users). content-security-policy restricts where the page may load scripts and other resources from. The X-UA-Compatible entry from older templates applied only to Internet Explorer and is unnecessary today.
  • property

    og:title | og:description | og:image | og:url | og:type | …

    Open Graph protocol tags, from which social networks build a preview of a shared link: title, description, and image. Give the address in og:image in full, with https://. An image of 1200 × 630 pixels looks good on most platforms. X (formerly Twitter) uses the same data, and you additionally set the card type with the name="twitter:card" tag.

The element also supports all global attributes, such as class, id, style and hidden.

Attributes in practice

#

charset

utf-8

Declares the document’s character encoding. In HTML the only allowed value is utf-8. The tag must fit within the first 1024 bytes of the file, so make it the first element in <head>. Without it, the browser may guess the encoding wrong and show odd characters instead of “é” or “ü”.

HTML
<head>
  <meta charset="utf-8">
  <title>Crème brûlée, naïve café, Zürich</title>
</head>

name="viewport"

width=device-width, initial-scale=1

Tells the browser on a phone how wide the page should be and at what zoom to show it. width=device-width fits the layout width to the device screen, and initial-scale=1 turns off the initial zoom-out. Without it, CSS @media queries won’t work on phones the way you expect.

HTML
<meta name="viewport" content="width=device-width, initial-scale=1">

name="description"

text

A short summary of the page. Search engines often show it under the title in results, usually up to about 150-160 characters. Be specific and inviting, and give every subpage a different description.

HTML
<meta name="description" content="A free HTML course for beginners: 24 lessons, exercises with a live preview, and a certificate at the end.">

name="robots"

index | noindex | follow | nofollow | …

Instructions for search engine robots. noindex asks that the page not appear in results, and nofollow asks the robot not to follow its links. By default robots index the page and visit its links, so you don’t need to write index, follow.

HTML
<meta name="robots" content="noindex, follow">

A typical use: a cart page or a site’s search results page that shouldn’t end up in Google.

name="theme-color"

CSS color

A color the browser can use to tint its interface, for example the address bar on a phone. With the media attribute you can give separate colors for light and dark mode. Not every browser supports it, so treat it as a nice extra.

HTML
<meta name="theme-color" content="#f8fafc" media="(prefers-color-scheme: light)">
<meta name="theme-color" content="#0f172a" media="(prefers-color-scheme: dark)">

http-equiv

content-security-policy | refresh | …

Works like an HTTP header sent by the server. refresh reloads the page or redirects to another address after the given number of seconds (avoid it, because it surprises users). content-security-policy restricts where the page may load scripts and other resources from. The X-UA-Compatible entry from older templates applied only to Internet Explorer and is unnecessary today.

HTML
<meta http-equiv="content-security-policy" content="default-src 'self'">
<meta http-equiv="refresh" content="5; url=/courses">

The first tag allows loading scripts, styles, and images only from your own domain. The second redirects to the /courses page after 5 seconds.

property

og:title | og:description | og:image | og:url | og:type | …

Open Graph protocol tags, from which social networks build a preview of a shared link: title, description, and image. Give the address in og:image in full, with https://. An image of 1200 × 630 pixels looks good on most platforms. X (formerly Twitter) uses the same data, and you additionally set the card type with the name="twitter:card" tag.

HTML
<meta property="og:type" content="website">
<meta property="og:url" content="https://www.codeworlds.com/javascript-course">
<meta property="og:title" content="JavaScript Course for Beginners">
<meta property="og:description" content="Variables, functions, and loops in short lessons with a live preview.">
<meta property="og:image" content="https://www.codeworlds.com/og/javascript-course.png">
<meta name="twitter:card" content="summary_large_image">

Default CSS

#

Most browsers display this element with these default styles:

CSS
meta {
  display: none;
}

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • Make <meta charset="utf-8"> the first element in <head>, even before <title>.
  • Don’t block zooming with values like maximum-scale=1 or user-scalable=no. People with low vision need to be able to enlarge the text.
  • After adding Open Graph tags, check the link preview in the platforms’ tools, e.g. Facebook’s Sharing Debugger. Sites cache the preview, so after changes you need to refresh it there.

Related entries

#

Practise it in lessons

#

Spotted a mistake or missing an example? Write to us.