Skip to content
Tutorials Center
My learning

Topics

Lesson 50 of 122 ยท The Head, Metadata and SEO

The head Element

Learn what belongs in the HTML head element (title, meta, link, style, script, base and noscript) and the order that helps pages load fast and read well.

Every HTML document has two halves. The <body> holds what people see. The <head> holds information about the page: its title, its character encoding, which stylesheets and scripts it needs, how it should look when shared, and much more.

Almost nothing in the head is drawn on the screen, which is exactly why it is easy to neglect. Yet browsers, search engines, screen readers and social networks all read it first. In this lesson you will meet every element that may live in the head, learn what each one is for, and see a recommended order that keeps pages fast and predictable.

Where the head fits

The head sits between the opening <html> tag and the <body>:

HTML
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Trail Running for Beginners</title>
  </head>
  <body>
    <h1>Trail Running for Beginners</h1>
  </body>
</html>

The head contains only metadata content: elements that describe or configure the document rather than display it. The full list is short:

Element What it does
<title> The document's name, shown in the browser tab, history and bookmarks.
<meta> Named pieces of metadata: encoding, viewport, description, robots rules and more.
<link> Relationships to other resources: stylesheets, icons, the canonical URL, preloads.
<style> CSS written directly in the page.
<script> JavaScript, inline or from a file, plus data blocks such as JSON-LD.
<base> The base URL (and default link target) for every relative URL on the page.
<noscript> Fallback content used only when scripting is turned off.
<template> Inert HTML kept for later use by scripts.

If you have used DevTools, you have already seen the head in the Elements panel. Head content is invisible on the page, so let's make it visible with a small script.

Inspect the head

Add another <meta> or <link> to the head and run the example again. Your new element appears in the list.

<title>: the page's name

The <title> element is the only one that is required in almost every document (the rare exceptions are documents such as iframe content whose title comes from elsewhere). It contains plain text only; any tags you type inside it are shown literally.

HTML
<title>Trail Running for Beginners โ€“ Peak Club</title>

The title appears in the browser tab, in bookmarks and history, and is announced by screen readers when the page opens. Search engines usually use it as the clickable headline of a result. Writing good titles is a skill of its own, covered in Page Titles and Descriptions.

<meta>: facts about the page

A <meta> element is a void element (no closing tag) that records one fact. Two are so important that every page should start with them:

HTML
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
  • charset declares the character encoding. It must appear within the first 1,024 bytes of the file, so put it first. Character Encoding and UTF-8 explains why.
  • viewport tells phones to lay out the page at the device's width instead of pretending to be a desktop screen. See Responsive Web Design.

Other <meta> elements describe the page for search engines, tint the browser's toolbar, control referrer information and more. They get a whole lesson: Meta Tags.

<link> points to another resource and says, with its rel attribute, how that resource relates to the page:

HTML
<link rel="stylesheet" href="/css/site.css">
<link rel="icon" href="/favicon.svg" type="image/svg+xml">
<link rel="canonical" href="https://example.com/guides/trail-running">
<link rel="preconnect" href="https://fonts.example-cdn.com">
  • stylesheet loads CSS (see Adding CSS to HTML).
  • icon sets the favicon (see Favicons and App Icons).
  • canonical names the preferred URL when the same content is reachable at several addresses, for example with and without tracking parameters. Search engines treat it as a strong hint, not an order.
  • preconnect, preload and friends speed up loading. They are the subject of Loading Resources Efficiently.

<style> and <script>

<style> holds CSS that applies to the whole document. It is handy for small pages and for the "critical" CSS a page needs to render its first screen, but most sites keep their styles in external files so the browser can cache them.

<script> runs JavaScript. A classic script in the head pauses the HTML parser while it downloads and runs, so the page stays blank longer. The fix is the defer attribute, which lets the script download in the background and run once the document has been parsed:

HTML
<script src="/js/app.js" defer></script>

Scripts can also carry data instead of code, such as a JSON-LD block for Structured Data. You will learn all the loading options in Adding JavaScript.

<base>: changing how relative URLs resolve

The <base> element sets the starting point for every relative URL in the document. It may also set the default target for links and forms. A page may contain at most one <base>, and it must come before any element that uses a URL.

The base element

Look at the last line: even #faq now points to a different page, because it is resolved against the base URL. That surprise is the main reason most sites avoid <base>. If you do use it, remember that it affects every relative URL: links, images, stylesheets, scripts and form actions. File Paths and URLs covers relative URLs in detail.

<noscript> and <template>

<noscript> provides a fallback for visitors with JavaScript turned off. Inside the head it may only contain <link>, <style> and <meta> elements, for example a stylesheet that reveals content a script would normally show:

HTML
<noscript>
  <link rel="stylesheet" href="/css/no-js.css">
</noscript>

<template> is allowed in the head too, although it is more common in the body. Its content is parsed but not rendered until a script clones it. See Templates and Web Components.

The tags are optional, the head is not

Here is a surprise: the <head> and </head> tags themselves are optional. If you leave them out, the browser creates the head element anyway and puts your <title> and <meta> elements in it.

The flip side is that the head ends as soon as the parser meets something that does not belong there, such as text or a <p>. Everything after that point lands in the body.

A broken head

The description and canonical link end up in the body, where many tools will never look for them. Delete the stray paragraph and run it again: everything moves back into the head. Write the head tags explicitly and keep only metadata inside them; Validating Your HTML shows how a validator catches mistakes like this.

The order of elements in the head matters less than their presence, but a few rules make a real difference:

  1. <meta charset> first, so it is inside the first 1,024 bytes and the browser never has to re-decode the page.
  2. <meta name="viewport"> right after it.
  3. <title> early, so the tab shows a name as soon as possible.
  4. Connection hints (preconnect) before the resources that need them.
  5. Stylesheets, because the page cannot render until they arrive.
  6. Scripts with defer (or type="module"). They do not block, so their position is flexible.
  7. Everything else: description, canonical link, icons, social sharing tags and structured data.

Put together, a typical head looks like this:

HTML
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Trail Running for Beginners โ€“ Peak Club</title>
  <link rel="preconnect" href="https://cdn.example.com">
  <link rel="stylesheet" href="/css/site.css">
  <script src="/js/app.js" defer></script>
  <meta name="description" content="Gear, pacing and safety tips for your first trail run.">
  <link rel="canonical" href="https://example.com/guides/trail-running">
  <link rel="icon" href="/favicon.svg" type="image/svg+xml">
  <meta property="og:title" content="Trail Running for Beginners">
</head>

The og:title line is an Open Graph tag, which controls how links look when shared. You will write a full set in Social Sharing Previews.

Keep the head tidy. Old templates often carry tags that do nothing today, such as <meta name="keywords"> or <meta http-equiv="X-UA-Compatible">. Every tag in your head should have a reason to be there.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which element should come first inside the head?

    The character encoding must be declared within the first 1,024 bytes of the document, so <meta charset> goes first.
  2. 2. You write a <p> element between two <meta> elements in the head. What happens?

    The head can only hold metadata. A <p> closes it implicitly, and everything after it is placed in the body.
  3. 3. A page has <base href="https://example.com/shop/">. Where does <a href="#reviews"> point?

    Every relative URL, including a bare fragment, is resolved against the base URL. This is a common surprise with <base>.
  4. 4. Why add defer to a script in the head?

    A classic script without defer pauses the parser. With defer, downloading happens in the background and execution waits until the document has been parsed.

Summary

  • The <head> holds metadata: <title>, <meta>, <link>, <style>, <script>, <base>, <noscript> and <template>.
  • <title> is required and names the page in tabs, bookmarks, screen readers and search results.
  • Start every head with <meta charset="utf-8"> and the viewport meta tag.
  • <link> connects the page to stylesheets, icons, canonical URLs and loading hints.
  • <base> changes how all relative URLs resolve, including #fragments; use it with care.
  • Anything that is not metadata closes the head early, so keep the head clean and write its tags explicitly.

Next lesson: Page Titles and Descriptions.