Everything you've written so far, headings, images, tables, forms, iframes, lives inside <body> and appears on the screen. But every real web page also has a part that is never drawn: the <head>.
When you share a link on WhatsApp and a nice preview card with an image appears, when a phone colours its browser bar blue for one site and green for another, when Google shows a two-line description under a search result, all of that comes from the <head>. In the HTML Document Structure chapter we met its three essentials. In this chapter we'll go through everything else that belongs there, and why.
The
<head>element holds metadata: information about the document (its title, encoding, description, icons, linked resources) that is used by browsers, search engines and other programs, but isn't displayed as page content.
What Is Allowed Inside <head>?
Only a small set of elements may appear in <head>:
| Element | Purpose |
|---|---|
<title> | The page title (exactly one per page) |
<meta> | Name/value metadata, encoding, and some HTTP-header equivalents |
<link> | Relationships to other resources: CSS files, icons, canonical URL, manifest |
<style> | CSS written inside the page |
<script> | JavaScript |
<base> | Base URL for all relative links (at most one) |
<noscript> | Content for browsers with JavaScript turned off |
<template> | Inert HTML for scripts to clone later |
If you put a visible element like <p> or <img> inside <head>, the browser doesn't complain. It silently closes the head, starts the body early, and puts the element there. Every head element after it ends up in the wrong place. A validator will catch this.
A Complete, Modern <head>
Here is a well-organised head for a real tutorial page. Don't worry, we'll go through it group by group.
<!DOCTYPE html>
<html lang="en">
<head>
<!-- 1. Must-haves, in this order -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Binary Search Trees – DSA Notes | CS Learning Portal</title>
<!-- 2. Description and search engine hints -->
<meta name="description" content="Learn binary search trees with diagrams, insertion, deletion and GATE questions.">
<meta name="robots" content="index, follow">
<link rel="canonical" href="https://example.com/dsa/binary-search-trees">
<!-- 3. Look and feel -->
<meta name="color-scheme" content="light dark">
<meta name="theme-color" content="#1e3a8a">
<link rel="icon" href="/favicon.ico" sizes="32x32">
<link rel="icon" href="/icon.svg" type="image/svg+xml">
<link rel="apple-touch-icon" href="/apple-touch-icon.png">
<link rel="manifest" href="/manifest.webmanifest">
<!-- 4. Social media previews (Open Graph) -->
<meta property="og:title" content="Binary Search Trees – DSA Notes">
<meta property="og:description" content="BSTs explained with diagrams and GATE questions.">
<meta property="og:image" content="https://example.com/images/bst-preview.png">
<meta property="og:url" content="https://example.com/dsa/binary-search-trees">
<meta property="og:type" content="article">
<meta name="twitter:card" content="summary_large_image">
<!-- 5. Performance hints, CSS and JavaScript -->
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link rel="stylesheet" href="styles.css">
<script src="app.js" defer></script>
</head>
<body>
<h1>Binary Search Trees</h1>
<p>Only the body is drawn on the screen.</p>
</body>
</html>In the browser you see only the heading and the paragraph. The title appears on the tab. Everything else works behind the scenes. This page passes the html-validate checker with no errors.
Group 1: The Must-Haves
A quick recap from the HTML Document Structure chapter:
<meta charset="utf-8">tells the browser how to turn bytes into characters. It must be within the first 1024 bytes of the file, so put it first.<meta name="viewport" content="width=device-width, initial-scale=1">makes the page use the phone's real width instead of pretending to be a ~980px desktop screen.<title>names the page on tabs, bookmarks and search results.
Never add
user-scalable=noormaximum-scale=1to the viewport. They stop users from pinch-zooming, which makes your page unusable for people with low vision. Safari on iPhone now ignores them for this reason, but other browsers may not.
How <meta> Works
<meta> is a void element (no closing tag) that comes in a few forms:
| Form | Example | Used for |
|---|---|---|
charset | <meta charset="utf-8"> | Character encoding |
name + content | <meta name="description" content="..."> | Document-level metadata |
http-equiv + content | <meta http-equiv="refresh" content="5"> | Acting like an HTTP response header |
property + content | <meta property="og:title" content="..."> | Open Graph (from the RDFa standard) |
Common name Values
| name | What it does |
|---|---|
description | One or two sentences summarising the page. Search engines often (not always) show it as the snippet under your title |
robots | Instructions for search engine crawlers, e.g. noindex (don't show in results) and nofollow (don't follow links) |
viewport | Mobile layout (above) |
color-scheme | Which colour schemes the page supports: light, dark or light dark |
theme-color | Suggested colour for the browser's UI around the page (address bar on some mobile browsers) |
author | Name of the author |
referrer | How much of this page's URL is sent in the Referer header when following links |
generator | Software that generated the page (e.g. a CMS) |
keywords | A comma-separated keyword list. Still valid HTML, but Google has ignored it for ranking for many years because it was abused for spam |
Group 2: Description and Search Engine Hints
A good description is specific and about 150 characters or fewer, so it isn't cut off in results:
<!-- ❌ Vague -->
<meta name="description" content="Welcome to my website. Best notes.">
<!-- ✅ Specific -->
<meta name="description" content="Learn binary search trees with diagrams, insertion, deletion and GATE questions.">robots is mostly used to keep pages out of search results, like a login page or a "thank you for registering" page:
<meta name="robots" content="noindex, nofollow">If you don't include a robots tag at all, crawlers assume index, follow, so writing it explicitly (as in our example) is optional.
<link rel="canonical"> tells search engines which URL is the "official" one when the same content is reachable at several URLs (for example with and without ?ref=whatsapp). We'll go deeper into all of these in the HTML for SEO chapter.
Group 3: Look and Feel
color-scheme
<meta name="color-scheme" content="light dark"> tells the browser "this page is designed to work in both light and dark mode". When the user's system is in dark mode, the browser then uses dark defaults for the page background, text, form controls and scrollbars.
We tested our example page with Chrome in dark mode. With the tag, the page background is near-black and the text is white. Without it, the same page stays white with black text even though the system is dark. (In real projects, you would also write dark-mode CSS; this tag just gets the browser's built-in parts right.)
theme-color
theme-color suggests a colour for the browser interface around your page, like the address bar in Chrome on Android. Desktop browsers mostly ignore it. Safari used it for its tab bar in older versions, but from Safari 26 (2025) it mostly ignores it in normal browsing and instead tints its toolbar by sampling the colours at the top of the page; the tag still matters for sites installed to the Home Screen as web apps. You can give different colours for light and dark mode using a media attribute:
<meta name="theme-color" content="#ffffff" media="(prefers-color-scheme: light)">
<meta name="theme-color" content="#111827" media="(prefers-color-scheme: dark)">Favicons and App Icons
| Tag | Used by |
|---|---|
<link rel="icon" href="/favicon.ico" sizes="32x32"> | Browser tabs, bookmarks (works everywhere) |
<link rel="icon" href="/icon.svg" type="image/svg+xml"> | Modern browsers; one sharp, scalable file |
<link rel="apple-touch-icon" href="/apple-touch-icon.png"> | iPhone/iPad "Add to Home Screen" (a 180 × 180 PNG is typical) |
<link rel="manifest" href="/manifest.webmanifest"> | A JSON file with the app's name, icons and colours, used when installing a site as an app (PWA) |
Even with no icon tags, browsers try to load /favicon.ico from the site root, which is why you see a 404 for it in many server logs.
Group 4: Social Media Previews (Open Graph)
When you paste a link into WhatsApp, LinkedIn, Facebook, Slack or Discord, their servers download your page and look for Open Graph tags to build the preview card:
┌──────────────────────────────────────────┐
│ [ og:image: big preview picture ] │
├──────────────────────────────────────────┤
│ Binary Search Trees – DSA Notes │ ← og:title
│ BSTs explained with diagrams and GATE... │ ← og:description
│ example.com │ ← from og:url
└──────────────────────────────────────────┘| Property | Meaning |
|---|---|
og:title | Title on the card (can differ from <title>) |
og:description | One-line summary |
og:image | Preview image. Use an absolute URL; about 1200 × 630 pixels is a widely used size |
og:url | The canonical URL of the page |
og:type | website, article, etc. |
Notice Open Graph uses property=, not name=. X (formerly Twitter) reads Open Graph too, and adds its own name="twitter:card" tag to choose the card style; summary_large_image gives a big image.
These platforms cache previews. If you change
og:imageand the old picture still appears, the platform is showing its cached copy; most offer a "debugger" or "post inspector" tool to refresh it.
Group 5: CSS, JavaScript and Performance Hints
Stylesheets
<link rel="stylesheet" href="styles.css"> <!-- external CSS file -->
<style>
body { font-family: sans-serif; } /* CSS inside the page */
</style>CSS belongs in the head so the browser knows the styles before it draws the body; otherwise the user sees unstyled content that suddenly jumps into place.
Scripts: defer and async
A plain <script src="app.js"> in the head stops HTML parsing until the script is downloaded and run, which delays the whole page. Two attributes fix that:
| Attribute | Download | Runs when | Order kept? | Use for |
|---|---|---|---|---|
| (none) | Blocks parsing | Immediately | Yes | Rarely needed today |
defer | In parallel | After the HTML is fully parsed | Yes | Your main page scripts |
async | In parallel | As soon as it's downloaded | No | Independent scripts like analytics |
<script type="module"> is deferred automatically. You'll use these more in the JavaScript tutorial.
Resource Hints
| Tag | Tells the browser |
|---|---|
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> | "Open a connection to this server now; we'll need it soon" |
<link rel="dns-prefetch" href="https://cdn.example.com"> | "Look up this domain's IP address early" (cheaper than preconnect) |
<link rel="preload" href="hero.webp" as="image"> | "Download this file early with high priority; this page definitely uses it" |
Use hints sparingly. Preloading ten files makes them compete with each other, so nothing gets faster.
The <base> Element
<base href="https://example.com/notes/">After this, every relative URL on the page (href="trees.html", src="img/bst.png") is resolved from https://example.com/notes/. It must come before any element that uses a URL, and a page can have only one. It's rarely needed and easy to forget it's there; links like href="#section" also get resolved against the base, which surprises many developers. Use it only if you have a clear reason.
http-equiv: Pretending to Be an HTTP Header
Some settings are normally sent by the server as HTTP headers. http-equiv lets you put a few of them in HTML:
<!-- Reload this page every 60 seconds -->
<meta http-equiv="refresh" content="60">
<!-- Redirect to another page after 3 seconds -->
<meta http-equiv="refresh" content="3; url=https://example.com/new-page">
<!-- Content Security Policy: only load scripts from this site -->
<meta http-equiv="Content-Security-Policy" content="script-src 'self'">Timed refreshes and redirects are bad for accessibility (the page changes before a slow reader or a screen-reader user finishes reading); accessibility linters such as html-validate report any refresh with a delay as an error. For redirects, a real HTTP 301/302 redirect from the server is better. Also note that some CSP features, such as frame-ancestors from the Iframes chapter, only work as a real HTTP header, not in a meta tag.
Outdated Head Tags You'll Still See
| Tag | Status |
|---|---|
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | Only affected Internet Explorer, which is retired. Safe to delete |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> | Old long form of <meta charset="utf-8">. Use the short form |
<meta name="keywords"> | Valid, but ignored by Google for ranking |
<link rel="shortcut icon"> | Old IE-era spelling; rel="icon" is enough |
type="text/css" on <link>/<style>, type="text/javascript" on <script> | Unnecessary; they are the defaults |
Common Mistakes
<meta charset>not first. If a long comment or other tags push it past the first 1024 bytes, the browser may decode the page wrongly.The same title and description on every page. Each page should describe itself.
Using
name="og:title". Open Graph usesproperty.Relative
og:imageURLs. Use a fullhttps://URL.Disabling zoom with
user-scalable=no.Blocking scripts in the head without
deferorasync.Visible elements in the head. They end the head early.
Interview Questions
Q1. What is the difference between defer and async on a script?
Both download the script without blocking HTML parsing. defer scripts run after the document is fully parsed, in the order they appear. async scripts run as soon as each one finishes downloading, in any order, possibly before parsing is complete.
Q2. Why must <meta charset> be near the top of the head?
The browser needs the encoding before it can correctly decode the rest of the text. The spec requires the declaration to be within the first 1024 bytes of the document.
Q3. Which tags control how a link looks when shared on WhatsApp or LinkedIn?
Open Graph meta tags: og:title, og:description, og:image, og:url and og:type, written as <meta property="..." content="...">.
Q4. How do you stop a page from appearing in search results?
Add <meta name="robots" content="noindex"> to its head (or send the equivalent X-Robots-Tag HTTP header). The crawler must be able to fetch the page to see this tag.
Q5. Does <meta name="keywords"> help a page rank on Google?
No. Google has ignored the keywords meta tag for ranking for many years because it was widely abused. A good title, description and content matter instead.
Conclusion
The <head> is the page's ID card: invisible to readers, but read by browsers, search engines and social apps.
Start with
charset,viewportandtitle, in that order.description,robotsandcanonicaltalk to search engines.color-scheme,theme-color, icons and the manifest shape how the browser and phone present your site.Open Graph (
property="og:...") builds link previews.Load CSS in the head; load scripts with
defer(orasyncfor independent ones).Drop outdated tags like
X-UA-Compatible.
In the next chapter, Character Entities & Symbols, we'll see how to write characters like <, &, © and ₹ safely in HTML.