<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>
Example
#<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
#<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:
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
deferattribute so they donβt hold up rendering the page.
Related entries
#- <header> vs <head><head> is an invisible section holding the pageβs metadata, one per document. <header> is a visible introductory element, and a page can have several.
- <html>The root of an HTML document. Every other element on the page lives inside it.
- <title>The document title shown on the browser tab, in bookmarks, and as the headline of a search result.
- <meta>Page metadata: character encoding, mobile viewport settings, a description for search engines, and a link preview on social networks.
- <link>Connects the document to an external resource: a stylesheet, a page icon, a font, or another language version.
- <script>Embeds JavaScript code in the page or loads it from a file to give the page behavior.
- <header>An introduction to a page or section: logo, title, navigation, author info.
Practise it in lessons
#Spotted a mistake or missing an example? Write to us.