TExcelTAD
Home › HTML › HTML Charset

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

html
<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.

html · try it yourself
<!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.

html · try it yourself
<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.

html · try it yourself
<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

  1. Save your files as UTF-8 in your editor (VS Code does this by default; look at the status bar).
  2. Add <meta charset="UTF-8"> as the first thing in <head>.
  3. Make sure the web server does not send a different charset in its Content-Type header — 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-8 and Utf-8 all mean the same thing.

Test yourself

Where should <meta charset="UTF-8"> go?

  1. At the end of <body>
  2. As the first element in <head>
  3. Inside <title>
  4. Anywhere; position doesn't matter

How many bytes does UTF-8 use for the letter A?

  1. 1
  2. 2
  3. 3
  4. 4

You see Café instead of Café. What is the most likely cause?

  1. The font is missing
  2. UTF-8 bytes were decoded with a different charset
  3. The <p> tag is not closed
  4. CSS is disabled