TExcelTAD
Home › HTML › HTML Head

HTML Head

Every HTML document has two main parts: the <head> and the <body>. The body holds what visitors see. The head holds information about the page — its title, character set, styles, scripts, and hints for browsers, search engines and social networks. Almost nothing in the head is displayed directly, but it affects how the whole page behaves.

html
<!DOCTYPE html>
<html lang="en">
<head>
  <!-- metadata goes here -->
</head>
<body>
  <!-- visible content goes here -->
</body>
</html>

Elements that belong in the head

Element Purpose
<title> Text in the browser tab, bookmarks and search results (required)
<meta> Metadata: character set, viewport, description, author…
<link> Connects external resources: stylesheets, icons, fonts
<style> CSS written directly in the page
<script> JavaScript (often with defer)
<base> Sets a base URL for all relative links on the page

A complete, modern head

This is a good starting template for almost any page:

html
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Fresh Bakes – Neighbourhood Bakery</title>
  <meta name="description" content="Sourdough, pastries and cakes baked every morning.">
  <link rel="icon" href="favicon.svg">
  <link rel="stylesheet" href="css/style.css">
  <script src="js/app.js" defer></script>
</head>

The title element

<title> should be short, specific and unique for each page. Search engines show it as the clickable headline of a result, so "Contact – Fresh Bakes" beats "Page 2".

Meta tags

<meta> is an empty element: it has no closing tag and all its information is in attributes.

Meta tag What it does
<meta charset="UTF-8"> Declares the character encoding; put it first in the head
<meta name="viewport" content="width=device-width, initial-scale=1.0"> Makes the page fit phone screens instead of showing a zoomed-out desktop view
<meta name="description" content="…"> Summary often shown under the title in search results
<meta name="author" content="…"> The page author
<meta name="keywords" content="…"> Largely ignored by modern search engines
<meta name="theme-color" content="#e34c26"> Colors the browser UI on some mobile browsers
<meta http-equiv="refresh" content="30"> Reloads the page every 30 seconds (use sparingly)

Tip: Without the viewport meta tag, mobile browsers pretend the screen is about 980px wide and shrink the page. Always include it — it is the foundation of responsive design.

The style element

Internal CSS in a <style> block applies to the whole page:

html · try it yourself
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Styled from the head</title>
  <style>
    body { font-family: Georgia, serif; background: #fdf6ec; color: #3b2f2f; }
    h1 { color: #e34c26; border-bottom: 2px solid currentColor; }
    p { line-height: 1.6; }
  </style>
</head>
<body>
  <h1>Fresh Bakes</h1>
  <p>All of these styles were defined in the head of the document, not on the elements themselves.</p>
</body>
</html>

Reading head information with JavaScript

The head is not visible, but scripts can read it. This example prints the page's title and every meta tag:

html · try it yourself
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta name="description" content="A demo page about the head element.">
  <meta name="author" content="ExcelTAD student">
  <title>Head Inspector</title>
</head>
<body style="font-family: sans-serif">
  <h3>What is in this page's head?</h3>
  <ul id="info"></ul>
  <script>
    const info = document.getElementById("info");
    function add(text) {
      const li = document.createElement("li");
      li.textContent = text;
      info.appendChild(li);
    }
    add("title: " + document.title);
    for (const m of document.querySelectorAll("meta")) {
      if (m.getAttribute("charset")) add("charset: " + m.getAttribute("charset"));
      else add(m.name + ": " + m.content);
    }
  </script>
</body>
</html>

The base element

<base href="…"> changes the starting point for every relative URL on the page. It can also set a default link target. There may be only one, and it must come before any element that uses a URL.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
  <base href="https://docs.example.com/guide/" target="_blank">
</head>
<body style="font-family: sans-serif">
  <a id="link" href="install.html">Installation</a>
  <p>This relative link really points to: <code id="out"></code></p>
  <script>
    document.getElementById("out").textContent = document.getElementById("link").href;
  </script>
</body>
</html>

Note: <base> is easy to forget about and can break every link and image on a page. Use it only when you have a clear reason.

Social sharing tags

When someone shares your page on social media, the preview card is built from Open Graph meta tags:

html
<meta property="og:title" content="Fresh Bakes">
<meta property="og:description" content="Sourdough baked every morning.">
<meta property="og:image" content="https://freshbakes.example/og-card.png">

What if I leave the head out?

Browsers are forgiving: if you omit <head>, they create one automatically and move elements like <title> into it. But writing it explicitly makes your document clearer and avoids surprises.

Test yourself

Which element is required inside the head of every valid HTML page?

  1. <meta>
  2. <title>
  3. <style>
  4. <link>

What does <meta name="viewport" content="width=device-width, initial-scale=1.0"> do?

  1. Sets the page language
  2. Makes the layout use the device's real screen width
  3. Hides the page from search engines
  4. Sets the character encoding

Which element connects an external stylesheet?

  1. <style src="a.css">
  2. <link rel="stylesheet" href="a.css">
  3. <css href="a.css">
  4. <meta css="a.css">