← Back to HTML & CSS, page 8

html-css-0159 · Medium mid

Accented letters show as strange characters such as é. How do you find and fix the encoding problem?

Mojibake means text was written in one encoding and read in another, typically UTF-8 read as Windows-1252. The browser uses a byte order mark, then the Content-Type header, then meta charset. Make the file, header, meta tag and data all use UTF-8.

Full answer

Garbled text like é instead of é is called mojibake. It happens when text is saved in one encoding and read in another. The usual case is a file saved as UTF-8 that the browser reads as a single-byte encoding such as Windows-1252. In UTF-8 the letter é takes two bytes. Read as Windows-1252, each byte is a separate character, which gives à and ©.

The browser picks the encoding in this order:

  1. A byte order mark at the start of the file.
  2. The charset parameter of the Content-Type header sent by the server.
  3. A meta charset tag in the first part of the file (the first 1024 bytes).
  4. A guess, which can be wrong.

The header wins over the meta tag. So a server that sends text/html; charset=iso-8859-1 overrides a correct meta charset="utf-8", and a missing header with a meta tag placed late in the head can also fail.

Debug steps:

  1. Open the network panel and look at the Content-Type header of the page.
  2. In the page, read document.characterSet to see what the browser decided.
  3. Check how the file is saved in the editor, and fix the setting.
  4. Check data that comes from a database, an API or a form: each hop must use UTF-8, including the connection and the table collation.
  5. Put <meta charset="utf-8"> first in the head, and make the server send UTF-8 in the header.

The fix is always to make every part agree, not to replace the characters with entities.

The letter é is stored as the two bytes 195 and 169. Decoded as Windows-1252, they give é. Decoded as UTF-8, they give é. The page itself is read as UTF-8.

Repeated damage cannot be undone by changing the encoding, so fix the source and the data.

html
<p id="out"></p>
<script>
  const bytes = new TextEncoder().encode("é");
  const wrong = new TextDecoder("windows-1252").decode(bytes);
  const right = new TextDecoder("utf-8").decode(bytes);
  window.result = { bytes: Array.from(bytes), wrong, right, set: document.characterSet };
  out.textContent = wrong + " " + right;
</script>

The garbled text was produced on purpose by decoding bytes with the wrong encoding. The test did not use a real server or real response headers. Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • UTF-8 read as a single-byte encoding gives characters like é.
  • Order: byte order mark, header, meta charset, guess.
  • The header overrides the meta tag.
  • Check document.characterSet and the network panel.
  • Every hop of the data must use UTF-8.

Common mistakes

  • Adding a meta charset tag when the server header says something else.
  • Fixing the display with entities and leaving wrong bytes in the database.
  • Placing the meta charset tag far down the head.

Follow-ups

  • Which wins, the Content-Type header or the meta tag?
  • How can a script read the encoding the browser used?

Tips

  • Show the two bytes example for é, and the order the browser uses to decide.
  • Say that the header wins over the meta tag.