HTML Charset
Computers store text as numbers. A character encoding (or charset) is the rulebook that says which number means which character. If the browser uses a different rulebook from the one your file was saved with, readers see nonsense such as Café instead of Café. HTML fixes this with one short line in the <head>.
The one line you need
<meta charset="UTF-8">Put it as the first element inside <head>. Browsers only look at the first 1024 bytes of a page to find the charset, so placing it early guarantees it is seen before any text is decoded.
<!DOCTYPE html>
<html lang="vi">
<head>
<meta charset="UTF-8">
<title>Charset demo</title>
</head>
<body>
<p>Vietnamese: Xin chào, thế giới!</p>
<p>French: Café crème, s'il vous plaît.</p>
<p>Japanese: こんにちは</p>
<p>Emoji: 🌐 ✨</p>
</body>
</html>All of these languages live happily in a single UTF-8 page.
A short history of charsets
| Charset | Year | Characters | Notes |
|---|---|---|---|
| ASCII | 1963 | 128 | English letters, digits, punctuation, control codes |
| ISO-8859-1 (Latin-1) | 1987 | 256 | Adds Western European accents like é, ñ, ü |
| Windows-1252 | 1990s | 256 | Microsoft's variant of Latin-1 with curly quotes and € |
| UTF-8 | 1993 | 1,100,000+ | Covers every Unicode character; the default for HTML5 |
ASCII only had room for 128 characters — enough for English, but nothing else. Each region then invented its own 256-character table, which caused chaos when text moved between countries. Unicode solved this by giving every character in every writing system one unique number, and UTF-8 is the most popular way to store those numbers as bytes.
How UTF-8 stores characters
UTF-8 uses a variable number of bytes per character:
| Character range | Bytes | Example |
|---|---|---|
| ASCII (U+0000–U+007F) | 1 | A |
| Latin, Greek, Cyrillic… | 2 | é, Ω |
| Most other scripts, € | 3 | €, こ, ế |
| Emojis and rare symbols | 4 | 😀 |
Because plain ASCII characters still use one byte with the same values, any old ASCII file is already valid UTF-8. The playground below uses JavaScript's TextEncoder to show the real bytes.
<style>
td, th { border: 1px solid #ccc; padding: 4px 10px; font-family: monospace; }
table { border-collapse: collapse; }
</style>
<table id="t"><tr><th>Char</th><th>Code point</th><th>UTF-8 bytes</th></tr></table>
<script>
const enc = new TextEncoder();
for (const ch of ['A', 'é', '€', 'ế', '😀']) {
const bytes = [...enc.encode(ch)].map(b => b.toString(16).toUpperCase().padStart(2, '0'));
const row = document.createElement('tr');
row.innerHTML = `<td>${ch}</td><td>U+${ch.codePointAt(0).toString(16).toUpperCase()}</td><td>${bytes.join(' ')}</td>`;
document.getElementById('t').append(row);
}
</script>What goes wrong: mojibake
When UTF-8 bytes are read as Windows-1252, each multi-byte character falls apart into several wrong characters. This garbled text even has a name: mojibake. The demo below decodes the same bytes with two different rulebooks.
<p id="good"></p>
<p id="bad"></p>
<script>
const bytes = new TextEncoder().encode('Café ở Hà Nội €5');
document.getElementById('good').textContent =
'Read as UTF-8: ' + new TextDecoder('utf-8').decode(bytes);
document.getElementById('bad').textContent =
'Read as windows-1252: ' + new TextDecoder('windows-1252').decode(bytes);
</script>Checklist for correct characters
- Save your files as UTF-8 in your editor (VS Code does this by default; look at the status bar).
- Add
<meta charset="UTF-8">as the first thing in<head>. - Make sure the web server does not send a different charset in its
Content-Typeheader — the header wins over the meta tag.
Tip: The older, longer form
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">still works, but the short HTML5 form does the same job and is easier to remember.
Note: The value is case-insensitive:
UTF-8,utf-8andUtf-8all mean the same thing.
Test yourself
Where should <meta charset="UTF-8"> go?
- At the end of
<body> - As the first element in
<head> - Inside
<title> - Anywhere; position doesn't matter
How many bytes does UTF-8 use for the letter A?
- 1
- 2
- 3
- 4
You see Café instead of Café. What is the most likely cause?
- The font is missing
- UTF-8 bytes were decoded with a different charset
- The
<p>tag is not closed - CSS is disabled