Skip to content

How to declare the character encoding

Best practices1 min readFixes check charset

Declaring UTF-8 in the first 1024 bytes stops browsers from guessing and prevents garbled characters.

Step by step, with screenshots: HTML validation errors: which ones matter and which are noise →

Check your own site

Runs this check and the other 192, free, in about 45 seconds.

What it is

Character encoding tells the browser how to turn the bytes of a page into letters. You declare it with <meta charset="utf-8"> at the top of the head, or with charset=utf-8 in the Content-Type HTTP header. UTF-8 covers every language and is used by over 98 % of websites.

The browser needs the encoding before it reads any text. If it is missing, declared after the first 1024 bytes or does not match how the file was saved, accented letters such as č, ć, é and ü can turn into garbage like ć, and the browser may have to start rendering again once it finds the declaration.

OK looks like this: <meta charset="utf-8"> as the first element in the head, within the first 1024 bytes, and the file actually saved as UTF-8. getReport checks that the encoding is declared early enough.

What a passing site looks like

  • Character encoding is declared earlywarning · −1 ptseffort S

Character encoding is not declared early enough

Why it matters. Browsers need to know the encoding before they read the text. When it is missing or declared late, accented letters such as č, ć and š can appear as garbage and the page re-renders.

How to fix it.
  1. Put <meta charset="utf-8"> as the first element inside <head>, within the first 1024 bytes of the document.
  2. Or send it in the response header: Content-Type: text/html; charset=utf-8.

Questions people ask

What does meta charset utf-8 mean?

It is a tag, <meta charset="utf-8">, that tells the browser the page's text is encoded in UTF-8. With it, the browser decodes every character correctly from the start instead of guessing. It belongs at the very top of the head, before the title and any other text.

What is UTF-8?

UTF-8 is a character encoding that can represent every character in Unicode, from English and Croatian letters to Chinese and emoji, using one to four bytes per character. It is compatible with ASCII and is the standard encoding of the web, so it is the right choice for every new page.

Is meta charset utf-8 necessary?

Yes, unless the server already sends Content-Type: text/html; charset=utf-8, and even then it is a safe backup. Without any declaration, browsers guess the encoding from the content, which can garble accented letters. HTML validators and accessibility checks also expect it.

Where do you put meta charset utf-8?

As the first element inside the head, before the title, and within the first 1024 bytes of the file. Browsers only look for the declaration that early; if long comments, scripts or styles come first, they may miss it or have to re-parse the page.

Filed under Best practices. Copy is generated from the same catalogue that scores every report, so what you read here is what the report says.