Menel Menel - 3 months ago 5
HTML Question

Declaring character encodings in HTML

Should I declare charset like this:

<meta http-equiv="content-type" content="text/html" charset="UTF-8">

or like this:

<meta http-equiv="content-type" content="text/html; charset=UTF-8">

Or are both valid?


Updated in light of @Alohci's comment.

See W3C's documentation on <meta http-equiv="...">:

encoding declaration state (http-equiv="content-type")

The encoding declaration state is just an alternative form of setting the charset attribute: it is a character encoding declaration. This state's user agent requirements are all handled by the parsing section of the specification.

For meta elements with an http-equiv attribute in the Encoding declaration state, the content attribute must have a value that is an ASCII case-insensitive match for a string that consists of: the literal string "text/html;", optionally followed by any number of space characters, followed by the literal string "charset=", followed by one of the labels of the character encoding of the character encoding declaration.

A document must not contain both a meta element with an http-equiv attribute in the encoding declaration state and a meta element with the charset attribute present. (emphasis mine)

Therefore, if you are going to use http-equiv, it must be used as <meta http-equiv="Content-Type" content="text/html; charset=utf-8">. But, that's just another way of saying <meta charset="utf-8">, so use the abbreviated form.