Lably
Skip to content

Character encodings in HTML

Use of encoding systems for international characters in HTML

  • 7 min read
  • 3 chapters

Related on Lably

Through HTML, Character (computing) and ASCII

In version 5.3 of the now retired W3C specification, and the current Living Standard published by WHATWG, the only valid encoding is UTF-8.

Specifying the document's character encoding

There are two general ways to specify which character encoding is used in the document.

First, the web server can include the character encoding or "charset" in the Hypertext Transfer Protocol (HTTP) Content-Type header, which would typically look like this:

<span id="mwKg"></span><span class="err" id="mwKw">Content-Type: text/html; charset=utf-8</span>

This method gives the HTTP server a convenient way to alter document's encoding according to content negotiation; certain HTTP server software can do it, for example Apache with the module mod_charset_lite.

Second, a declaration can be included within the document itself.

For HTML it is possible to include this information inside the head element near the top of the document:

<span id="mwPQ"></span><span class="p" id="mwPg"><</span><span class="nt" id="mwPw">meta</span> <span class="na" id="mwQA">http-equiv</span><span class="o" id="mwQQ">=</span><span class="s" id="mwQg">"Content-Type"</span> <span class="na" id="mwQw">content</span><span class="o" id="mwRA">=</span><span class="s" id="mwRQ">"text/html; charset=utf-8"</span><span class="p" id="mwRg">></span>

HTML5 also allows the following syntax to mean exactly the same:

<span id="mwTw"></span><span class="p" id="mwUA"><</span><span class="nt" id="mwUQ">meta</span> <span class="na" id="mwUg">charset</span><span class="o" id="mwUw">=</span><span class="s" id="mwVA">"utf-8"</span><span class="p" id="mwVQ">></span>

XHTML documents have a third option: to express the character encoding via XML declaration, as follows:

<span id="mwXw"></span><span class="cp" id="mwYA"><?xml version="1.0" encoding="utf-8"?></span>

With this second approach, because the character encoding cannot be known until the declaration is parsed, there is a problem knowing which character encoding is used in the document up to and including the declaration itself. If the character encoding is an ASCII extension then the content up to and including the declaration itself should be pure ASCII and this will work correctly. For character encodings that are not ASCII extensions (i.e. not a superset of ASCII), such as UTF-16BE and UTF-16LE, a processor of HTML, such as a web browser, should be able to parse the declaration in some cases through the use of heuristics.

Although HTML written to the current Living Standard is required to be UTF-8, an encoding declaration, in any of the above forms, is nonetheless required. It must be a case-insensitive match for the string "utf-8" and the document must, in fact, be in UTF-8.

Encoding detection algorithm

An "encoding sniffing algorithm" is defined in the specification to determine the character encoding of the document based on multiple sources of input, including:

  1. Explicit user instruction
  2. An explicit meta tag within the first 1024 bytes of the document
  3. A byte order mark (BOM) within the first three bytes of the document
  4. The HTTP Content-Type or other transport layer information
  5. Analysis of the document bytes looking for specific sequences or ranges of byte values, and other tentative detection mechanisms.

Characters outside of the printable ASCII range (32 to 126) may appear incorrectly if the document is served with an incorrect character encoding. This presents few problems for English-speaking users, but other languages regularly—in some cases, always—require characters outside that range. In Chinese, Japanese, and Korean (CJK) language environments where there are several different multi-byte encodings in use, auto-detection is also often employed. Finally, browsers usually permit the user to override incorrect charset label manually as well.

UTF-8 has been the most common character encoding on the Web since 2008, in part because, as an encoding of Unicode, it allows use of the same encoding for all languages. As of January 2026, UTF-8 is used by 98.9% of web sites surveyed by W3Techs. UTF-16 or UTF-32, other encodings of Unicode, are less widely used because they can be harder to handle in programming languages that assume a byte-oriented ASCII superset encoding, and they are less efficient for text with a high frequency of ASCII characters, which is usually the case for HTML documents.

Successful viewing of a page is not necessarily an indication that its encoding is specified correctly. If the page's creator and reader are both assuming some platform-specific character encoding, and the server does not send any identifying information, then the reader will nonetheless see the page as the creator intended, but other readers on different platforms or with different native languages will not see the page as intended.

Sign in to keep reading

2 more chapters are free with a Lably account.

More in Computing and communication