<head>

The invisible part of the document holding metadata: the title, character encoding, description, styles, and scripts.

Display
none
Closing tag
Required
Permitted content
Metadata elements: <title> (required), <meta>, <link>, <style>, <script>, <base>, <noscript>, and <template>
On this page

Example

#
HTML
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Python Course from Scratch | CodeWorlds</title>
  <meta name="description" content="Learn Python in 30 short lessons with exercises and hints.">
  <link rel="icon" href="/favicon.ico" sizes="32x32">
  <link rel="stylesheet" href="/css/style.css">
  <script src="/js/app.js" defer></script>
</head>

Definition and usage

#

The <head> element is the first child of <html>. You put information about the page in it, not its content: the title (<title>), metadata (<meta>), related files (<link>), styles (<style>), scripts (<script>), and the base address (<base>). None of it appears directly on the page.

Many programs use the contents of the head at once. The browser shows the title on the tab and knows which styles to load. A search engine takes the title and description from it for its results, and social networks build a link preview from it when someone shares the page.

Order matters. <meta charset> must fit within the first 1024 bytes of the file, so put it at the very top. <base> should come before all elements that contain addresses, and you should attach stylesheets in the head so the page looks right from the first moment.

If you put something visible in <head>, such as a <p> or an <img>, the parser decides the document head has just ended. That element and everything after it ends up in <body>.

Syntax

#
Syntax
<head>
  <meta charset="utf-8">
  <title>Page title</title>
</head>

Attributes

#

This element has no attributes of its own. It supports the global attributes, such as class, id and hidden.

Default CSS

#

Most browsers display this element with these default styles:

CSS
head {
  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

#
  • Start with <meta charset="utf-8">, add the viewport tag right after it, and only then the title, styles, and scripts.
  • <head> and <header> are two different elements. The first holds metadata, the second is the visible header of a page or section.
  • Mark scripts in the head with the defer attribute so they don’t hold up rendering the page.

Related entries

#

Practise it in lessons

#

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