Lesson 42 of 51 · Module 7, lesson 2 of 11 · 3 min

The head: character set, viewport and linked files

Understand every line of the head, and connect icons, stylesheets and scripts.

What you'll learn

  • Why <meta charset> must come first
  • What the viewport line does on phones
  • How the head connects other files: stylesheets, scripts and icons

Why this matters

You have written the head of every page since the first module. Now you know enough to understand every line in it — and to add the lines that connect your page to CSS, JavaScript and an icon.

A complete head

html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Tomato soup — Home Kitchen</title>
  <meta name="description" content="A 30-minute tomato soup for four people.">
  <link rel="icon" href="favicon.png" type="image/png">
  <link rel="stylesheet" href="css/styles.css">
  <script src="js/script.js" defer></script>
</head>
<body>
  <h1>Tomato soup</h1>
</body>
</html>

1. The character set comes first

<meta charset="UTF-8"> tells the browser how to turn the saved bytes of your file into letters. UTF-8 covers every language and symbol. Put it as the first line inside <head>: the browser must know the character set before it reads any text — including the title.

Older pages may declare other character sets, such as ISO-8859-1. Do not use them. Also make sure your editor saves in UTF-8. VS Code does this by default: you can see "UTF-8" in the status bar at the bottom of the window.

2. The viewport

<meta name="viewport" content="width=device-width, initial-scale=1.0"> tells phones: "this page is made for your screen width; start at normal zoom". Without it, phones pretend to be a wide desktop screen and shrink the whole page until the text is tiny.

3. Connected files

LineConnectsLesson
<link rel="icon" href="…">The small icon on the tabFavicons
<link rel="stylesheet" href="…">A CSS file with the page's stylesConnecting CSS to your page
<script src="…" defer></script>A JavaScript fileConnecting JavaScript to your page

All three paths follow the rules in Folders and relative paths: relative, lower case, no slash at the start.

A good order

  1. <meta charset> — always first.
  2. <meta name="viewport">.
  3. <title> and <meta name="description">.
  4. Icons, stylesheets and scripts.

Browsers are forgiving about the rest of the order, but this one is clear for people to read and check.

Old lines you can leave out

  • <meta name="keywords"> — ignored by search engines.
  • <meta http-equiv="refresh"> — reloads or redirects the page after some seconds. It can move people away before they finish reading, which is a problem for many users. Leave it out.
  • <base href="…"> — changes how every relative path on the page works. It causes confusing bugs; you will not need it.

Try it yourself

  1. Open your index.html. Put its head in the order above.
  2. Check the status bar in VS Code: is the file saved as UTF-8?
  3. Remove the viewport line, open the page on a phone (or with the phone view in the developer tools: the phone icon at the top left), and compare. Then put it back.

Common mistakes

  • <meta charset> after the title. Special letters in the title may show as strange symbols. Fix: make it the first line in the head.
  • No viewport line. The page is tiny on phones. Fix: add it to every page.
  • Links to CSS or JavaScript in the <body> by accident, or with a leading slash. Fix: keep them in the head, with relative paths.

Check yourself

  1. Which line must be first inside <head>?
  2. What happens on a phone when the viewport line is missing?
  3. Which element connects a CSS file?

Answers

  1. <meta charset="UTF-8">.
  2. The phone shows a shrunken desktop-size page with tiny text.
  3. <link rel="stylesheet" href="…">.

Next: give your site its own small icon on the browser tab.

Chat with us on WhatsApp