Sub topic 3
Declaring UTF-8 with meta charset
This chapter assumes you know basic HTML syntax, the structure of an HTML document (doctype, html, head, body), and that a browser turns bytes from a server into a page. It covers the one meta tag that tells the browser how to read those bytes.
Why encoding matters
A file on disk or on the wire is a sequence of bytes. A character encoding is the rule that maps those bytes to characters. The same bytes produce different text under different rules. The word "café" saved as UTF-8 contains the two bytes C3 A9 for the letter "é". A browser that wrongly assumes the older Windows-1252 encoding reads those two bytes as "é" and shows "café". This garbled output is called mojibake.
UTF-8 is the encoding you should use. It can represent every character in Unicode, including accented letters, non-Latin scripts, symbols, and emoji. It is also the encoding the HTML Standard recommends for new documents.
The tag
Declare the encoding with a meta element that has a charset attribute:
<meta charset="utf-8">The value is case-insensitive, so UTF-8 works too. Write it with the exact attribute shown. It takes no content attribute, which makes it different from the name/content pairs you saw in the previous chapter.
Where it must go
The browser reads the start of the file before it knows the encoding, so it needs to find the declaration early. The rule is that the meta charset element must appear within the first 1024 bytes of the document. The safest habit is to make it the first element inside head:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Café Menu</title>
</head>
<body>
<h1>Café Menu</h1>
<p>Crème brûlée, 日本茶, and a ☕ to finish.</p>
</body>
</html>If a long comment, a large inline script, or many other tags come first, the declaration can fall past byte 1024. The browser then may guess the encoding, and the guess can be wrong. Keeping meta charset first, before the title, avoids this. The title is the clearest case. A title containing "é" or "—" is itself encoded text, and it must be read after the encoding is known.
How the browser decides
A browser does not rely only on the tag. It checks several sources in a fixed order, and the first match wins:
- A byte order mark (BOM) at the start of the file.
- The
charsetparameter of the HTTPContent-Typeheader, for exampleContent-Type: text/html; charset=utf-8. - The
meta charsetelement, found by a quick scan of the first bytes. - A guess based on the content, or a locale default.
The consequence is that the HTTP header overrides the meta tag. If your server sends charset=iso-8859-1 and your HTML says utf-8, the browser follows the header, and the page shows mojibake even though the markup looks correct.
Common errors
- The file is not saved as UTF-8. The tag only describes the file. If your editor saved it as Windows-1252, declaring UTF-8 does not convert it. Set the editor's file encoding to UTF-8.
- Server and tag disagree. Check the
Content-Typeresponse header in the browser's Network panel. Fix the server setting or remove its charset override. - Declaration too late. Move the tag to the top of
head. - Using
http-equivfor encoding. The older form<meta http-equiv="Content-Type" content="text/html; charset=utf-8">is valid but longer. Prefer the short form. - Using HTML entities as a workaround. Writing
éfor every accented letter works, but it hides the real problem and does not help with text in scripts or attributes.
Exercises
Exercise
Your HTML file is saved as UTF-8 and contains <meta charset="utf-8"> as the first element in head. The page still shows "café". The server sends Content-Type: text/html; charset=iso-8859-1. Why does this happen, and what should you change?
Show answer
The HTTP Content-Type charset takes priority over the meta tag, so the browser decodes the UTF-8 bytes as ISO-8859-1. Change the server configuration to send charset=utf-8 (or remove its charset override so the meta tag is used).
Exercise
Why should meta charset come before the title element, and what is the byte limit for finding it?
Show answer
The browser needs to know the encoding before it decodes text such as the title. The declaration must appear within the first 1024 bytes of the document, so putting it first in head keeps it safely inside that limit.