Sub topic 4
Metadata in the Head
This chapter assumes you can write a basic HTML document, know the difference between an element and an attribute, and can open a local file in a browser. Here you will look at the head element: what belongs in it, what each piece does, and which mistakes show up most often.
What the head is for
The head element holds information about the page. Almost none of it appears in the page itself. Browsers, search engines, and social sites read it to decide how to decode, display, and describe the document.
The essential elements
A reliable head needs three things: a character encoding, a viewport setting, and a title.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>My First Page</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Hello, HTML</h1>
</body>
</html>Character encoding
<meta charset="utf-8"> tells the browser how to turn the file's bytes into characters. UTF-8 covers virtually every writing system. Two rules apply:
- The declaration must appear within the first 1024 bytes of the file, so put it first in the head.
- Your editor must actually save the file as UTF-8. If the declaration and the saved encoding disagree, you get garbled text such as
éwhereéshould be.
The viewport
On a phone, browsers pretend the screen is about 980 pixels wide unless the page says otherwise. The viewport meta element switches that off:
width=device-widthsets the layout width to the real screen width.initial-scale=1sets the starting zoom to 100%.
Without this line, a page that is otherwise mobile-friendly can appear zoomed out and tiny.
The title
The title element sets the text shown in the browser tab, in bookmarks, and as the headline in search results. It is also the first thing a screen reader announces when a page loads. Every page needs exactly one, and it should be unique and descriptive. "Home" on every page is unhelpful. "Growing tomatoes – Garden Notes" is useful.
Linking resources
The link element connects the document to an external resource. The rel attribute names the relationship, and href gives the location. Stylesheets are the most common use. Another is the favicon, the small icon in the tab:
<link rel="icon" href="/favicon.ico">meta and link are void elements. They have no content and no end tag.
A description for search results
Another useful meta element is the description. Search engines often use it as the snippet under your title.
<meta name="description" content="Practical tips for growing tomatoes in a small garden.">Notice the pattern. Most meta elements use a name and a content attribute as a pair. The charset form is a special case with a single attribute.
Common errors
| Mistake | Effect | Fix |
|---|---|---|
| Missing `title` | Tab shows the file name or URL. Validators report an error. | Add one unique `title` |
| `charset` placed late in the head | Browser may guess the encoding wrong | Make it the first element in the head |
| No viewport meta | Page looks tiny on phones | Add the viewport line |
| Visible content in the head, such as a `p` | Browser closes the head early and moves content into the body | Keep content in the body |
Exercise
A page shows the text "Café" instead of "Café". The head contains <meta charset="utf-8">. What is the most likely cause, and how would you fix it?
Show answer
The file was probably saved in a different encoding, such as Windows-1252, so the declaration and the actual bytes disagree. Re-save the file as UTF-8 in your editor. Also confirm that the charset meta element is within the first 1024 bytes.
Exercise
Write a head for a page called "Spring Recipes" that includes the encoding, the viewport, a title, and a link to recipes.css.
Show answer
<head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Spring Recipes</title> <link rel="stylesheet" href="recipes.css"> </head>