HTML Head and Metadata
Open any web page, and you see its body: the headings, pictures, and links. But every page also has a part you never see, called the head. It is the page's control room. It holds the title that appears in the browser tab, the instructions that tell the browser which letters to use, which stylesheets to load, and which summary a search engine or a chat app should show when someone shares your page.
The information stored there is called metadata: data about the page, as opposed to the content of the page. A well-made head makes a page load correctly, look right on a phone, rank sensibly in search results, preview nicely when shared, and work for everyone.
What You Will Learn in This Sectionβ
- How an HTML document is organised: the
<html>and<head>elements - Naming a page with
<title> - Describing a page with
<meta>, and getting the character set and viewport right - Adding a favicon
- Connecting stylesheets, scripts, and other files with
<link>,<style>,<script>, and<base> - Metadata best practices for search, sharing, and accessibility
The Big Ideaβ
- HTML Code
- Explore the Head
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Sourdough for Beginners | The Bread Bin</title>
<meta name="description" content="A gentle, step-by-step guide to your first sourdough loaf.">
<link rel="icon" href="/favicon.svg" type="image/svg+xml">
<link rel="stylesheet" href="/css/site.css">
<script src="/js/app.js" defer></script>
</head>
<body>
<h1>Sourdough for Beginners</h1>
<p>Welcome!</p>
</body>
</html>
<title>Sourdough for Beginners | The Bread Bin</title>| What it does | Names the page. |
|---|---|
| Who reads it | Browser tabs, bookmarks, history, search results, screen readers, shared links |
| If it is missing | Tabs and results show the web address, or βUntitledβ. It is also a basic accessibility failure. |
| Lesson | Title |
Click through the elements above. For each one, ask three questions: What does it do? Who reads it? What goes wrong if it is missing? These three questions are the key to the whole section.
A very important point: almost nothing in the head is shown on the page itself. The head works behind the scenes, and its readers are not just people. They are browsers, search engines, social networks, screen readers, and operating systems.
Try It: Read the Head of This Pageβ
The page you are reading right now has a head. Press the button, and the page will read its own document.head, and show you what is inside.
- HTML Code
- Browser Output
// Everything in the head is available to JavaScript
console.log(document.title); // the page title
console.log(document.documentElement.lang); // the lang attribute
console.log(document.characterSet); // the character encoding
for (const element of document.head.children) {
console.log(element.tagName, element.getAttribute("name") || element.getAttribute("rel"));
}
Press the button, and the page will list what is in its own head.
What you see is whatever this documentation site puts in its head. Tools such as Docusaurus add many tags for you. Your own pages can be much smaller.
You can do the same on any web page: right-click, choose "View page source" or "Inspect", and look at the <head>. It is one of the best ways to learn: see how well-made sites set theirs up.
The Smallest Valid Headβ
Every HTML document needs at least these three things in its head:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>A page title</title>
</head>
<body>
<p>Hello</p>
</body>
</html>
| Part | Why it is needed |
|---|---|
<!DOCTYPE html> | Switches the browser into standards mode, so it follows the modern rules |
<html lang="en"> | Names the language, for translation, pronunciation, and spelling |
<meta charset="utf-8"> | Tells the browser how to read the letters |
<title> | Names the page. Required in every document |
For a page that will be used on phones (that is, almost every page), also add the viewport line. Together, these make a good starting template:
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Page title | Site name</title>
What Goes Whereβ
Order matters a little in the head. A good habit:
<meta charset>first, within the first 1024 bytes of the file, so the browser knows how to read everything after it.<meta name="viewport">and<title>next.- Other
<meta>and<link>tags: description, canonical address, icons, social previews. - Stylesheets, so the page can be styled as early as possible.
- Scripts, usually with
defer, so they do not block the page from appearing.
You will learn the reasons for each choice in the lessons that follow.
What Is in This Sectionβ
ποΈ HTML
Learn the root of every web page: the doctype, the html element, the lang and dir attributes, how the document is split into head and body, and what happens when you leave them out. Includes a live document-skeleton builder and a quirks-mode check.
ποΈ Head
Learn what belongs inside the HTML head element, what does not, and what the browser does when you break the rules. Includes a live parser lab that shows where the browser really puts each element, and a simulation of how script loading affects your page.
ποΈ Title
Learn how to write great HTML page titles: where the title shows up, how long it should be, how to structure it for search results and screen readers, what characters are safe, and how to update it in single-page apps. Includes a live tab and search-result preview.
ποΈ Meta
Learn the HTML meta element: name and content pairs, http-equiv, descriptions, robots, theme-color, color-scheme, and social sharing tags. Includes a live meta tag checker that uses the browser's real parser, and a social card preview builder.
ποΈ Charset
Learn how character encoding works in HTML: what UTF-8 is, how meta charset tells the browser how to read your page, why text turns into garbled symbols, and how to fix it. Includes a live encoding lab with real bytes and a real page rendered with different encodings.
ποΈ Viewport
Learn the viewport meta tag: why phones need it, what width=device-width and initial-scale=1 do, why you should never block zoom, safe-area settings for notched phones, and how it connects to responsive CSS. Includes a live viewport reader and a mobile page simulator.
ποΈ Link
Learn the HTML link element: how it connects your page to stylesheets, icons, canonical addresses, translated versions, feeds, and performance hints like preload and preconnect. Includes a live stylesheet lab, a real link-tag checker, and an hreflang builder.
ποΈ Favicon
Learn how to add a favicon to your website: which files you need (SVG, ICO, Apple touch icon, manifest icons), the link tags to use, how to design an icon that stays readable at 16 pixels, and how to fix caching problems. Includes a live icon designer that changes this page's real tab icon.
ποΈ Style
Learn the HTML style element: how to write CSS directly inside your page, when that is better than an external file, how the order of rules decides which one wins, and how a Content-Security-Policy can block inline styles. Includes a live CSS editor, a cascade-order lab, and a real CSP experiment.
ποΈ Script
Learn the HTML script element: inline and external scripts, defer, async and type module, where to place scripts, data blocks like JSON-LD, noscript fallbacks, and how to load JavaScript safely. Includes a live loading-order experiment, a script tag checker, and a JSON data block demo.
ποΈ Base
Learn the HTML base element: how it changes the starting address of every relative link, image and form, how it affects fragment links like #top, how base target works, why it is risky, and what to use instead. Includes a live URL resolver and a base tag checker.
ποΈ Metadata Best Practices
Bring the whole HTML head together: a complete, correctly ordered head template, a checklist for essentials, search, sharing, performance, accessibility, security and global audiences, and tools to test it. Includes a live head auditor that scores your page, and a head generator that builds a complete head for you.
What Can Go in the Head?β
Only a short list of elements is allowed in <head>:
| Element | Purpose |
|---|---|
<title> | The page name (required) |
<meta> | Information about the page |
<link> | A relationship with another file, such as a stylesheet or icon |
<style> | CSS inside the page |
<script> | JavaScript (or data) |
<base> | The base address for relative links |
<noscript> | What to show when scripts are off (limited use in the head) |
<template> | A fragment that is not shown until script uses it |
If you put something else in the head, such as a <div> or a <p>, the browser closes the head early and starts the body. Everything after that point is treated as body content, including any <meta> or <link> tags that follow. That is a common cause of "my tags do nothing" bugs.
Good Habits from the Startβ
- Always include
lang,charset,viewport, andtitle. The four basics. - Give every page its own title and description. Copy-pasted ones help no one.
- Do not block the page. Load scripts with
defer, and keep the head light. - Never disable zoom. People with low vision rely on it.
- Do not trust any value that comes from a visitor when you build a head on a server. Escape it.
Everything in the head, including comments, is visible to anyone who views the page source. Never put passwords, private notes, internal server names, or unreleased plans in it.
Accessibility at a Glanceβ
langon<html>lets screen readers choose the right pronunciation, and browsers offer correct translation and spell checking.- A unique, descriptive
<title>is the first thing a screen reader announces on every new page, and the label in the browser's tab list and history. - A correct
viewport, withoutuser-scalable=no, lets people zoom. charsetkeeps text readable in every language, including text read aloud.- A stylesheet that loads reliably keeps contrast, focus rings, and layout intact.
- Scripts that load without blocking keep the page usable quickly, including with assistive technology.
Security and Privacy Notesβ
- Anything in the head is public. Do not put secrets there, not even in comments or hidden
metatags. - Scripts and styles from other sites run with full power on your page. Load only what you trust, pin versions, and use Subresource Integrity where you can. See Third-Party Content Security.
- A
<base>tag changes every relative link and form address. An attacker who can inject one can redirect your visitors. Keep it out unless you need it, and put it carefully. - If a server builds the head from visitor input (for example, a title that includes a search term), escape it, so no one can inject extra tags.
- A Content-Security-Policy in a
<meta>tag helps, but a response header is stronger. - Metadata can reveal more than you think: editor names in comments, internal paths in script addresses, and draft notes. Review the source before publishing.
Global Audience Notesβ
- Set
lang, and use language codes that match your content (en,hi,ar,pt-BR). For right-to-left languages, also setdir="rtl". - Always use UTF-8 (
<meta charset="utf-8">), which covers every language. Save your files in UTF-8 as well. - Write the title and description in the language of the page, and keep titles short enough that translations do not become too long.
- Tell search engines about other language versions with
<link rel="alternate" hreflang="β¦">. - Think about slow connections: a lean head, deferred scripts, and a small stylesheet help most where the network is weakest.
- Use the viewport tag, because in many regions, most visitors use phones.
Common Mistakes to Avoidβ
- No
<title>, or one title for the whole site. - No
charset, so accents and symbols break. - No
viewport, so phones show a tiny desktop page. - Disabling zoom with
user-scalable=noormaximum-scale=1. - Putting
<div>or text in the head, which ends the head early. - Blocking scripts in the head with no
defer. - Forgetting
langon<html>. - Missing doctype, so the browser enters quirks mode.
- Duplicate or conflicting tags (two titles, two viewports).
- Secrets in comments or metadata.
- A
<base>tag added without thinking, breaking every relative link. - Copying one head to every page, with the same title and description.
Practice Timeβ
Try these in your editor:
- Write the smallest valid HTML document, with a
lang,charset, andtitle. - Add the
viewportmeta tag, and open the page on a phone, or in your browser's phone view. - Remove the
charset, add some accented letters and an emoji, and see what happens. - Add a description, and a link to an icon.
- Open any website, view the source, and list five things in its head.
- Use the button above to read this page's head, and find its
viewportandlang. - Put a
<p>in the head, and look at the page in your browser's inspector to see what the browser did. - Add a stylesheet with
<link>, and a script withdefer. - Give three different pages three different titles and descriptions.
- Bonus: use a free online checker for your page's metadata, and fix the first three problems it finds.
Quick Recapβ
Click a question to check your answer.
1. What is the head of an HTML page?
The part that holds information about the page (metadata) and links to the files it needs. It is not shown on the page itself, except for things like the title in the tab and the icon.
2. Which four things should every page's head include?
A lang attribute on <html>, <meta charset="utf-8">, a viewport meta tag, and a unique <title>.
3. Who reads the head?
Browsers, search engines, social networks and messaging apps, screen readers, and operating systems. People mostly see only its effects.
4. Why must <meta charset> come early?
The browser needs to know how to read the letters before it reads the rest of the file. It should appear within the first 1024 bytes.
5. What happens if you put a <div> in the head?
The browser closes the head and starts the body at that point. Any <meta> or <link> after it is then in the body, and may not work as intended.
6. Is the head private?
No. Anyone can view the page source, so never put secrets, private notes, or internal details in the head.
Key Takeawaysβ
- The head holds metadata: information about the page, for browsers, search engines, social apps, and assistive technology.
- Every page needs a doctype,
lang,charset, andtitle, and nearly every page needs a viewport tag. - Put
charsetfirst, then the viewport and title, then othermetaandlinktags, then styles, and then scripts withdefer. - Only
title,meta,link,style,script,base,noscript, andtemplatebelong in the head. - Give each page its own title and description.
- Never disable zoom, and never hide secrets in the head.
- Treat third-party files and server-built values with care.
- Use UTF-8 and
lang, write for your audience's language, and keep the head light for slow connections.
Ready to start? Begin with HTML, the root element that holds everything.