Back to HTML

Character Encoding and UTF-8 in HTML

Coding Tutorials Team 26 July 2026

Everyone meets this bug eventually. An apostrophe becomes a sequence of odd symbols, an accented name turns into gibberish, and an em dash arrives as something unpronounceable. The cause is always the same: the bytes were written using one encoding and read using another.

Text is stored as numbers

A computer stores no letters. It stores numbers, and an encoding is the agreed table mapping numbers to characters. The original ASCII table covered 128 characters, which was enough for unaccented English and nothing else. Dozens of incompatible extensions followed, each claiming the numbers above 127 for a different set of characters.

The result was that the same file could be read as three different things depending on assumptions. UTF-8 ended that. It can represent every character in every writing system, it uses a variable number of bytes so common characters stay compact, and it is backwards compatible with ASCII, meaning plain English text is byte for byte identical. There is no situation in modern web development where another encoding is the better choice.

Declaring it in your document

The declaration goes first in the head.

<head> <meta charset="UTF-8"> ...

Its position is not stylistic. The browser must guess an encoding to start reading at all, and it looks for the declaration within the first 1024 bytes. Find it later and the parser has to discard everything and restart, which wastes time and in some cases produces visible flickering. Put it above the title, because a title containing an accented character will otherwise be parsed before the browser knows how to read it.

Three places have to agree

The declaration alone is not sufficient. Encoding is decided in three places and all three must match.

  • The file itself must be saved as UTF-8. Editors have a setting for this, usually in a status bar or a save dialogue, and one file saved in a legacy encoding will misbehave no matter what your markup says.
  • The server should send a matching content type header. This header takes priority over your meta tag, so a server declaring a different encoding wins and your declaration is ignored.
  • Any database connection must use a UTF-8 character set. Text can be stored correctly and still be corrupted in transit if the connection assumes something else.

When text is wrong on a page, work through these three in order. The answer is nearly always the one you did not think to check.

Reading the symptoms

The corruption pattern tells you what happened. A capital A with a circumflex appearing before other symbols means UTF-8 bytes were read as a single byte legacy encoding, the most common direction of failure. Black diamonds containing a question mark mean the browser received bytes that are not valid UTF-8 at all, usually because the file was saved in something else. Plain question marks in place of characters usually mean the text passed through a system that could not represent them and substituted a placeholder, which is unrecoverable because the original information is gone.

Do not fix it in the wrong place

The tempting response to broken characters is to replace each one with an entity. This treats a symptom. The underlying mismatch remains, so the next piece of content hits the same problem, and now your source is full of entities obscuring what the text says.

Worse is double encoding, where already correct UTF-8 is converted again by a script assuming it was something else. This produces long strings of symbols and is genuinely hard to reverse, because the original byte boundaries are lost. Correcting the declaration or the file encoding fixes every character at once and prevents recurrence.

A note on byte order marks

Some editors prepend an invisible marker to UTF-8 files. It is unnecessary for UTF-8 and occasionally harmful, because those bytes arrive before anything else and can appear as stray characters at the top of a page or cause a server side header error. Choose the save option without a byte order mark.

Set it once and forget it

Configure your editor to default to UTF-8, include the meta tag in your template, check the server header once, and set your database connection correctly. After that, characters simply work, and you can type any symbol you need directly into your markup without thinking about it again.