HTML Character Encoding Declaration, Parsing, Meta Charset and HTTP Headers
How HTML declares UTF-8 via meta charset and Content-Type, how parsers decode bytes, and how to fix missing or late declarations.
This page summarises the WHATWG HTML Living Standard on character encoding declarations and byte-stream parsing, together with practical guidance from Google Search Central, Chrome DevTools performance insights, Lighthouse, and Cloudflare on fixing garbled text, slow reparsing, and CDN-related charset mistakes.
Character encoding: what bytes mean in an HTML document
Section titled “Character encoding: what bytes mean in an HTML document”A character encoding (or encoding when unambiguous) is a defined way to convert between byte streams and Unicode strings, as specified in the Encoding standard. Each encoding has a name and one or more encoding labels used to identify it.
A character encoding, or just encoding where that is not ambiguous, is a defined way to convert between byte streams and Unicode strings, as defined in Encoding. An encoding has an encoding name and one or more encoding labels, referred to as the encoding’s name and labels in the Encoding standard.
— WHATWG HTML, Common infrastructure
Servers and browsers exchange HTML as bytes. Without an agreed encoding, the user agent cannot know which character each byte represents. A character encoding declaration is the mechanism that states which encoding was used to store or transmit the document.
A character encoding declaration is a mechanism by which the character encoding used to store or transmit a document is specified.
— WHATWG HTML, The elements of HTML — Specifying the document’s character encoding
UTF-8: what authors must use for modern HTML
Section titled “UTF-8: what authors must use for modern HTML”For new HTML on the open web, authors should treat UTF-8 as the only sensible choice. The Encoding standard requires UTF-8 and the utf-8 label; the HTML specification ties author requirements to that rule.
The Encoding standard requires use of the UTF-8 character encoding and requires use of the “utf-8” encoding label to identify it. Those requirements necessitate that the document’s character encoding declaration, if it exists, specifies an encoding label using an ASCII case-insensitive match for “utf-8”. Regardless of whether a character encoding declaration is present or not, the actual character encoding used to encode the document must be UTF-8.
— WHATWG HTML, Specifying the document’s character encoding
Google Search Central recommends Unicode/UTF-8 where possible for pages you want crawled with predictable metadata.
Using a non-UTF-8 encoding can break form submission and URL encoding, which default to the document’s character encoding. A declaration is still required even when the visible text is all ASCII, because users may type non-ASCII in forms and scripts may build URLs with non-ASCII characters.
Authoring tools must default to UTF-8 for newly created documents.
How to declare charset: meta charset and Content-Type
Section titled “How to declare charset: meta charset and Content-Type”You need at least one valid declaration so the browser does not guess wrong. Chrome’s Declare a character encoding insight passes when either condition is met:
- A
<meta charset>element in the<head>that is completely contained in the first 1024 bytes of the document, or - A
Content-TypeHTTP response header with acharsetdirective that matches a valid IANA name.
meta charset in the first 1024 bytes
Section titled “meta charset in the first 1024 bytes”Place <meta charset="utf-8"> near the top of the document, inside <head>. Best practice is to make it the first element in <head>.
Add a
<meta charset>element within the first 1024 bytes of your HTML document. The element must be fully contained within the first 1024 bytes. The best practice is to make the<meta charset>element the first element in the<head>of your document.— Chrome DevTools, Declare a character encoding
Example markup from the HTML specification:
<meta charset="utf-8">Restrictions on the meta-based declaration:
| Rule | Why it matters |
|---|---|
| No character references or escapes in the declaration | The declaration must appear literally in the byte stream |
The whole <meta> must fit in the first 1024 bytes |
Late declarations trigger sniffing or a full re-parse |
| Only one meta-based character encoding declaration per document | Duplicate or conflicting meta tags confuse parsers |
If the document has no BOM, encoding is not given by Content-Type metadata, and the document is not an iframe srcdoc document, the encoding must be specified with <meta charset> or <meta http-equiv> in the Encoding declaration state.
For the legacy form, Google notes that content on http-equiv must be quoted—otherwise charset may be parsed incorrectly:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">iframe srcdoc documents must not include a character encoding declaration; the content is already decoded from the parent document.
In XML, use an XML declaration instead of HTML meta rules, for example <?xml version="1.0" encoding="utf-8"?> at the top of the markup.
Content-Type response header with charset
Section titled “Content-Type response header with charset”Configure the origin (or CDN) to send:
Content-Type: text/html; charset=UTF-8Configure your server to add a Content-Type HTTP response header that includes a charset directive.
— Lighthouse, Charset declaration is missing or occurs too late in the HTML
Cloudflare recommends explicitly setting the correct charset in Content-Type on the origin for all text/html pages, especially when not using UTF-8 for the file bytes—though for new work, UTF-8 for both file and header is the aligned choice.
Input byte stream: how the HTML parser chooses an encoding
Section titled “Input byte stream: how the HTML parser chooses an encoding”Before tokenisation, the parser sees a stream of bytes. The user agent decodes bytes to code points using a character encoding and a confidence (tentative, certain, or irrelevant).
The stream of code points that comprises the input to the tokenization stage will be initially seen by the user agent as a stream of bytes (typically coming over the network or from the local file system). The bytes encode the actual characters according to a particular character encoding, which the user agent uses to decode the bytes into characters.
— WHATWG HTML, Parsing HTML documents — The input byte stream
Usually the encoding sniffing algorithm determines the encoding unless the encoding is already known with certain confidence—for example when the parser operates on an input byte stream with a known definite encoding.
A leading BOM causes the declared encoding argument to be ignored and the BOM itself is skipped.
Supported encodings in HTML (not everything Unicode allows)
Section titled “Supported encodings in HTML (not everything Unicode allows)”HTML user agents must support the encodings defined in Encoding (including UTF-8, many ISO-8859 and windows-* pages, GBK, Big5, Japanese and Korean legacy sets, UTF-16 variants, and x-user-defined). They must not support other encodings such as CESU-8, UTF-7, or UTF-32; using prohibited encodings leads to unexpected behaviour.
Extracting charset from a meta element
Section titled “Extracting charset from a meta element”When the parser reads a meta declaration, it runs an algorithm distinct from HTTP’s charset rules—for example HTML allows slightly different quoting rules than HTTP charset parameters.
This algorithm is distinct from those in the HTTP specifications (for example, HTTP doesn’t allow the use of single quotes and requires supporting a backslash-escape mechanism that is not supported by this algorithm).
— WHATWG HTML, Extracting character encodings from meta elements
The algorithm searches for the word charset, optional whitespace, =, then reads a quoted or unquoted encoding label.
Changing encoding mid-parse (why a late meta charset hurts performance)
Section titled “Changing encoding mid-parse (why a late meta charset hurts performance)”If sniffing failed or the first guess was wrong, a meta declaration can force a change of encoding while parsing. That can restart navigation: the user agent may re-parse from memory, or in awkward cases misinterpret the resource.
Whenever possible, this should be done without actually contacting the network layer (the bytes should be re-parsed from memory), even if, e.g., the document is marked as not being cacheable.
— WHATWG HTML, Changing the encoding while parsing
Chrome explains the user-visible cost:
A late
<meta charset>element (one that is not fully contained in the first 1024 bytes of the document) can significantly affect load performance as the browser will assume one character encoding, and if it discovers later that it made a wrong assumption, it will need to start parsing the HTML from the beginning.— Chrome DevTools, Declare a character encoding
Special cases from the spec: if the current encoding is UTF-16BE/LE, a newly discovered encoding is ignored; if the new encoding is UTF-16BE/LE it is changed to UTF-8; x-user-defined maps to windows-1252.
This section does not apply to XML documents; XML uses its own encoding determination rules.
Common mistakes: garbled text, CDN rewrites, and invalid <head>
Section titled “Common mistakes: garbled text, CDN rewrites, and invalid <head>”Mismatched bytes and label. The file is UTF-8 but the server sends charset=ISO-8859-1, or the meta says one label while bytes use another. Browsers may repaint or re-parse; users see mojibake.
Declaration after large inline scripts or comments in <head>. Anything that pushes the full <meta charset> past byte 1024 counts as late for insight and spec rules about early declaration.
Unquoted http-equiv content. Breaks charset extraction in some cases; use quoted content as Google documents.
CDN or edge HTML rewriting. Cloudflare notes that wrong or missing Content-Type/charset plus features that rewrite HTML (Email Obfuscation, Rocket Loader, Automatic HTTPS Rewrites) can disturb presentation. Fixing origin headers and charset in HTML is the first step; you can disable HTML-rewriting features or use a Configuration Rule to send content as-is.
Invalid elements before metadata in <head>. Google states that invalid markup in <head> can cause it to ignore elements after the invalid one—including charset-related meta tags.
Runtime APIs (UTF-8 in JavaScript environments)
Section titled “Runtime APIs (UTF-8 in JavaScript environments)”In Cloudflare Workers, TextEncoder always reports encoding as utf-8. TextDecoder exposes the decoder name, fatal, and ignoreBOM. That reflects how modern web runtimes standardise on UTF-8 for string/byte conversion, separate from legacy HTML document encodings the parser must still decode.
How to check this on your site
Section titled “How to check this on your site”-
Open Chrome DevTools Performance insights or run Lighthouse and confirm Declare a character encoding passes (early
<meta charset>orContent-Typewith charset). -
In browser DevTools → Network, select the HTML document and verify the response header
Content-Type: text/html; charset=UTF-8(or your chosen IANA name that matches the file bytes). -
View page source or fetch raw bytes and confirm
<meta charset="utf-8">(or equivalent) appears entirely within the first 1024 bytes and is the first child of<head>where possible. -
If text is wrong only behind a CDN, follow Cloudflare content encoding troubleshooting to verify origin
charsetand rule out HTML-rewriting features. -
You can check automatically in Answerwell (our tool): https://answerwell.org/workspace
Report an error or ask a question
Section titled “Report an error or ask a question”If something on this page is wrong or unclear, email yimasynthetic@gmail.com with the page URL and the exact sentence you are asking about, and we will correct or clarify it.