Sub topic 4
Metadata in the head
This chapter assumes you can write the document skeleton from the previous chapter (doctype, html, head, body) and that you know what an element and an attribute are. It covers what belongs in the head element, what each common piece does, and the errors that come up most often.
What the head is for
The head element holds information about the page. The browser reads it but does not draw it in the page area. The body holds the content people see. Metadata in the head affects the browser tab, search results, character decoding, mobile layout, and which other files the page loads.
The elements allowed in the head are:
| Element | Purpose |
|---|---|
| `title` | The page name shown in tabs, bookmarks and search results |
| `meta` | Generic metadata such as encoding, viewport and description |
| `link` | A relationship to an external resource such as a stylesheet or icon |
| `style` | CSS written directly in the document |
| `script` | JavaScript, either inline or loaded from a file |
| `base` | A base URL for relative URLs in the document |
A head must contain exactly one title element, unless the page's title is supplied by a higher-level protocol such as an HTML email. Everything else is optional.
A complete head
This example uses the elements you will need in almost every page.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Growing Tomatoes | Garden Notes</title>
<meta name="description" content="A practical guide to planting, watering and harvesting tomatoes.">
<link rel="stylesheet" href="styles.css">
<link rel="icon" href="favicon.ico">
</head>
<body>
<h1>Growing Tomatoes</h1>
<p>Tomatoes need sun and regular watering.</p>
</body>
</html>The sections below take each line in turn.
The title element
The title element contains plain text only. Child elements are not parsed as markup inside it, so <title>My <em>Page</em></title> shows the literal tags. The text appears in several places:
- The browser tab and window title bar.
- The bookmark name when a user saves the page.
- The headline link in most search results.
- The accessible name that a screen reader announces when the page loads.
Because screen reader users often hear the title first, it should identify the page on its own. A pattern that works well is the specific page name first, then the site name: Growing Tomatoes | Garden Notes. If every page on a site has the title "Home", users with many tabs open cannot tell them apart.
The title is not the same as the h1. The title names the page in contexts outside the page, such as a tab list. The h1 is the main heading inside the page. They are often similar but need not match exactly.
Character encoding with meta charset
A computer stores text as numbers. A character encoding is the table that maps those numbers to characters. If the browser guesses the wrong table, you see garbled text such as é where é should be.
<meta charset="utf-8">This declares UTF-8, which can represent essentially every writing system. Three rules prevent most encoding bugs:
- Use
utf-8. The HTML standard requires new documents to use it. - Put the
meta charsetelement inside the first 1024 bytes of the file. Browsers only scan that far for it, so place it first in thehead, before thetitleand before any comments that might be long. - Make sure your editor actually saves the file as UTF-8. The declaration only tells the browser what to expect. It does not convert the file.
Because charset is declared in the head, you can still use character references such as © in the body. With UTF-8 you can also type the © character directly.
The viewport meta element
Mobile browsers historically pretended the screen was about 980 pixels wide, then shrank the page to fit. That made unadapted pages tiny. The viewport meta element tells the browser to use the real device width.
<meta name="viewport" content="width=device-width, initial-scale=1">The content value is a comma-separated list of settings:
width=device-widthsets the layout width to the device's screen width in CSS pixels.initial-scale=1sets the starting zoom level to 100 percent.
Without this line, responsive CSS such as media queries will not behave as you expect on a phone. It has no visible effect on a desktop browser, so you may not notice it is missing until you test on a small screen.
Avoid adding user-scalable=no or maximum-scale=1. These block pinch-to-zoom, which people with low vision rely on, and accessibility checkers flag them.
Named metadata with meta name
A meta element with a name attribute gives a named piece of information, and its content attribute gives the value. A meta element is a void element, so it has no end tag and no content between tags.
<meta name="description" content="A practical guide to planting, watering and harvesting tomatoes.">
<meta name="author" content="Priya Nair">
<meta name="theme-color" content="#2e7d32">The description value is the most useful. Search engines often show it as the snippet under the result title, though they may choose other text from the page instead. Write one sentence or two that summarises that particular page, and use a different description for each page.
Browsers ignore names they do not recognise, so a typo such as name="descripton" fails silently. The validator, covered in a later chapter, will not always catch a misspelt name either, so check by eye.
Linking resources with the link element
The link element connects the document to another resource. The rel attribute (short for relationship) says what that relationship is, and href gives the URL. Like meta, link is a void element.
<link rel="stylesheet" href="styles.css">
<link rel="icon" href="favicon.ico">
<link rel="canonical" href="https://example.com/tomatoes">Common rel values:
| `rel` value | Meaning |
|---|---|
| `stylesheet` | Load this CSS file and apply it to the page |
| `icon` | The small image shown in the tab and bookmarks |
| `canonical` | The preferred URL when several URLs show the same content |
| `preload` | Fetch this resource early because the page will need it soon |
The href value follows the same URL rules you will meet in the chapter on links. A relative URL such as styles.css is resolved against the location of the HTML file. If the stylesheet is in a folder, write css/styles.css. A wrong path is the most common reason a page appears unstyled. The browser's Network tab shows the request as a 404 error.
A stylesheet link blocks rendering by default. The browser waits for the CSS before painting, to avoid showing unstyled content. So put stylesheet links in the head, not at the end of the body.
Inline style and script in the head
The style element holds CSS directly in the document:
<style>
body { font-family: sans-serif; }
</style>The script element loads or contains JavaScript. A script in the head runs before the body exists, so it cannot find body elements unless you delay it. The defer attribute tells the browser to download the file while parsing and run it after the document has been parsed.
<script src="app.js" defer></script>For anything beyond a few lines, an external stylesheet is easier to cache and maintain than an inline style element.
The base element
The base element sets the URL that relative links resolve against. It affects every relative URL on the page, including links that start with #, which is a frequent source of confusion. Use it rarely, and place it before any element that uses a relative URL.
<base href="https://example.com/docs/">How the browser processes the head
Common errors
- Content in the head that belongs in the body. If you put a
pordivin the head, the parser closes the head early and starts the body there. Any metadata after it ends up in the wrong place and may be ignored. - Missing
title. Browsers show the file name or URL instead, and validators report an error. - Charset declared too late. If it falls after the first 1024 bytes, the browser may guess the encoding.
- Missing viewport. The page looks fine on desktop and tiny on a phone.
- Wrong
linkpath. The page loads without styles. Check the Network tab for a 404. - Writing
<meta charset="utf-8" />or<link ... />. The trailing slash is allowed in HTML but has no effect. Do not rely on it to "close" anything.
Checking your work
Open the page in a browser and check each item:
- The tab shows your title text.
- Special characters such as
éand©display correctly. - In developer tools, switch on the device toolbar to emulate a phone. The layout should fill the screen width.
- In the Network tab, reload and confirm that the stylesheet and icon return status 200.
Exercise
A page's CSS file is correctly written, but the page appears with no styling. The head contains <link rel="stylesheet" href="styles.css"> and the stylesheet is stored in a folder named css next to the HTML file. What is wrong, and how do you fix it?
Show answer
The href is resolved relative to the HTML file, so the browser looks for styles.css next to the page and gets a 404. Change the value to css/styles.css. The Network tab in the browser's developer tools confirms the fix when the request returns status 200.
Exercise
Which two meta elements should appear in nearly every page head, and what goes wrong on a phone if one of them is missing?
Show answer
<meta charset="utf-8"> and <meta name="viewport" content="width=device-width, initial-scale=1">. Without the viewport element, a phone lays the page out at about 980 pixels wide and shrinks it, so text is tiny and responsive CSS does not apply. Without the charset element (or with it placed after the first 1024 bytes), the browser may guess the wrong encoding and show garbled characters.